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 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 88 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 89 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 90 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 91 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 93 | #ifdef HAVE_FCNTL_H |
| 94 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 95 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 97 | #ifdef HAVE_GRP_H |
| 98 | #include <grp.h> |
| 99 | #endif |
| 100 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 101 | #ifdef HAVE_SYSEXITS_H |
| 102 | #include <sysexits.h> |
| 103 | #endif /* HAVE_SYSEXITS_H */ |
| 104 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 105 | #ifdef HAVE_SYS_LOADAVG_H |
| 106 | #include <sys/loadavg.h> |
| 107 | #endif |
| 108 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 109 | #ifdef HAVE_SYS_SENDFILE_H |
| 110 | #include <sys/sendfile.h> |
| 111 | #endif |
| 112 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 113 | #if defined(__APPLE__) |
| 114 | #include <copyfile.h> |
| 115 | #endif |
| 116 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 117 | #ifdef HAVE_SCHED_H |
| 118 | #include <sched.h> |
| 119 | #endif |
| 120 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 121 | #ifdef HAVE_COPY_FILE_RANGE |
| 122 | #include <unistd.h> |
| 123 | #endif |
| 124 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 125 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 126 | #undef HAVE_SCHED_SETAFFINITY |
| 127 | #endif |
| 128 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 129 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__) |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 130 | #define USE_XATTRS |
| 131 | #endif |
| 132 | |
| 133 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 134 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 135 | #endif |
| 136 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 137 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 138 | #ifdef HAVE_SYS_SOCKET_H |
| 139 | #include <sys/socket.h> |
| 140 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 141 | #endif |
| 142 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 143 | #ifdef HAVE_DLFCN_H |
| 144 | #include <dlfcn.h> |
| 145 | #endif |
| 146 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 147 | #ifdef __hpux |
| 148 | #include <sys/mpctl.h> |
| 149 | #endif |
| 150 | |
| 151 | #if defined(__DragonFly__) || \ |
| 152 | defined(__OpenBSD__) || \ |
| 153 | defined(__FreeBSD__) || \ |
| 154 | defined(__NetBSD__) || \ |
| 155 | defined(__APPLE__) |
| 156 | #include <sys/sysctl.h> |
| 157 | #endif |
| 158 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 159 | #ifdef HAVE_LINUX_RANDOM_H |
| 160 | # include <linux/random.h> |
| 161 | #endif |
| 162 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 163 | # include <sys/syscall.h> |
| 164 | #endif |
| 165 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 166 | #if defined(MS_WINDOWS) |
| 167 | # define TERMSIZE_USE_CONIO |
| 168 | #elif defined(HAVE_SYS_IOCTL_H) |
| 169 | # include <sys/ioctl.h> |
| 170 | # if defined(HAVE_TERMIOS_H) |
| 171 | # include <termios.h> |
| 172 | # endif |
| 173 | # if defined(TIOCGWINSZ) |
| 174 | # define TERMSIZE_USE_IOCTL |
| 175 | # endif |
| 176 | #endif /* MS_WINDOWS */ |
| 177 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 178 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 179 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 180 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 181 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 182 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 183 | #include <process.h> |
| 184 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 185 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 186 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 187 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 188 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 189 | #define HAVE_EXECV 1 |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 190 | #define HAVE_WSPAWNV 1 |
| 191 | #define HAVE_WEXECV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 192 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 193 | #define HAVE_SYSTEM 1 |
| 194 | #define HAVE_CWAIT 1 |
| 195 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 196 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 197 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 198 | /* Unix functions that the configure script doesn't check for */ |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 199 | #ifndef __VXWORKS__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 200 | #define HAVE_EXECV 1 |
| 201 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 202 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 203 | #define HAVE_FORK1 1 |
| 204 | #endif |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 205 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 206 | #define HAVE_GETEGID 1 |
| 207 | #define HAVE_GETEUID 1 |
| 208 | #define HAVE_GETGID 1 |
| 209 | #define HAVE_GETPPID 1 |
| 210 | #define HAVE_GETUID 1 |
| 211 | #define HAVE_KILL 1 |
| 212 | #define HAVE_OPENDIR 1 |
| 213 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 214 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 215 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 216 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 217 | #endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 218 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 219 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 220 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 221 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 222 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 223 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 224 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 225 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 226 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 227 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 228 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 229 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 230 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 231 | (default) */ |
| 232 | extern char *ctermid_r(char *); |
| 233 | #endif |
| 234 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 235 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 236 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 237 | #if defined(__VXWORKS__) |
| 238 | #include <vxCpuLib.h> |
| 239 | #include <rtpLib.h> |
| 240 | #include <wait.h> |
| 241 | #include <taskLib.h> |
| 242 | #ifndef _P_WAIT |
| 243 | #define _P_WAIT 0 |
| 244 | #define _P_NOWAIT 1 |
| 245 | #define _P_NOWAITO 1 |
| 246 | #endif |
| 247 | #endif /* __VXWORKS__ */ |
| 248 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 249 | #ifdef HAVE_POSIX_SPAWN |
| 250 | #include <spawn.h> |
| 251 | #endif |
| 252 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 253 | #ifdef HAVE_UTIME_H |
| 254 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 255 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 257 | #ifdef HAVE_SYS_UTIME_H |
| 258 | #include <sys/utime.h> |
| 259 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 260 | #endif /* HAVE_SYS_UTIME_H */ |
| 261 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 262 | #ifdef HAVE_SYS_TIMES_H |
| 263 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 264 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | |
| 266 | #ifdef HAVE_SYS_PARAM_H |
| 267 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 268 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 269 | |
| 270 | #ifdef HAVE_SYS_UTSNAME_H |
| 271 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 272 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 274 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 275 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 276 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 277 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 278 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 279 | #include <direct.h> |
| 280 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 281 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 282 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 283 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 284 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 285 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 286 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 287 | #endif |
| 288 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 289 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 290 | #endif |
| 291 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 292 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 293 | #endif |
| 294 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 296 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 297 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 298 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 299 | #endif |
| 300 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 301 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 302 | #endif |
| 303 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 304 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 305 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 306 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 307 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 308 | #endif |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 309 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 310 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 311 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 312 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 313 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 314 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 315 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 316 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 317 | #define HAVE_SYMLINK |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 318 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 319 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 320 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 321 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 322 | #define MAXPATHLEN PATH_MAX |
| 323 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 324 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 325 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 326 | #endif /* MAXPATHLEN */ |
| 327 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 328 | #ifdef UNION_WAIT |
| 329 | /* Emulate some macros on systems that have a union instead of macros */ |
| 330 | |
| 331 | #ifndef WIFEXITED |
| 332 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 333 | #endif |
| 334 | |
| 335 | #ifndef WEXITSTATUS |
| 336 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 337 | #endif |
| 338 | |
| 339 | #ifndef WTERMSIG |
| 340 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 341 | #endif |
| 342 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 343 | #define WAIT_TYPE union wait |
| 344 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 345 | |
| 346 | #else /* !UNION_WAIT */ |
| 347 | #define WAIT_TYPE int |
| 348 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 349 | #endif /* UNION_WAIT */ |
| 350 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 351 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 352 | prototype for it, at least on Solaris -- maybe others as well?). */ |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 353 | #if defined(HAVE_CTERMID_R) |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 354 | #define USE_CTERMID_R |
| 355 | #endif |
| 356 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 357 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 358 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 359 | #undef FSTAT |
| 360 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 361 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 362 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 363 | # define LSTAT win32_lstat |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 364 | # define FSTAT _Py_fstat_noraise |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 365 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 366 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 367 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 368 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 369 | # define FSTAT fstat |
| 370 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 371 | #endif |
| 372 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 373 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 374 | #include <sys/mkdev.h> |
| 375 | #else |
| 376 | #if defined(MAJOR_IN_SYSMACROS) |
| 377 | #include <sys/sysmacros.h> |
| 378 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 379 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 380 | #include <sys/mkdev.h> |
| 381 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 382 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 383 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 384 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 385 | #define INITFUNC PyInit_nt |
| 386 | #define MODNAME "nt" |
| 387 | #else |
| 388 | #define INITFUNC PyInit_posix |
| 389 | #define MODNAME "posix" |
| 390 | #endif |
| 391 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 392 | #if defined(__sun) |
| 393 | /* Something to implement in autoconf, not present in autoconf 2.69 */ |
| 394 | #define HAVE_STRUCT_STAT_ST_FSTYPE 1 |
| 395 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 396 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 397 | /* memfd_create is either defined in sys/mman.h or sys/memfd.h |
| 398 | * linux/memfd.h defines additional flags |
| 399 | */ |
| 400 | #ifdef HAVE_SYS_MMAN_H |
| 401 | #include <sys/mman.h> |
| 402 | #endif |
| 403 | #ifdef HAVE_SYS_MEMFD_H |
| 404 | #include <sys/memfd.h> |
| 405 | #endif |
| 406 | #ifdef HAVE_LINUX_MEMFD_H |
| 407 | #include <linux/memfd.h> |
| 408 | #endif |
| 409 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 410 | #ifdef _Py_MEMORY_SANITIZER |
| 411 | # include <sanitizer/msan_interface.h> |
| 412 | #endif |
| 413 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 414 | #ifdef HAVE_FORK |
| 415 | static void |
| 416 | run_at_forkers(PyObject *lst, int reverse) |
| 417 | { |
| 418 | Py_ssize_t i; |
| 419 | PyObject *cpy; |
| 420 | |
| 421 | if (lst != NULL) { |
| 422 | assert(PyList_CheckExact(lst)); |
| 423 | |
| 424 | /* Use a list copy in case register_at_fork() is called from |
| 425 | * one of the callbacks. |
| 426 | */ |
| 427 | cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst)); |
| 428 | if (cpy == NULL) |
| 429 | PyErr_WriteUnraisable(lst); |
| 430 | else { |
| 431 | if (reverse) |
| 432 | PyList_Reverse(cpy); |
| 433 | for (i = 0; i < PyList_GET_SIZE(cpy); i++) { |
| 434 | PyObject *func, *res; |
| 435 | func = PyList_GET_ITEM(cpy, i); |
Jeroen Demeyer | 7f41c8e | 2019-07-04 12:35:31 +0200 | [diff] [blame] | 436 | res = _PyObject_CallNoArg(func); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 437 | if (res == NULL) |
| 438 | PyErr_WriteUnraisable(func); |
| 439 | else |
| 440 | Py_DECREF(res); |
| 441 | } |
| 442 | Py_DECREF(cpy); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | void |
| 448 | PyOS_BeforeFork(void) |
| 449 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 450 | run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 451 | |
| 452 | _PyImport_AcquireLock(); |
| 453 | } |
| 454 | |
| 455 | void |
| 456 | PyOS_AfterFork_Parent(void) |
| 457 | { |
| 458 | if (_PyImport_ReleaseLock() <= 0) |
| 459 | Py_FatalError("failed releasing import lock after fork"); |
| 460 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 461 | run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void |
| 465 | PyOS_AfterFork_Child(void) |
| 466 | { |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 467 | _PyRuntimeState *runtime = &_PyRuntime; |
| 468 | _PyGILState_Reinit(runtime); |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 469 | _PyEval_ReInitThreads(runtime); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 470 | _PyImport_ReInitLock(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 471 | _PySignal_AfterFork(); |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 472 | _PyRuntimeState_ReInitThreads(runtime); |
Victor Stinner | b49858b | 2019-05-24 15:20:23 +0200 | [diff] [blame] | 473 | _PyInterpreterState_DeleteExceptMain(runtime); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 474 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 475 | run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static int |
| 479 | register_at_forker(PyObject **lst, PyObject *func) |
| 480 | { |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 481 | if (func == NULL) /* nothing to register? do nothing. */ |
| 482 | return 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 483 | if (*lst == NULL) { |
| 484 | *lst = PyList_New(0); |
| 485 | if (*lst == NULL) |
| 486 | return -1; |
| 487 | } |
| 488 | return PyList_Append(*lst, func); |
| 489 | } |
| 490 | #endif |
| 491 | |
| 492 | /* Legacy wrapper */ |
| 493 | void |
| 494 | PyOS_AfterFork(void) |
| 495 | { |
| 496 | #ifdef HAVE_FORK |
| 497 | PyOS_AfterFork_Child(); |
| 498 | #endif |
| 499 | } |
| 500 | |
| 501 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 502 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 503 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 504 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 505 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 506 | ULONG, struct _Py_stat_struct *); |
| 507 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 508 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 509 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 510 | #ifndef MS_WINDOWS |
| 511 | PyObject * |
| 512 | _PyLong_FromUid(uid_t uid) |
| 513 | { |
| 514 | if (uid == (uid_t)-1) |
| 515 | return PyLong_FromLong(-1); |
| 516 | return PyLong_FromUnsignedLong(uid); |
| 517 | } |
| 518 | |
| 519 | PyObject * |
| 520 | _PyLong_FromGid(gid_t gid) |
| 521 | { |
| 522 | if (gid == (gid_t)-1) |
| 523 | return PyLong_FromLong(-1); |
| 524 | return PyLong_FromUnsignedLong(gid); |
| 525 | } |
| 526 | |
| 527 | int |
| 528 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 529 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 530 | uid_t uid; |
| 531 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 532 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 533 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 534 | unsigned long uresult; |
| 535 | |
| 536 | index = PyNumber_Index(obj); |
| 537 | if (index == NULL) { |
| 538 | PyErr_Format(PyExc_TypeError, |
| 539 | "uid should be integer, not %.200s", |
| 540 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 541 | return 0; |
| 542 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 543 | |
| 544 | /* |
| 545 | * Handling uid_t is complicated for two reasons: |
| 546 | * * Although uid_t is (always?) unsigned, it still |
| 547 | * accepts -1. |
| 548 | * * We don't know its size in advance--it may be |
| 549 | * bigger than an int, or it may be smaller than |
| 550 | * a long. |
| 551 | * |
| 552 | * So a bit of defensive programming is in order. |
| 553 | * Start with interpreting the value passed |
| 554 | * in as a signed long and see if it works. |
| 555 | */ |
| 556 | |
| 557 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 558 | |
| 559 | if (!overflow) { |
| 560 | uid = (uid_t)result; |
| 561 | |
| 562 | if (result == -1) { |
| 563 | if (PyErr_Occurred()) |
| 564 | goto fail; |
| 565 | /* It's a legitimate -1, we're done. */ |
| 566 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 567 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 568 | |
| 569 | /* Any other negative number is disallowed. */ |
| 570 | if (result < 0) |
| 571 | goto underflow; |
| 572 | |
| 573 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 574 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 575 | (long)uid != result) |
| 576 | goto underflow; |
| 577 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 578 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 579 | |
| 580 | if (overflow < 0) |
| 581 | goto underflow; |
| 582 | |
| 583 | /* |
| 584 | * Okay, the value overflowed a signed long. If it |
| 585 | * fits in an *unsigned* long, it may still be okay, |
| 586 | * as uid_t may be unsigned long on this platform. |
| 587 | */ |
| 588 | uresult = PyLong_AsUnsignedLong(index); |
| 589 | if (PyErr_Occurred()) { |
| 590 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 591 | goto overflow; |
| 592 | goto fail; |
| 593 | } |
| 594 | |
| 595 | uid = (uid_t)uresult; |
| 596 | |
| 597 | /* |
| 598 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 599 | * but this value would get interpreted as (uid_t)-1 by chown |
| 600 | * and its siblings. That's not what the user meant! So we |
| 601 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 602 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 603 | */ |
| 604 | if (uid == (uid_t)-1) |
| 605 | goto overflow; |
| 606 | |
| 607 | /* Ensure the value wasn't truncated. */ |
| 608 | if (sizeof(uid_t) < sizeof(long) && |
| 609 | (unsigned long)uid != uresult) |
| 610 | goto overflow; |
| 611 | /* fallthrough */ |
| 612 | |
| 613 | success: |
| 614 | Py_DECREF(index); |
| 615 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 616 | return 1; |
| 617 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 618 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 619 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 620 | "uid is less than minimum"); |
| 621 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 622 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 623 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 624 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 625 | "uid is greater than maximum"); |
| 626 | /* fallthrough */ |
| 627 | |
| 628 | fail: |
| 629 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | int |
| 634 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 635 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 636 | gid_t gid; |
| 637 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 638 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 639 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 640 | unsigned long uresult; |
| 641 | |
| 642 | index = PyNumber_Index(obj); |
| 643 | if (index == NULL) { |
| 644 | PyErr_Format(PyExc_TypeError, |
| 645 | "gid should be integer, not %.200s", |
| 646 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 647 | return 0; |
| 648 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 649 | |
| 650 | /* |
| 651 | * Handling gid_t is complicated for two reasons: |
| 652 | * * Although gid_t is (always?) unsigned, it still |
| 653 | * accepts -1. |
| 654 | * * We don't know its size in advance--it may be |
| 655 | * bigger than an int, or it may be smaller than |
| 656 | * a long. |
| 657 | * |
| 658 | * So a bit of defensive programming is in order. |
| 659 | * Start with interpreting the value passed |
| 660 | * in as a signed long and see if it works. |
| 661 | */ |
| 662 | |
| 663 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 664 | |
| 665 | if (!overflow) { |
| 666 | gid = (gid_t)result; |
| 667 | |
| 668 | if (result == -1) { |
| 669 | if (PyErr_Occurred()) |
| 670 | goto fail; |
| 671 | /* It's a legitimate -1, we're done. */ |
| 672 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 673 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 674 | |
| 675 | /* Any other negative number is disallowed. */ |
| 676 | if (result < 0) { |
| 677 | goto underflow; |
| 678 | } |
| 679 | |
| 680 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 681 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 682 | (long)gid != result) |
| 683 | goto underflow; |
| 684 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 685 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 686 | |
| 687 | if (overflow < 0) |
| 688 | goto underflow; |
| 689 | |
| 690 | /* |
| 691 | * Okay, the value overflowed a signed long. If it |
| 692 | * fits in an *unsigned* long, it may still be okay, |
| 693 | * as gid_t may be unsigned long on this platform. |
| 694 | */ |
| 695 | uresult = PyLong_AsUnsignedLong(index); |
| 696 | if (PyErr_Occurred()) { |
| 697 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 698 | goto overflow; |
| 699 | goto fail; |
| 700 | } |
| 701 | |
| 702 | gid = (gid_t)uresult; |
| 703 | |
| 704 | /* |
| 705 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 706 | * but this value would get interpreted as (gid_t)-1 by chown |
| 707 | * and its siblings. That's not what the user meant! So we |
| 708 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 709 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 710 | */ |
| 711 | if (gid == (gid_t)-1) |
| 712 | goto overflow; |
| 713 | |
| 714 | /* Ensure the value wasn't truncated. */ |
| 715 | if (sizeof(gid_t) < sizeof(long) && |
| 716 | (unsigned long)gid != uresult) |
| 717 | goto overflow; |
| 718 | /* fallthrough */ |
| 719 | |
| 720 | success: |
| 721 | Py_DECREF(index); |
| 722 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 723 | return 1; |
| 724 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 725 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 726 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 727 | "gid is less than minimum"); |
| 728 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 729 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 730 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 731 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 732 | "gid is greater than maximum"); |
| 733 | /* fallthrough */ |
| 734 | |
| 735 | fail: |
| 736 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 737 | return 0; |
| 738 | } |
| 739 | #endif /* MS_WINDOWS */ |
| 740 | |
| 741 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 742 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 743 | |
| 744 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 745 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 746 | static int |
| 747 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 748 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 749 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 750 | if (PyErr_Occurred()) |
| 751 | return 0; |
| 752 | return 1; |
| 753 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 754 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 755 | |
| 756 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 757 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 758 | /* |
| 759 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 760 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 761 | * which doesn't play nicely with all the initializer lines in this file that |
| 762 | * look like this: |
| 763 | * int dir_fd = DEFAULT_DIR_FD; |
| 764 | */ |
| 765 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 766 | #else |
| 767 | #define DEFAULT_DIR_FD (-100) |
| 768 | #endif |
| 769 | |
| 770 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 771 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 772 | { |
| 773 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 774 | long long_value; |
| 775 | |
| 776 | PyObject *index = PyNumber_Index(o); |
| 777 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 778 | return 0; |
| 779 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 780 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 781 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 782 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 783 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 784 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 785 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 786 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 787 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 788 | return 0; |
| 789 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 790 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 791 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 792 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 793 | return 0; |
| 794 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 795 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 796 | *p = (int)long_value; |
| 797 | return 1; |
| 798 | } |
| 799 | |
| 800 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 801 | dir_fd_converter(PyObject *o, void *p) |
| 802 | { |
| 803 | if (o == Py_None) { |
| 804 | *(int *)p = DEFAULT_DIR_FD; |
| 805 | return 1; |
| 806 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 807 | else if (PyIndex_Check(o)) { |
| 808 | return _fd_converter(o, (int *)p); |
| 809 | } |
| 810 | else { |
| 811 | PyErr_Format(PyExc_TypeError, |
| 812 | "argument should be integer or None, not %.200s", |
| 813 | Py_TYPE(o)->tp_name); |
| 814 | return 0; |
| 815 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 819 | /* |
| 820 | * A PyArg_ParseTuple "converter" function |
| 821 | * that handles filesystem paths in the manner |
| 822 | * preferred by the os module. |
| 823 | * |
| 824 | * path_converter accepts (Unicode) strings and their |
| 825 | * subclasses, and bytes and their subclasses. What |
| 826 | * it does with the argument depends on the platform: |
| 827 | * |
| 828 | * * On Windows, if we get a (Unicode) string we |
| 829 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 830 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 831 | * |
| 832 | * * On all other platforms, strings are encoded |
| 833 | * to bytes using PyUnicode_FSConverter, then we |
| 834 | * extract the char * from the bytes object and |
| 835 | * return that. |
| 836 | * |
| 837 | * path_converter also optionally accepts signed |
| 838 | * integers (representing open file descriptors) instead |
| 839 | * of path strings. |
| 840 | * |
| 841 | * Input fields: |
| 842 | * path.nullable |
| 843 | * If nonzero, the path is permitted to be None. |
| 844 | * path.allow_fd |
| 845 | * If nonzero, the path is permitted to be a file handle |
| 846 | * (a signed int) instead of a string. |
| 847 | * path.function_name |
| 848 | * If non-NULL, path_converter will use that as the name |
| 849 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 850 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 851 | * path.argument_name |
| 852 | * If non-NULL, path_converter will use that as the name |
| 853 | * of the parameter in error messages. |
| 854 | * (If path.argument_name is NULL it uses "path".) |
| 855 | * |
| 856 | * Output fields: |
| 857 | * path.wide |
| 858 | * Points to the path if it was expressed as Unicode |
| 859 | * and was not encoded. (Only used on Windows.) |
| 860 | * path.narrow |
| 861 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 862 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 863 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 864 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 865 | * path.fd |
| 866 | * Contains a file descriptor if path.accept_fd was true |
| 867 | * and the caller provided a signed integer instead of any |
| 868 | * sort of string. |
| 869 | * |
| 870 | * WARNING: if your "path" parameter is optional, and is |
| 871 | * unspecified, path_converter will never get called. |
| 872 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 873 | * yourself! |
| 874 | * path.length |
| 875 | * The length of the path in characters, if specified as |
| 876 | * a string. |
| 877 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 878 | * The original object passed in (if get a PathLike object, |
| 879 | * the result of PyOS_FSPath() is treated as the original object). |
| 880 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 881 | * path.cleanup |
| 882 | * For internal use only. May point to a temporary object. |
| 883 | * (Pay no attention to the man behind the curtain.) |
| 884 | * |
| 885 | * At most one of path.wide or path.narrow will be non-NULL. |
| 886 | * If path was None and path.nullable was set, |
| 887 | * or if path was an integer and path.allow_fd was set, |
| 888 | * both path.wide and path.narrow will be NULL |
| 889 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 890 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 891 | * path_converter takes care to not write to the path_t |
| 892 | * unless it's successful. However it must reset the |
| 893 | * "cleanup" field each time it's called. |
| 894 | * |
| 895 | * Use as follows: |
| 896 | * path_t path; |
| 897 | * memset(&path, 0, sizeof(path)); |
| 898 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 899 | * // ... use values from path ... |
| 900 | * path_cleanup(&path); |
| 901 | * |
| 902 | * (Note that if PyArg_Parse fails you don't need to call |
| 903 | * path_cleanup(). However it is safe to do so.) |
| 904 | */ |
| 905 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 906 | const char *function_name; |
| 907 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 908 | int nullable; |
| 909 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 910 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 911 | #ifdef MS_WINDOWS |
| 912 | BOOL narrow; |
| 913 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 914 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 915 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 916 | int fd; |
| 917 | Py_ssize_t length; |
| 918 | PyObject *object; |
| 919 | PyObject *cleanup; |
| 920 | } path_t; |
| 921 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 922 | #ifdef MS_WINDOWS |
| 923 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 924 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 925 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 926 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 927 | {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] | 928 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 929 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 930 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 931 | path_cleanup(path_t *path) |
| 932 | { |
| 933 | Py_CLEAR(path->object); |
| 934 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 938 | path_converter(PyObject *o, void *p) |
| 939 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 940 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 941 | PyObject *bytes = NULL; |
| 942 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 943 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 944 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 945 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 946 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 947 | const wchar_t *wide; |
| 948 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 949 | |
| 950 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 951 | PyErr_Format(exc, "%s%s" fmt, \ |
| 952 | path->function_name ? path->function_name : "", \ |
| 953 | path->function_name ? ": " : "", \ |
| 954 | path->argument_name ? path->argument_name : "path") |
| 955 | |
| 956 | /* Py_CLEANUP_SUPPORTED support */ |
| 957 | if (o == NULL) { |
| 958 | path_cleanup(path); |
| 959 | return 1; |
| 960 | } |
| 961 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 962 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 963 | path->object = path->cleanup = NULL; |
| 964 | /* path->object owns a reference to the original object */ |
| 965 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 966 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 967 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 968 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 969 | #ifdef MS_WINDOWS |
| 970 | path->narrow = FALSE; |
| 971 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 972 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 973 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 974 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 975 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 978 | /* Only call this here so that we don't treat the return value of |
| 979 | os.fspath() as an fd or buffer. */ |
| 980 | is_index = path->allow_fd && PyIndex_Check(o); |
| 981 | is_buffer = PyObject_CheckBuffer(o); |
| 982 | is_bytes = PyBytes_Check(o); |
| 983 | is_unicode = PyUnicode_Check(o); |
| 984 | |
| 985 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 986 | /* Inline PyOS_FSPath() for better error messages. */ |
| 987 | _Py_IDENTIFIER(__fspath__); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 988 | PyObject *func, *res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 989 | |
| 990 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 991 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 992 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 993 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 994 | res = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 995 | Py_DECREF(func); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 996 | if (NULL == res) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 997 | goto error_exit; |
| 998 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 999 | else if (PyUnicode_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1000 | is_unicode = 1; |
| 1001 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1002 | else if (PyBytes_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1003 | is_bytes = 1; |
| 1004 | } |
| 1005 | else { |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1006 | PyErr_Format(PyExc_TypeError, |
| 1007 | "expected %.200s.__fspath__() to return str or bytes, " |
| 1008 | "not %.200s", Py_TYPE(o)->tp_name, |
| 1009 | Py_TYPE(res)->tp_name); |
| 1010 | Py_DECREF(res); |
| 1011 | goto error_exit; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1012 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1013 | |
| 1014 | /* still owns a reference to the original object */ |
| 1015 | Py_DECREF(o); |
| 1016 | o = res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1020 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1021 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1022 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1023 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1024 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1025 | if (length > 32767) { |
| 1026 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1027 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1028 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1029 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1030 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1031 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1032 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1033 | |
| 1034 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1035 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1036 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1037 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1038 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1039 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1040 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1041 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1042 | #endif |
| 1043 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1044 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1045 | bytes = o; |
| 1046 | Py_INCREF(bytes); |
| 1047 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1048 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1049 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1050 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1051 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1052 | "%s%s%s should be %s, not %.200s", |
| 1053 | path->function_name ? path->function_name : "", |
| 1054 | path->function_name ? ": " : "", |
| 1055 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1056 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1057 | "integer or None" : |
| 1058 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1059 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1060 | "string, bytes or os.PathLike", |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1061 | Py_TYPE(o)->tp_name)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1062 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1063 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1064 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1065 | if (!bytes) { |
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 | } |
| 1068 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1069 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1070 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1071 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1072 | } |
| 1073 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1074 | #ifdef MS_WINDOWS |
| 1075 | path->narrow = FALSE; |
| 1076 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1077 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1078 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1079 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1080 | } |
| 1081 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1082 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1083 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1084 | path->function_name ? path->function_name : "", |
| 1085 | path->function_name ? ": " : "", |
| 1086 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1087 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1088 | "integer or None" : |
| 1089 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1090 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1091 | "string, bytes or os.PathLike", |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1092 | Py_TYPE(o)->tp_name); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1093 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1096 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1097 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1098 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1099 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1100 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1103 | #ifdef MS_WINDOWS |
| 1104 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1105 | narrow, |
| 1106 | length |
| 1107 | ); |
| 1108 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1109 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1112 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1113 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1114 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1115 | } |
| 1116 | if (length > 32767) { |
| 1117 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1118 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1119 | } |
| 1120 | if (wcslen(wide) != length) { |
| 1121 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1122 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1123 | } |
| 1124 | path->wide = wide; |
| 1125 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1126 | path->cleanup = wo; |
| 1127 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1128 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1129 | path->wide = NULL; |
| 1130 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1131 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1132 | /* Still a reference owned by path->object, don't have to |
| 1133 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1134 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1135 | } |
| 1136 | else { |
| 1137 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1138 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1139 | #endif |
| 1140 | path->fd = -1; |
| 1141 | |
| 1142 | success_exit: |
| 1143 | path->length = length; |
| 1144 | path->object = o; |
| 1145 | return Py_CLEANUP_SUPPORTED; |
| 1146 | |
| 1147 | error_exit: |
| 1148 | Py_XDECREF(o); |
| 1149 | Py_XDECREF(bytes); |
| 1150 | #ifdef MS_WINDOWS |
| 1151 | Py_XDECREF(wo); |
| 1152 | #endif |
| 1153 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1157 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1158 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1159 | PyErr_Format(PyExc_NotImplementedError, |
| 1160 | "%s%s%s unavailable on this platform", |
| 1161 | (function_name != NULL) ? function_name : "", |
| 1162 | (function_name != NULL) ? ": ": "", |
| 1163 | argument_name); |
| 1164 | } |
| 1165 | |
| 1166 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1167 | dir_fd_unavailable(PyObject *o, void *p) |
| 1168 | { |
| 1169 | int dir_fd; |
| 1170 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1171 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1172 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1173 | argument_unavailable_error(NULL, "dir_fd"); |
| 1174 | return 0; |
| 1175 | } |
| 1176 | *(int *)p = dir_fd; |
| 1177 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1181 | fd_specified(const char *function_name, int fd) |
| 1182 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1183 | if (fd == -1) |
| 1184 | return 0; |
| 1185 | |
| 1186 | argument_unavailable_error(function_name, "fd"); |
| 1187 | return 1; |
| 1188 | } |
| 1189 | |
| 1190 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1191 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1192 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1193 | if (follow_symlinks) |
| 1194 | return 0; |
| 1195 | |
| 1196 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1197 | return 1; |
| 1198 | } |
| 1199 | |
| 1200 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1201 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1202 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1203 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1204 | #ifndef MS_WINDOWS |
| 1205 | && !path->narrow |
| 1206 | #endif |
| 1207 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1208 | PyErr_Format(PyExc_ValueError, |
| 1209 | "%s: can't specify dir_fd without matching path", |
| 1210 | function_name); |
| 1211 | return 1; |
| 1212 | } |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1217 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1218 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1219 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1220 | PyErr_Format(PyExc_ValueError, |
| 1221 | "%s: can't specify both dir_fd and fd", |
| 1222 | function_name); |
| 1223 | return 1; |
| 1224 | } |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
| 1228 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1229 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1230 | int follow_symlinks) |
| 1231 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1232 | if ((fd > 0) && (!follow_symlinks)) { |
| 1233 | PyErr_Format(PyExc_ValueError, |
| 1234 | "%s: cannot use fd and follow_symlinks together", |
| 1235 | function_name); |
| 1236 | return 1; |
| 1237 | } |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1242 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1243 | int follow_symlinks) |
| 1244 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1245 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1246 | PyErr_Format(PyExc_ValueError, |
| 1247 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1248 | function_name); |
| 1249 | return 1; |
| 1250 | } |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1254 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1255 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1256 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1257 | typedef off_t Py_off_t; |
| 1258 | #endif |
| 1259 | |
| 1260 | static int |
| 1261 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1262 | { |
| 1263 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1264 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1265 | #else |
| 1266 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1267 | #endif |
| 1268 | if (PyErr_Occurred()) |
| 1269 | return 0; |
| 1270 | return 1; |
| 1271 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1272 | |
| 1273 | static PyObject * |
| 1274 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1275 | { |
| 1276 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1277 | return PyLong_FromLongLong(offset); |
| 1278 | #else |
| 1279 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1280 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1281 | } |
| 1282 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1283 | #ifdef HAVE_SIGSET_T |
| 1284 | /* Convert an iterable of integers to a sigset. |
| 1285 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1286 | int |
| 1287 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1288 | { |
| 1289 | sigset_t *mask = (sigset_t *)addr; |
| 1290 | PyObject *iterator, *item; |
| 1291 | long signum; |
| 1292 | int overflow; |
| 1293 | |
Rémi Lapeyre | f090019 | 2019-05-04 01:30:53 +0200 | [diff] [blame] | 1294 | // The extra parens suppress the unreachable-code warning with clang on MacOS |
| 1295 | if (sigemptyset(mask) < (0)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1296 | /* Probably only if mask == NULL. */ |
| 1297 | PyErr_SetFromErrno(PyExc_OSError); |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | iterator = PyObject_GetIter(obj); |
| 1302 | if (iterator == NULL) { |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1307 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1308 | Py_DECREF(item); |
| 1309 | if (signum <= 0 || signum >= NSIG) { |
| 1310 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1311 | PyErr_Format(PyExc_ValueError, |
| 1312 | "signal number %ld out of range", signum); |
| 1313 | } |
| 1314 | goto error; |
| 1315 | } |
| 1316 | if (sigaddset(mask, (int)signum)) { |
| 1317 | if (errno != EINVAL) { |
| 1318 | /* Probably impossible */ |
| 1319 | PyErr_SetFromErrno(PyExc_OSError); |
| 1320 | goto error; |
| 1321 | } |
| 1322 | /* For backwards compatibility, allow idioms such as |
| 1323 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1324 | */ |
| 1325 | const char msg[] = |
| 1326 | "invalid signal number %ld, please use valid_signals()"; |
| 1327 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1328 | goto error; |
| 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | if (!PyErr_Occurred()) { |
| 1333 | Py_DECREF(iterator); |
| 1334 | return 1; |
| 1335 | } |
| 1336 | |
| 1337 | error: |
| 1338 | Py_DECREF(iterator); |
| 1339 | return 0; |
| 1340 | } |
| 1341 | #endif /* HAVE_SIGSET_T */ |
| 1342 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1343 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1344 | |
| 1345 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1346 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1347 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1348 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1349 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1350 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1351 | |
| 1352 | if (0 == DeviceIoControl( |
| 1353 | reparse_point_handle, |
| 1354 | FSCTL_GET_REPARSE_POINT, |
| 1355 | NULL, 0, /* in buffer */ |
| 1356 | target_buffer, sizeof(target_buffer), |
| 1357 | &n_bytes_returned, |
| 1358 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1359 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1360 | |
| 1361 | if (reparse_tag) |
| 1362 | *reparse_tag = rdb->ReparseTag; |
| 1363 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1364 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1365 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1366 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1367 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1368 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1369 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1370 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1371 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1372 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1373 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1374 | */ |
| 1375 | #include <crt_externs.h> |
| 1376 | static char **environ; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 1377 | #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1378 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1379 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1380 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1381 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1382 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1383 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1384 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1385 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1386 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1387 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1388 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1389 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1390 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1391 | d = PyDict_New(); |
| 1392 | if (d == NULL) |
| 1393 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1394 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1395 | if (environ == NULL) |
| 1396 | environ = *_NSGetEnviron(); |
| 1397 | #endif |
| 1398 | #ifdef MS_WINDOWS |
| 1399 | /* _wenviron must be initialized in this way if the program is started |
| 1400 | through main() instead of wmain(). */ |
| 1401 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1402 | e = _wenviron; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1403 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1404 | e = environ; |
| 1405 | #endif |
| 1406 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1407 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1408 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1409 | PyObject *k; |
| 1410 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1411 | #ifdef MS_WINDOWS |
| 1412 | const wchar_t *p = wcschr(*e, L'='); |
| 1413 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1414 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1415 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1416 | if (p == NULL) |
| 1417 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1418 | #ifdef MS_WINDOWS |
| 1419 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1420 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1421 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1422 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1423 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1424 | Py_DECREF(d); |
| 1425 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1426 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1427 | #ifdef MS_WINDOWS |
| 1428 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1429 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1430 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1431 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1432 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1433 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1434 | Py_DECREF(d); |
| 1435 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1436 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1437 | if (PyDict_GetItemWithError(d, k) == NULL) { |
| 1438 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { |
| 1439 | Py_DECREF(v); |
| 1440 | Py_DECREF(k); |
| 1441 | Py_DECREF(d); |
| 1442 | return NULL; |
| 1443 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1444 | } |
| 1445 | Py_DECREF(k); |
| 1446 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1447 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1448 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1451 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1452 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1453 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1454 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1455 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1456 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1457 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1458 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1459 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1460 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1461 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1462 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1463 | /* XXX We should pass the function name along in the future. |
| 1464 | (winreg.c also wants to pass the function name.) |
| 1465 | This would however require an additional param to the |
| 1466 | Windows error object, which is non-trivial. |
| 1467 | */ |
| 1468 | errno = GetLastError(); |
| 1469 | if (filename) |
| 1470 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1471 | else |
| 1472 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1473 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1474 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1475 | static PyObject * |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1476 | win32_error_object_err(const char* function, PyObject* filename, DWORD err) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1477 | { |
| 1478 | /* XXX - see win32_error for comments on 'function' */ |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1479 | if (filename) |
| 1480 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1481 | PyExc_OSError, |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1482 | err, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1483 | filename); |
| 1484 | else |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1485 | return PyErr_SetFromWindowsErr(err); |
| 1486 | } |
| 1487 | |
| 1488 | static PyObject * |
| 1489 | win32_error_object(const char* function, PyObject* filename) |
| 1490 | { |
| 1491 | errno = GetLastError(); |
| 1492 | return win32_error_object_err(function, filename, errno); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1493 | } |
| 1494 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1495 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1496 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1497 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1498 | posix_path_object_error(PyObject *path) |
| 1499 | { |
| 1500 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1501 | } |
| 1502 | |
| 1503 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1504 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1505 | { |
| 1506 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1507 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1508 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1509 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1510 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1511 | #endif |
| 1512 | } |
| 1513 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1514 | static PyObject * |
| 1515 | path_object_error2(PyObject *path, PyObject *path2) |
| 1516 | { |
| 1517 | #ifdef MS_WINDOWS |
| 1518 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1519 | PyExc_OSError, 0, path, path2); |
| 1520 | #else |
| 1521 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1522 | #endif |
| 1523 | } |
| 1524 | |
| 1525 | static PyObject * |
| 1526 | path_error(path_t *path) |
| 1527 | { |
| 1528 | return path_object_error(path->object); |
| 1529 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1530 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1531 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1532 | posix_path_error(path_t *path) |
| 1533 | { |
| 1534 | return posix_path_object_error(path->object); |
| 1535 | } |
| 1536 | |
| 1537 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1538 | path_error2(path_t *path, path_t *path2) |
| 1539 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1540 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1544 | /* POSIX generic methods */ |
| 1545 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1546 | static int |
| 1547 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1548 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1549 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1550 | int *pointer = (int *)p; |
| 1551 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1552 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1553 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1554 | *pointer = fd; |
| 1555 | return 1; |
| 1556 | } |
| 1557 | |
| 1558 | static PyObject * |
| 1559 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1560 | { |
| 1561 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1562 | int async_err = 0; |
| 1563 | |
| 1564 | do { |
| 1565 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1566 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1567 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1568 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1569 | Py_END_ALLOW_THREADS |
| 1570 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1571 | if (res != 0) |
| 1572 | return (!async_err) ? posix_error() : NULL; |
| 1573 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1574 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1575 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1576 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1577 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1578 | /* This is a reimplementation of the C library's chdir function, |
| 1579 | but one that produces Win32 errors instead of DOS error codes. |
| 1580 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1581 | it also needs to set "magic" environment variables indicating |
| 1582 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1583 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1584 | win32_wchdir(LPCWSTR path) |
| 1585 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1586 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1587 | int result; |
| 1588 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1589 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1590 | if(!SetCurrentDirectoryW(path)) |
| 1591 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1592 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1593 | if (!result) |
| 1594 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1595 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1596 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1597 | if (!new_path) { |
| 1598 | SetLastError(ERROR_OUTOFMEMORY); |
| 1599 | return FALSE; |
| 1600 | } |
| 1601 | result = GetCurrentDirectoryW(result, new_path); |
| 1602 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1603 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1604 | return FALSE; |
| 1605 | } |
| 1606 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1607 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1608 | wcsncmp(new_path, L"//", 2) == 0); |
| 1609 | if (!is_unc_like_path) { |
| 1610 | env[1] = new_path[0]; |
| 1611 | result = SetEnvironmentVariableW(env, new_path); |
| 1612 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1613 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1614 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1615 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1616 | } |
| 1617 | #endif |
| 1618 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1619 | #ifdef MS_WINDOWS |
| 1620 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1621 | - time stamps are restricted to second resolution |
| 1622 | - file modification times suffer from forth-and-back conversions between |
| 1623 | UTC and local time |
| 1624 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1625 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1626 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1627 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1628 | #define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1629 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1630 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1631 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1632 | BY_HANDLE_FILE_INFORMATION *info, |
| 1633 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1634 | { |
| 1635 | memset(info, 0, sizeof(*info)); |
| 1636 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1637 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1638 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1639 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1640 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1641 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1642 | /* info->nNumberOfLinks = 1; */ |
| 1643 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1644 | *reparse_tag = pFileData->dwReserved0; |
| 1645 | else |
| 1646 | *reparse_tag = 0; |
| 1647 | } |
| 1648 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1649 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1650 | 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] | 1651 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1652 | HANDLE hFindFile; |
| 1653 | WIN32_FIND_DATAW FileData; |
| 1654 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1655 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1656 | return FALSE; |
| 1657 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1658 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1659 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1662 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1663 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1664 | BOOL traverse) |
| 1665 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1666 | HANDLE hFile; |
| 1667 | BY_HANDLE_FILE_INFORMATION fileInfo; |
| 1668 | FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 }; |
| 1669 | DWORD fileType, error; |
| 1670 | BOOL isUnhandledTag = FALSE; |
| 1671 | int retval = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1672 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1673 | DWORD access = FILE_READ_ATTRIBUTES; |
| 1674 | DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */ |
| 1675 | if (!traverse) { |
| 1676 | flags |= FILE_FLAG_OPEN_REPARSE_POINT; |
| 1677 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1678 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1679 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1680 | if (hFile == INVALID_HANDLE_VALUE) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1681 | /* Either the path doesn't exist, or the caller lacks access. */ |
| 1682 | error = GetLastError(); |
| 1683 | switch (error) { |
| 1684 | case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */ |
| 1685 | case ERROR_SHARING_VIOLATION: /* It's a paging file. */ |
| 1686 | /* Try reading the parent directory. */ |
| 1687 | if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) { |
| 1688 | /* Cannot read the parent directory. */ |
| 1689 | SetLastError(error); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1690 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1691 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1692 | if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1693 | if (traverse || |
| 1694 | !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1695 | /* The stat call has to traverse but cannot, so fail. */ |
| 1696 | SetLastError(error); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1697 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1698 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1699 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1700 | break; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1701 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1702 | case ERROR_INVALID_PARAMETER: |
| 1703 | /* \\.\con requires read or write access. */ |
| 1704 | hFile = CreateFileW(path, access | GENERIC_READ, |
| 1705 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 1706 | OPEN_EXISTING, flags, NULL); |
| 1707 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1708 | SetLastError(error); |
| 1709 | return -1; |
| 1710 | } |
| 1711 | break; |
| 1712 | |
| 1713 | case ERROR_CANT_ACCESS_FILE: |
| 1714 | /* bpo37834: open unhandled reparse points if traverse fails. */ |
| 1715 | if (traverse) { |
| 1716 | traverse = FALSE; |
| 1717 | isUnhandledTag = TRUE; |
| 1718 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, |
| 1719 | flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 1720 | } |
| 1721 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1722 | SetLastError(error); |
| 1723 | return -1; |
| 1724 | } |
| 1725 | break; |
| 1726 | |
| 1727 | default: |
| 1728 | return -1; |
| 1729 | } |
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 | |
| 1732 | if (hFile != INVALID_HANDLE_VALUE) { |
| 1733 | /* Handle types other than files on disk. */ |
| 1734 | fileType = GetFileType(hFile); |
| 1735 | if (fileType != FILE_TYPE_DISK) { |
| 1736 | if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) { |
| 1737 | retval = -1; |
| 1738 | goto cleanup; |
| 1739 | } |
| 1740 | DWORD fileAttributes = GetFileAttributesW(path); |
| 1741 | memset(result, 0, sizeof(*result)); |
| 1742 | if (fileAttributes != INVALID_FILE_ATTRIBUTES && |
| 1743 | fileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 1744 | /* \\.\pipe\ or \\.\mailslot\ */ |
| 1745 | result->st_mode = _S_IFDIR; |
| 1746 | } else if (fileType == FILE_TYPE_CHAR) { |
| 1747 | /* \\.\nul */ |
| 1748 | result->st_mode = _S_IFCHR; |
| 1749 | } else if (fileType == FILE_TYPE_PIPE) { |
| 1750 | /* \\.\pipe\spam */ |
| 1751 | result->st_mode = _S_IFIFO; |
| 1752 | } |
| 1753 | /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */ |
| 1754 | goto cleanup; |
| 1755 | } |
| 1756 | |
| 1757 | /* Query the reparse tag, and traverse a non-link. */ |
| 1758 | if (!traverse) { |
| 1759 | if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo, |
| 1760 | &tagInfo, sizeof(tagInfo))) { |
| 1761 | /* Allow devices that do not support FileAttributeTagInfo. */ |
| 1762 | switch (GetLastError()) { |
| 1763 | case ERROR_INVALID_PARAMETER: |
| 1764 | case ERROR_INVALID_FUNCTION: |
| 1765 | case ERROR_NOT_SUPPORTED: |
| 1766 | tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL; |
| 1767 | tagInfo.ReparseTag = 0; |
| 1768 | break; |
| 1769 | default: |
| 1770 | retval = -1; |
| 1771 | goto cleanup; |
| 1772 | } |
| 1773 | } else if (tagInfo.FileAttributes & |
| 1774 | FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1775 | if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1776 | if (isUnhandledTag) { |
| 1777 | /* Traversing previously failed for either this link |
| 1778 | or its target. */ |
| 1779 | SetLastError(ERROR_CANT_ACCESS_FILE); |
| 1780 | retval = -1; |
| 1781 | goto cleanup; |
| 1782 | } |
| 1783 | /* Traverse a non-link, but not if traversing already failed |
| 1784 | for an unhandled tag. */ |
| 1785 | } else if (!isUnhandledTag) { |
| 1786 | CloseHandle(hFile); |
| 1787 | return win32_xstat_impl(path, result, TRUE); |
| 1788 | } |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | if (!GetFileInformationByHandle(hFile, &fileInfo)) { |
| 1793 | switch (GetLastError()) { |
| 1794 | case ERROR_INVALID_PARAMETER: |
| 1795 | case ERROR_INVALID_FUNCTION: |
| 1796 | case ERROR_NOT_SUPPORTED: |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1797 | /* Volumes and physical disks are block devices, e.g. |
| 1798 | \\.\C: and \\.\PhysicalDrive0. */ |
| 1799 | memset(result, 0, sizeof(*result)); |
| 1800 | result->st_mode = 0x6000; /* S_IFBLK */ |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1801 | goto cleanup; |
| 1802 | } |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1803 | retval = -1; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1804 | goto cleanup; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result); |
| 1809 | |
| 1810 | if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1811 | /* Fix the file execute permissions. This hack sets S_IEXEC if |
| 1812 | the filename has an extension that is commonly used by files |
| 1813 | that CreateProcessW can execute. A real implementation calls |
| 1814 | GetSecurityInfo, OpenThreadToken/OpenProcessToken, and |
| 1815 | AccessCheck to check for generic read, write, and execute |
| 1816 | access. */ |
| 1817 | const wchar_t *fileExtension = wcsrchr(path, '.'); |
| 1818 | if (fileExtension) { |
| 1819 | if (_wcsicmp(fileExtension, L".exe") == 0 || |
| 1820 | _wcsicmp(fileExtension, L".bat") == 0 || |
| 1821 | _wcsicmp(fileExtension, L".cmd") == 0 || |
| 1822 | _wcsicmp(fileExtension, L".com") == 0) { |
| 1823 | result->st_mode |= 0111; |
| 1824 | } |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | cleanup: |
| 1829 | if (hFile != INVALID_HANDLE_VALUE) { |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1830 | /* Preserve last error if we are failing */ |
| 1831 | error = retval ? GetLastError() : 0; |
| 1832 | if (!CloseHandle(hFile)) { |
| 1833 | retval = -1; |
| 1834 | } else if (retval) { |
| 1835 | /* Restore last error */ |
| 1836 | SetLastError(error); |
| 1837 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | return retval; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1844 | 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] | 1845 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1846 | /* Protocol violation: we explicitly clear errno, instead of |
| 1847 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1848 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1849 | errno = 0; |
| 1850 | return code; |
| 1851 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1852 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1853 | |
| 1854 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1855 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1856 | default does not traverse symlinks and instead returns attributes for |
| 1857 | the symlink. |
| 1858 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1859 | Instead, we will open the file (which *does* traverse symlinks by default) |
| 1860 | and GetFileInformationByHandle(). */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1861 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1862 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1863 | 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] | 1864 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1865 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1868 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1869 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1870 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1871 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1874 | #endif /* MS_WINDOWS */ |
| 1875 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1876 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1877 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1878 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1879 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1880 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1881 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1882 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1883 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1884 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1885 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1886 | |
| 1887 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1888 | {"st_mode", "protection bits"}, |
| 1889 | {"st_ino", "inode"}, |
| 1890 | {"st_dev", "device"}, |
| 1891 | {"st_nlink", "number of hard links"}, |
| 1892 | {"st_uid", "user ID of owner"}, |
| 1893 | {"st_gid", "group ID of owner"}, |
| 1894 | {"st_size", "total size, in bytes"}, |
| 1895 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1896 | {NULL, "integer time of last access"}, |
| 1897 | {NULL, "integer time of last modification"}, |
| 1898 | {NULL, "integer time of last change"}, |
| 1899 | {"st_atime", "time of last access"}, |
| 1900 | {"st_mtime", "time of last modification"}, |
| 1901 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1902 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1903 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1904 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1905 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1906 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1907 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1908 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1909 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1910 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1911 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1912 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1913 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1914 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1915 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1916 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1917 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1918 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1919 | #endif |
| 1920 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1921 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1922 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1923 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1924 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1925 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1926 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1927 | {"st_fstype", "Type of filesystem"}, |
| 1928 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1929 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 1930 | {"st_reparse_tag", "Windows reparse tag"}, |
| 1931 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1932 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1933 | }; |
| 1934 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1935 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1936 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1937 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1938 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1939 | #endif |
| 1940 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1941 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1942 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1943 | #else |
| 1944 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1945 | #endif |
| 1946 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1947 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1948 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1949 | #else |
| 1950 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1951 | #endif |
| 1952 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1953 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1954 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1955 | #else |
| 1956 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1957 | #endif |
| 1958 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1959 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1960 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1961 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1962 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1963 | #endif |
| 1964 | |
| 1965 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1966 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1967 | #else |
| 1968 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1969 | #endif |
| 1970 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1971 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1972 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1973 | #else |
| 1974 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 1975 | #endif |
| 1976 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1977 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1978 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 1979 | #else |
| 1980 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 1981 | #endif |
| 1982 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1983 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 1984 | #define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1) |
| 1985 | #else |
| 1986 | #define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX |
| 1987 | #endif |
| 1988 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1989 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1990 | "stat_result", /* name */ |
| 1991 | stat_result__doc__, /* doc */ |
| 1992 | stat_result_fields, |
| 1993 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1994 | }; |
| 1995 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1996 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1997 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1998 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1999 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2000 | 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] | 2001 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2002 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2003 | |
| 2004 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2005 | {"f_bsize", }, |
| 2006 | {"f_frsize", }, |
| 2007 | {"f_blocks", }, |
| 2008 | {"f_bfree", }, |
| 2009 | {"f_bavail", }, |
| 2010 | {"f_files", }, |
| 2011 | {"f_ffree", }, |
| 2012 | {"f_favail", }, |
| 2013 | {"f_flag", }, |
| 2014 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 2015 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2016 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2017 | }; |
| 2018 | |
| 2019 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2020 | "statvfs_result", /* name */ |
| 2021 | statvfs_result__doc__, /* doc */ |
| 2022 | statvfs_result_fields, |
| 2023 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2024 | }; |
| 2025 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2026 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2027 | PyDoc_STRVAR(waitid_result__doc__, |
| 2028 | "waitid_result: Result from waitid.\n\n\ |
| 2029 | This object may be accessed either as a tuple of\n\ |
| 2030 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2031 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2032 | \n\ |
| 2033 | See os.waitid for more information."); |
| 2034 | |
| 2035 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2036 | {"si_pid", }, |
| 2037 | {"si_uid", }, |
| 2038 | {"si_signo", }, |
| 2039 | {"si_status", }, |
| 2040 | {"si_code", }, |
| 2041 | {0} |
| 2042 | }; |
| 2043 | |
| 2044 | static PyStructSequence_Desc waitid_result_desc = { |
| 2045 | "waitid_result", /* name */ |
| 2046 | waitid_result__doc__, /* doc */ |
| 2047 | waitid_result_fields, |
| 2048 | 5 |
| 2049 | }; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2050 | static PyTypeObject* WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2051 | #endif |
| 2052 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2053 | static int initialized; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2054 | static PyTypeObject* StatResultType; |
| 2055 | static PyTypeObject* StatVFSResultType; |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 2056 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2057 | static PyTypeObject* SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2058 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2059 | static newfunc structseq_new; |
| 2060 | |
| 2061 | static PyObject * |
| 2062 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2063 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2064 | PyStructSequence *result; |
| 2065 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2066 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2067 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2068 | if (!result) |
| 2069 | return NULL; |
| 2070 | /* If we have been initialized from a tuple, |
| 2071 | st_?time might be set to None. Initialize it |
| 2072 | from the int slots. */ |
| 2073 | for (i = 7; i <= 9; i++) { |
| 2074 | if (result->ob_item[i+3] == Py_None) { |
| 2075 | Py_DECREF(Py_None); |
| 2076 | Py_INCREF(result->ob_item[i]); |
| 2077 | result->ob_item[i+3] = result->ob_item[i]; |
| 2078 | } |
| 2079 | } |
| 2080 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2084 | static PyObject *billion = NULL; |
| 2085 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2086 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2087 | 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] | 2088 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2089 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2090 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2091 | PyObject *s_in_ns = NULL; |
| 2092 | PyObject *ns_total = NULL; |
| 2093 | PyObject *float_s = NULL; |
| 2094 | |
| 2095 | if (!(s && ns_fractional)) |
| 2096 | goto exit; |
| 2097 | |
| 2098 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2099 | if (!s_in_ns) |
| 2100 | goto exit; |
| 2101 | |
| 2102 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2103 | if (!ns_total) |
| 2104 | goto exit; |
| 2105 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2106 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2107 | if (!float_s) { |
| 2108 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | PyStructSequence_SET_ITEM(v, index, s); |
| 2112 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2113 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2114 | s = NULL; |
| 2115 | float_s = NULL; |
| 2116 | ns_total = NULL; |
| 2117 | exit: |
| 2118 | Py_XDECREF(s); |
| 2119 | Py_XDECREF(ns_fractional); |
| 2120 | Py_XDECREF(s_in_ns); |
| 2121 | Py_XDECREF(ns_total); |
| 2122 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2125 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2126 | (used by posix_stat() and posix_fstat()) */ |
| 2127 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2128 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2129 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2130 | unsigned long ansec, mnsec, cnsec; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2131 | PyObject *v = PyStructSequence_New(StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2132 | if (v == NULL) |
| 2133 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2134 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2135 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2136 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2137 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2138 | #ifdef MS_WINDOWS |
| 2139 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2140 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2141 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2142 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2143 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2144 | #if defined(MS_WINDOWS) |
| 2145 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2146 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2147 | #else |
| 2148 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2149 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2150 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2151 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2152 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2153 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2154 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2155 | ansec = st->st_atim.tv_nsec; |
| 2156 | mnsec = st->st_mtim.tv_nsec; |
| 2157 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2158 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2159 | ansec = st->st_atimespec.tv_nsec; |
| 2160 | mnsec = st->st_mtimespec.tv_nsec; |
| 2161 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2162 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2163 | ansec = st->st_atime_nsec; |
| 2164 | mnsec = st->st_mtime_nsec; |
| 2165 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2166 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2167 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2168 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2169 | fill_time(v, 7, st->st_atime, ansec); |
| 2170 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2171 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2172 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2173 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2174 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2175 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2176 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2177 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2178 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2179 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2180 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2181 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2182 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2183 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2184 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2185 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2186 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2187 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2188 | #endif |
| 2189 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2190 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2191 | PyObject *val; |
| 2192 | unsigned long bsec,bnsec; |
| 2193 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2194 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2195 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2196 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2197 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2198 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2199 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2200 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2201 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2202 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2203 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2204 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2205 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2206 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2207 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2208 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2209 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2210 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2211 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2212 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2213 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2214 | PyUnicode_FromString(st->st_fstype)); |
| 2215 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2216 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2217 | PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX, |
| 2218 | PyLong_FromUnsignedLong(st->st_reparse_tag)); |
| 2219 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2220 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2221 | if (PyErr_Occurred()) { |
| 2222 | Py_DECREF(v); |
| 2223 | return NULL; |
| 2224 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2225 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2226 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2229 | /* POSIX methods */ |
| 2230 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2231 | |
| 2232 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2233 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2234 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2235 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2236 | STRUCT_STAT st; |
| 2237 | int result; |
| 2238 | |
| 2239 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2240 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2241 | return NULL; |
| 2242 | #endif |
| 2243 | |
| 2244 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2245 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2246 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2247 | return NULL; |
| 2248 | |
| 2249 | Py_BEGIN_ALLOW_THREADS |
| 2250 | if (path->fd != -1) |
| 2251 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2252 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2253 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2254 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2255 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2256 | result = win32_lstat(path->wide, &st); |
| 2257 | #else |
| 2258 | else |
| 2259 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2260 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2261 | result = LSTAT(path->narrow, &st); |
| 2262 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2263 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2264 | #ifdef HAVE_FSTATAT |
| 2265 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2266 | result = fstatat(dir_fd, path->narrow, &st, |
| 2267 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2268 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2269 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2270 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2271 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2272 | Py_END_ALLOW_THREADS |
| 2273 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2274 | if (result != 0) { |
| 2275 | return path_error(path); |
| 2276 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2277 | |
| 2278 | return _pystat_fromstructstat(&st); |
| 2279 | } |
| 2280 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2281 | /*[python input] |
| 2282 | |
| 2283 | for s in """ |
| 2284 | |
| 2285 | FACCESSAT |
| 2286 | FCHMODAT |
| 2287 | FCHOWNAT |
| 2288 | FSTATAT |
| 2289 | LINKAT |
| 2290 | MKDIRAT |
| 2291 | MKFIFOAT |
| 2292 | MKNODAT |
| 2293 | OPENAT |
| 2294 | READLINKAT |
| 2295 | SYMLINKAT |
| 2296 | UNLINKAT |
| 2297 | |
| 2298 | """.strip().split(): |
| 2299 | s = s.strip() |
| 2300 | print(""" |
| 2301 | #ifdef HAVE_{s} |
| 2302 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2303 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2304 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2305 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2306 | """.rstrip().format(s=s)) |
| 2307 | |
| 2308 | for s in """ |
| 2309 | |
| 2310 | FCHDIR |
| 2311 | FCHMOD |
| 2312 | FCHOWN |
| 2313 | FDOPENDIR |
| 2314 | FEXECVE |
| 2315 | FPATHCONF |
| 2316 | FSTATVFS |
| 2317 | FTRUNCATE |
| 2318 | |
| 2319 | """.strip().split(): |
| 2320 | s = s.strip() |
| 2321 | print(""" |
| 2322 | #ifdef HAVE_{s} |
| 2323 | #define PATH_HAVE_{s} 1 |
| 2324 | #else |
| 2325 | #define PATH_HAVE_{s} 0 |
| 2326 | #endif |
| 2327 | |
| 2328 | """.rstrip().format(s=s)) |
| 2329 | [python start generated code]*/ |
| 2330 | |
| 2331 | #ifdef HAVE_FACCESSAT |
| 2332 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2333 | #else |
| 2334 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2335 | #endif |
| 2336 | |
| 2337 | #ifdef HAVE_FCHMODAT |
| 2338 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2339 | #else |
| 2340 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2341 | #endif |
| 2342 | |
| 2343 | #ifdef HAVE_FCHOWNAT |
| 2344 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2345 | #else |
| 2346 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2347 | #endif |
| 2348 | |
| 2349 | #ifdef HAVE_FSTATAT |
| 2350 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2351 | #else |
| 2352 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2353 | #endif |
| 2354 | |
| 2355 | #ifdef HAVE_LINKAT |
| 2356 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2357 | #else |
| 2358 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2359 | #endif |
| 2360 | |
| 2361 | #ifdef HAVE_MKDIRAT |
| 2362 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2363 | #else |
| 2364 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2365 | #endif |
| 2366 | |
| 2367 | #ifdef HAVE_MKFIFOAT |
| 2368 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2369 | #else |
| 2370 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2371 | #endif |
| 2372 | |
| 2373 | #ifdef HAVE_MKNODAT |
| 2374 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2375 | #else |
| 2376 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2377 | #endif |
| 2378 | |
| 2379 | #ifdef HAVE_OPENAT |
| 2380 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2381 | #else |
| 2382 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2383 | #endif |
| 2384 | |
| 2385 | #ifdef HAVE_READLINKAT |
| 2386 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2387 | #else |
| 2388 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2389 | #endif |
| 2390 | |
| 2391 | #ifdef HAVE_SYMLINKAT |
| 2392 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2393 | #else |
| 2394 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2395 | #endif |
| 2396 | |
| 2397 | #ifdef HAVE_UNLINKAT |
| 2398 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2399 | #else |
| 2400 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2401 | #endif |
| 2402 | |
| 2403 | #ifdef HAVE_FCHDIR |
| 2404 | #define PATH_HAVE_FCHDIR 1 |
| 2405 | #else |
| 2406 | #define PATH_HAVE_FCHDIR 0 |
| 2407 | #endif |
| 2408 | |
| 2409 | #ifdef HAVE_FCHMOD |
| 2410 | #define PATH_HAVE_FCHMOD 1 |
| 2411 | #else |
| 2412 | #define PATH_HAVE_FCHMOD 0 |
| 2413 | #endif |
| 2414 | |
| 2415 | #ifdef HAVE_FCHOWN |
| 2416 | #define PATH_HAVE_FCHOWN 1 |
| 2417 | #else |
| 2418 | #define PATH_HAVE_FCHOWN 0 |
| 2419 | #endif |
| 2420 | |
| 2421 | #ifdef HAVE_FDOPENDIR |
| 2422 | #define PATH_HAVE_FDOPENDIR 1 |
| 2423 | #else |
| 2424 | #define PATH_HAVE_FDOPENDIR 0 |
| 2425 | #endif |
| 2426 | |
| 2427 | #ifdef HAVE_FEXECVE |
| 2428 | #define PATH_HAVE_FEXECVE 1 |
| 2429 | #else |
| 2430 | #define PATH_HAVE_FEXECVE 0 |
| 2431 | #endif |
| 2432 | |
| 2433 | #ifdef HAVE_FPATHCONF |
| 2434 | #define PATH_HAVE_FPATHCONF 1 |
| 2435 | #else |
| 2436 | #define PATH_HAVE_FPATHCONF 0 |
| 2437 | #endif |
| 2438 | |
| 2439 | #ifdef HAVE_FSTATVFS |
| 2440 | #define PATH_HAVE_FSTATVFS 1 |
| 2441 | #else |
| 2442 | #define PATH_HAVE_FSTATVFS 0 |
| 2443 | #endif |
| 2444 | |
| 2445 | #ifdef HAVE_FTRUNCATE |
| 2446 | #define PATH_HAVE_FTRUNCATE 1 |
| 2447 | #else |
| 2448 | #define PATH_HAVE_FTRUNCATE 0 |
| 2449 | #endif |
| 2450 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2451 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2452 | #ifdef MS_WINDOWS |
| 2453 | #undef PATH_HAVE_FTRUNCATE |
| 2454 | #define PATH_HAVE_FTRUNCATE 1 |
| 2455 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2456 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2457 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2458 | |
| 2459 | class path_t_converter(CConverter): |
| 2460 | |
| 2461 | type = "path_t" |
| 2462 | impl_by_reference = True |
| 2463 | parse_by_reference = True |
| 2464 | |
| 2465 | converter = 'path_converter' |
| 2466 | |
| 2467 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2468 | # right now path_t doesn't support default values. |
| 2469 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2470 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2471 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2472 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2473 | if self.c_default not in (None, 'Py_None'): |
| 2474 | 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] | 2475 | |
| 2476 | self.nullable = nullable |
| 2477 | self.allow_fd = allow_fd |
| 2478 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2479 | def pre_render(self): |
| 2480 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2481 | if isinstance(value, str): |
| 2482 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2483 | return str(int(bool(value))) |
| 2484 | |
| 2485 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2486 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2487 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2488 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2489 | strify(self.nullable), |
| 2490 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2491 | ) |
| 2492 | |
| 2493 | def cleanup(self): |
| 2494 | return "path_cleanup(&" + self.name + ");\n" |
| 2495 | |
| 2496 | |
| 2497 | class dir_fd_converter(CConverter): |
| 2498 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2499 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2500 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2501 | if self.default in (unspecified, None): |
| 2502 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2503 | if isinstance(requires, str): |
| 2504 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2505 | else: |
| 2506 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2507 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2508 | class fildes_converter(CConverter): |
| 2509 | type = 'int' |
| 2510 | converter = 'fildes_converter' |
| 2511 | |
| 2512 | class uid_t_converter(CConverter): |
| 2513 | type = "uid_t" |
| 2514 | converter = '_Py_Uid_Converter' |
| 2515 | |
| 2516 | class gid_t_converter(CConverter): |
| 2517 | type = "gid_t" |
| 2518 | converter = '_Py_Gid_Converter' |
| 2519 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2520 | class dev_t_converter(CConverter): |
| 2521 | type = 'dev_t' |
| 2522 | converter = '_Py_Dev_Converter' |
| 2523 | |
| 2524 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2525 | type = 'dev_t' |
| 2526 | conversion_fn = '_PyLong_FromDev' |
| 2527 | unsigned_cast = '(dev_t)' |
| 2528 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2529 | class FSConverter_converter(CConverter): |
| 2530 | type = 'PyObject *' |
| 2531 | converter = 'PyUnicode_FSConverter' |
| 2532 | def converter_init(self): |
| 2533 | if self.default is not unspecified: |
| 2534 | fail("FSConverter_converter does not support default values") |
| 2535 | self.c_default = 'NULL' |
| 2536 | |
| 2537 | def cleanup(self): |
| 2538 | return "Py_XDECREF(" + self.name + ");\n" |
| 2539 | |
| 2540 | class pid_t_converter(CConverter): |
| 2541 | type = 'pid_t' |
| 2542 | format_unit = '" _Py_PARSE_PID "' |
| 2543 | |
| 2544 | class idtype_t_converter(int_converter): |
| 2545 | type = 'idtype_t' |
| 2546 | |
| 2547 | class id_t_converter(CConverter): |
| 2548 | type = 'id_t' |
| 2549 | format_unit = '" _Py_PARSE_PID "' |
| 2550 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2551 | class intptr_t_converter(CConverter): |
| 2552 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2553 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2554 | |
| 2555 | class Py_off_t_converter(CConverter): |
| 2556 | type = 'Py_off_t' |
| 2557 | converter = 'Py_off_t_converter' |
| 2558 | |
| 2559 | class Py_off_t_return_converter(long_return_converter): |
| 2560 | type = 'Py_off_t' |
| 2561 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2562 | |
| 2563 | class path_confname_converter(CConverter): |
| 2564 | type="int" |
| 2565 | converter="conv_path_confname" |
| 2566 | |
| 2567 | class confstr_confname_converter(path_confname_converter): |
| 2568 | converter='conv_confstr_confname' |
| 2569 | |
| 2570 | class sysconf_confname_converter(path_confname_converter): |
| 2571 | converter="conv_sysconf_confname" |
| 2572 | |
| 2573 | class sched_param_converter(CConverter): |
| 2574 | type = 'struct sched_param' |
| 2575 | converter = 'convert_sched_param' |
| 2576 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2577 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2578 | [python start generated code]*/ |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 2579 | /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2580 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2581 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2582 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2583 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2584 | |
| 2585 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2586 | 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] | 2587 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2588 | |
| 2589 | * |
| 2590 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2591 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2592 | If not None, it should be a file descriptor open to a directory, |
| 2593 | and path should be a relative string; path will then be relative to |
| 2594 | that directory. |
| 2595 | |
| 2596 | follow_symlinks: bool = True |
| 2597 | If False, and the last element of the path is a symbolic link, |
| 2598 | stat will examine the symbolic link itself instead of the file |
| 2599 | the link points to. |
| 2600 | |
| 2601 | Perform a stat system call on the given path. |
| 2602 | |
| 2603 | dir_fd and follow_symlinks may not be implemented |
| 2604 | on your platform. If they are unavailable, using them will raise a |
| 2605 | NotImplementedError. |
| 2606 | |
| 2607 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2608 | an open file descriptor. |
| 2609 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2610 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2611 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2612 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2613 | 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] | 2614 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2615 | { |
| 2616 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2617 | } |
| 2618 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2619 | |
| 2620 | /*[clinic input] |
| 2621 | os.lstat |
| 2622 | |
| 2623 | path : path_t |
| 2624 | |
| 2625 | * |
| 2626 | |
| 2627 | dir_fd : dir_fd(requires='fstatat') = None |
| 2628 | |
| 2629 | Perform a stat system call on the given path, without following symbolic links. |
| 2630 | |
| 2631 | Like stat(), but do not follow symbolic links. |
| 2632 | Equivalent to stat(path, follow_symlinks=False). |
| 2633 | [clinic start generated code]*/ |
| 2634 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2635 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2636 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2637 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2638 | { |
| 2639 | int follow_symlinks = 0; |
| 2640 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2641 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2642 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2643 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2644 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2645 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2646 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2647 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2648 | 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] | 2649 | |
| 2650 | mode: int |
| 2651 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2652 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2653 | |
| 2654 | * |
| 2655 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2656 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2657 | If not None, it should be a file descriptor open to a directory, |
| 2658 | and path should be relative; path will then be relative to that |
| 2659 | directory. |
| 2660 | |
| 2661 | effective_ids: bool = False |
| 2662 | If True, access will use the effective uid/gid instead of |
| 2663 | the real uid/gid. |
| 2664 | |
| 2665 | follow_symlinks: bool = True |
| 2666 | If False, and the last element of the path is a symbolic link, |
| 2667 | access will examine the symbolic link itself instead of the file |
| 2668 | the link points to. |
| 2669 | |
| 2670 | Use the real uid/gid to test for access to a path. |
| 2671 | |
| 2672 | {parameters} |
| 2673 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2674 | on your platform. If they are unavailable, using them will raise a |
| 2675 | NotImplementedError. |
| 2676 | |
| 2677 | Note that most operations will use the effective uid/gid, therefore this |
| 2678 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2679 | has the specified access to the path. |
| 2680 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2681 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2682 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2683 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2684 | 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] | 2685 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2686 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2687 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2688 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2689 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2690 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2691 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2692 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2693 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2694 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2695 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2696 | #ifndef HAVE_FACCESSAT |
| 2697 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2698 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2699 | |
| 2700 | if (effective_ids) { |
| 2701 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2702 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2703 | } |
| 2704 | #endif |
| 2705 | |
| 2706 | #ifdef MS_WINDOWS |
| 2707 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2708 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2709 | Py_END_ALLOW_THREADS |
| 2710 | |
| 2711 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2712 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2713 | * * we didn't get a -1, and |
| 2714 | * * write access wasn't requested, |
| 2715 | * * or the file isn't read-only, |
| 2716 | * * or it's a directory. |
| 2717 | * (Directories cannot be read-only on Windows.) |
| 2718 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2719 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2720 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2721 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2722 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2723 | #else |
| 2724 | |
| 2725 | Py_BEGIN_ALLOW_THREADS |
| 2726 | #ifdef HAVE_FACCESSAT |
| 2727 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2728 | effective_ids || |
| 2729 | !follow_symlinks) { |
| 2730 | int flags = 0; |
| 2731 | if (!follow_symlinks) |
| 2732 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2733 | if (effective_ids) |
| 2734 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2735 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2736 | } |
| 2737 | else |
| 2738 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2739 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2740 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2741 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2742 | #endif |
| 2743 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2744 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2747 | #ifndef F_OK |
| 2748 | #define F_OK 0 |
| 2749 | #endif |
| 2750 | #ifndef R_OK |
| 2751 | #define R_OK 4 |
| 2752 | #endif |
| 2753 | #ifndef W_OK |
| 2754 | #define W_OK 2 |
| 2755 | #endif |
| 2756 | #ifndef X_OK |
| 2757 | #define X_OK 1 |
| 2758 | #endif |
| 2759 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2760 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2761 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2762 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2763 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2764 | |
| 2765 | fd: int |
| 2766 | Integer file descriptor handle. |
| 2767 | |
| 2768 | / |
| 2769 | |
| 2770 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2771 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2772 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2773 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2774 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2775 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2776 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2777 | |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2778 | long size = sysconf(_SC_TTY_NAME_MAX); |
| 2779 | if (size == -1) { |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2780 | return posix_error(); |
| 2781 | } |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2782 | char *buffer = (char *)PyMem_RawMalloc(size); |
| 2783 | if (buffer == NULL) { |
| 2784 | return PyErr_NoMemory(); |
| 2785 | } |
| 2786 | int ret = ttyname_r(fd, buffer, size); |
| 2787 | if (ret != 0) { |
| 2788 | PyMem_RawFree(buffer); |
| 2789 | errno = ret; |
| 2790 | return posix_error(); |
| 2791 | } |
| 2792 | PyObject *res = PyUnicode_DecodeFSDefault(buffer); |
| 2793 | PyMem_RawFree(buffer); |
| 2794 | return res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2795 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2796 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2797 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2798 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2799 | /*[clinic input] |
| 2800 | os.ctermid |
| 2801 | |
| 2802 | Return the name of the controlling terminal for this process. |
| 2803 | [clinic start generated code]*/ |
| 2804 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2805 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2806 | os_ctermid_impl(PyObject *module) |
| 2807 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2808 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2809 | char *ret; |
| 2810 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2811 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2812 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2813 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2814 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2815 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2816 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2817 | if (ret == NULL) |
| 2818 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2819 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2820 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2821 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2822 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2823 | |
| 2824 | /*[clinic input] |
| 2825 | os.chdir |
| 2826 | |
| 2827 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2828 | |
| 2829 | Change the current working directory to the specified path. |
| 2830 | |
| 2831 | path may always be specified as a string. |
| 2832 | On some platforms, path may also be specified as an open file descriptor. |
| 2833 | If this functionality is unavailable, using it raises an exception. |
| 2834 | [clinic start generated code]*/ |
| 2835 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2836 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2837 | os_chdir_impl(PyObject *module, path_t *path) |
| 2838 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2839 | { |
| 2840 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2841 | |
| 2842 | Py_BEGIN_ALLOW_THREADS |
| 2843 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2844 | /* on unix, success = 0, on windows, success = !0 */ |
| 2845 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2846 | #else |
| 2847 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2848 | if (path->fd != -1) |
| 2849 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2850 | else |
| 2851 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2852 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2853 | #endif |
| 2854 | Py_END_ALLOW_THREADS |
| 2855 | |
| 2856 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2857 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2858 | } |
| 2859 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2860 | Py_RETURN_NONE; |
| 2861 | } |
| 2862 | |
| 2863 | |
| 2864 | #ifdef HAVE_FCHDIR |
| 2865 | /*[clinic input] |
| 2866 | os.fchdir |
| 2867 | |
| 2868 | fd: fildes |
| 2869 | |
| 2870 | Change to the directory of the given file descriptor. |
| 2871 | |
| 2872 | fd must be opened on a directory, not a file. |
| 2873 | Equivalent to os.chdir(fd). |
| 2874 | |
| 2875 | [clinic start generated code]*/ |
| 2876 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2877 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2878 | os_fchdir_impl(PyObject *module, int fd) |
| 2879 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2880 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2881 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2882 | } |
| 2883 | #endif /* HAVE_FCHDIR */ |
| 2884 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2885 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2886 | /*[clinic input] |
| 2887 | os.chmod |
| 2888 | |
| 2889 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2890 | 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] | 2891 | On some platforms, path may also be specified as an open file descriptor. |
| 2892 | If this functionality is unavailable, using it raises an exception. |
| 2893 | |
| 2894 | mode: int |
| 2895 | Operating-system mode bitfield. |
| 2896 | |
| 2897 | * |
| 2898 | |
| 2899 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2900 | If not None, it should be a file descriptor open to a directory, |
| 2901 | and path should be relative; path will then be relative to that |
| 2902 | directory. |
| 2903 | |
| 2904 | follow_symlinks: bool = True |
| 2905 | If False, and the last element of the path is a symbolic link, |
| 2906 | chmod will modify the symbolic link itself instead of the file |
| 2907 | the link points to. |
| 2908 | |
| 2909 | Change the access permissions of a file. |
| 2910 | |
| 2911 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2912 | an open file descriptor. |
| 2913 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2914 | If they are unavailable, using them will raise a NotImplementedError. |
| 2915 | |
| 2916 | [clinic start generated code]*/ |
| 2917 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2918 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2919 | 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] | 2920 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2921 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2922 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2923 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2924 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2925 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2926 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2927 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2928 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2929 | #ifdef HAVE_FCHMODAT |
| 2930 | int fchmodat_nofollow_unsupported = 0; |
| 2931 | #endif |
| 2932 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2933 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2934 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2935 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2936 | #endif |
| 2937 | |
| 2938 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2939 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2940 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2941 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2942 | result = 0; |
| 2943 | else { |
| 2944 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2945 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2946 | else |
| 2947 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2948 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2949 | } |
| 2950 | Py_END_ALLOW_THREADS |
| 2951 | |
| 2952 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2953 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2954 | } |
| 2955 | #else /* MS_WINDOWS */ |
| 2956 | Py_BEGIN_ALLOW_THREADS |
| 2957 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2958 | if (path->fd != -1) |
| 2959 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2960 | else |
| 2961 | #endif |
| 2962 | #ifdef HAVE_LCHMOD |
| 2963 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2964 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2965 | else |
| 2966 | #endif |
| 2967 | #ifdef HAVE_FCHMODAT |
| 2968 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2969 | /* |
| 2970 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2971 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2972 | * and then says it isn't implemented yet. |
| 2973 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2974 | * |
| 2975 | * Once it is supported, os.chmod will automatically |
| 2976 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2977 | * Until then, we need to be careful what exception we raise. |
| 2978 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2979 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2980 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2981 | /* |
| 2982 | * But wait! We can't throw the exception without allowing threads, |
| 2983 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2984 | */ |
| 2985 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2986 | result && |
| 2987 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2988 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2989 | } |
| 2990 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2991 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2992 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2993 | Py_END_ALLOW_THREADS |
| 2994 | |
| 2995 | if (result) { |
| 2996 | #ifdef HAVE_FCHMODAT |
| 2997 | if (fchmodat_nofollow_unsupported) { |
| 2998 | if (dir_fd != DEFAULT_DIR_FD) |
| 2999 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 3000 | dir_fd, follow_symlinks); |
| 3001 | else |
| 3002 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 3003 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3004 | } |
| 3005 | else |
| 3006 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3007 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3008 | } |
| 3009 | #endif |
| 3010 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3011 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3014 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3015 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3016 | /*[clinic input] |
| 3017 | os.fchmod |
| 3018 | |
| 3019 | fd: int |
| 3020 | mode: int |
| 3021 | |
| 3022 | Change the access permissions of the file given by file descriptor fd. |
| 3023 | |
| 3024 | Equivalent to os.chmod(fd, mode). |
| 3025 | [clinic start generated code]*/ |
| 3026 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3027 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3028 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 3029 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3030 | { |
| 3031 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3032 | int async_err = 0; |
| 3033 | |
| 3034 | do { |
| 3035 | Py_BEGIN_ALLOW_THREADS |
| 3036 | res = fchmod(fd, mode); |
| 3037 | Py_END_ALLOW_THREADS |
| 3038 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3039 | if (res != 0) |
| 3040 | return (!async_err) ? posix_error() : NULL; |
| 3041 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3042 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3043 | } |
| 3044 | #endif /* HAVE_FCHMOD */ |
| 3045 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3046 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3047 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3048 | /*[clinic input] |
| 3049 | os.lchmod |
| 3050 | |
| 3051 | path: path_t |
| 3052 | mode: int |
| 3053 | |
| 3054 | Change the access permissions of a file, without following symbolic links. |
| 3055 | |
| 3056 | If path is a symlink, this affects the link itself rather than the target. |
| 3057 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 3058 | [clinic start generated code]*/ |
| 3059 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3060 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3061 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 3062 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3063 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3064 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3065 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3066 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3067 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3068 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3069 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3070 | return NULL; |
| 3071 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3072 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3073 | } |
| 3074 | #endif /* HAVE_LCHMOD */ |
| 3075 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3076 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3077 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3078 | /*[clinic input] |
| 3079 | os.chflags |
| 3080 | |
| 3081 | path: path_t |
| 3082 | flags: unsigned_long(bitwise=True) |
| 3083 | follow_symlinks: bool=True |
| 3084 | |
| 3085 | Set file flags. |
| 3086 | |
| 3087 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3088 | link, chflags will change flags on the symbolic link itself instead of the |
| 3089 | file the link points to. |
| 3090 | follow_symlinks may not be implemented on your platform. If it is |
| 3091 | unavailable, using it will raise a NotImplementedError. |
| 3092 | |
| 3093 | [clinic start generated code]*/ |
| 3094 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3095 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3096 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3097 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3098 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3099 | { |
| 3100 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3101 | |
| 3102 | #ifndef HAVE_LCHFLAGS |
| 3103 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3104 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3105 | #endif |
| 3106 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3107 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3108 | #ifdef HAVE_LCHFLAGS |
| 3109 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3110 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3111 | else |
| 3112 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3113 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3114 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3115 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3116 | if (result) |
| 3117 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3118 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3119 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3120 | } |
| 3121 | #endif /* HAVE_CHFLAGS */ |
| 3122 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3123 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3124 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3125 | /*[clinic input] |
| 3126 | os.lchflags |
| 3127 | |
| 3128 | path: path_t |
| 3129 | flags: unsigned_long(bitwise=True) |
| 3130 | |
| 3131 | Set file flags. |
| 3132 | |
| 3133 | This function will not follow symbolic links. |
| 3134 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3135 | [clinic start generated code]*/ |
| 3136 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3137 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3138 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3139 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3140 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3141 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3142 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3143 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3144 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3145 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3146 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3147 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3148 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3149 | } |
| 3150 | #endif /* HAVE_LCHFLAGS */ |
| 3151 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3152 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3153 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3154 | /*[clinic input] |
| 3155 | os.chroot |
| 3156 | path: path_t |
| 3157 | |
| 3158 | Change root directory to path. |
| 3159 | |
| 3160 | [clinic start generated code]*/ |
| 3161 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3162 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3163 | os_chroot_impl(PyObject *module, path_t *path) |
| 3164 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3165 | { |
| 3166 | int res; |
| 3167 | Py_BEGIN_ALLOW_THREADS |
| 3168 | res = chroot(path->narrow); |
| 3169 | Py_END_ALLOW_THREADS |
| 3170 | if (res < 0) |
| 3171 | return path_error(path); |
| 3172 | Py_RETURN_NONE; |
| 3173 | } |
| 3174 | #endif /* HAVE_CHROOT */ |
| 3175 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3176 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3177 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3178 | /*[clinic input] |
| 3179 | os.fsync |
| 3180 | |
| 3181 | fd: fildes |
| 3182 | |
| 3183 | Force write of fd to disk. |
| 3184 | [clinic start generated code]*/ |
| 3185 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3186 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3187 | os_fsync_impl(PyObject *module, int fd) |
| 3188 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3189 | { |
| 3190 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3191 | } |
| 3192 | #endif /* HAVE_FSYNC */ |
| 3193 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3194 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3195 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3196 | /*[clinic input] |
| 3197 | os.sync |
| 3198 | |
| 3199 | Force write of everything to disk. |
| 3200 | [clinic start generated code]*/ |
| 3201 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3202 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3203 | os_sync_impl(PyObject *module) |
| 3204 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3205 | { |
| 3206 | Py_BEGIN_ALLOW_THREADS |
| 3207 | sync(); |
| 3208 | Py_END_ALLOW_THREADS |
| 3209 | Py_RETURN_NONE; |
| 3210 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3211 | #endif /* HAVE_SYNC */ |
| 3212 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3213 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3214 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3215 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3216 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3217 | #endif |
| 3218 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3219 | /*[clinic input] |
| 3220 | os.fdatasync |
| 3221 | |
| 3222 | fd: fildes |
| 3223 | |
| 3224 | Force write of fd to disk without forcing update of metadata. |
| 3225 | [clinic start generated code]*/ |
| 3226 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3227 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3228 | os_fdatasync_impl(PyObject *module, int fd) |
| 3229 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3230 | { |
| 3231 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3232 | } |
| 3233 | #endif /* HAVE_FDATASYNC */ |
| 3234 | |
| 3235 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3236 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3237 | /*[clinic input] |
| 3238 | os.chown |
| 3239 | |
| 3240 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3241 | 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] | 3242 | |
| 3243 | uid: uid_t |
| 3244 | |
| 3245 | gid: gid_t |
| 3246 | |
| 3247 | * |
| 3248 | |
| 3249 | dir_fd : dir_fd(requires='fchownat') = None |
| 3250 | If not None, it should be a file descriptor open to a directory, |
| 3251 | and path should be relative; path will then be relative to that |
| 3252 | directory. |
| 3253 | |
| 3254 | follow_symlinks: bool = True |
| 3255 | If False, and the last element of the path is a symbolic link, |
| 3256 | stat will examine the symbolic link itself instead of the file |
| 3257 | the link points to. |
| 3258 | |
| 3259 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3260 | |
| 3261 | path may always be specified as a string. |
| 3262 | On some platforms, path may also be specified as an open file descriptor. |
| 3263 | If this functionality is unavailable, using it raises an exception. |
| 3264 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3265 | and path should be relative; path will then be relative to that directory. |
| 3266 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3267 | link, chown will modify the symbolic link itself instead of the file the |
| 3268 | link points to. |
| 3269 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3270 | an open file descriptor. |
| 3271 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3272 | If they are unavailable, using them will raise a NotImplementedError. |
| 3273 | |
| 3274 | [clinic start generated code]*/ |
| 3275 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3276 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3277 | 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] | 3278 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3279 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3280 | { |
| 3281 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3282 | |
| 3283 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3284 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3285 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3286 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3287 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3288 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3289 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3290 | |
| 3291 | #ifdef __APPLE__ |
| 3292 | /* |
| 3293 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3294 | * (But we still have an lchown symbol because of weak-linking.) |
| 3295 | * It doesn't have fchownat either. So there's no possibility |
| 3296 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3297 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3298 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3299 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3300 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3301 | } |
| 3302 | #endif |
| 3303 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3304 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3305 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3306 | if (path->fd != -1) |
| 3307 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3308 | else |
| 3309 | #endif |
| 3310 | #ifdef HAVE_LCHOWN |
| 3311 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3312 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3313 | else |
| 3314 | #endif |
| 3315 | #ifdef HAVE_FCHOWNAT |
| 3316 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3317 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3318 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3319 | else |
| 3320 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3321 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3322 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3323 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3324 | if (result) |
| 3325 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3326 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3327 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3328 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3329 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3330 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3331 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3332 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3333 | /*[clinic input] |
| 3334 | os.fchown |
| 3335 | |
| 3336 | fd: int |
| 3337 | uid: uid_t |
| 3338 | gid: gid_t |
| 3339 | |
| 3340 | Change the owner and group id of the file specified by file descriptor. |
| 3341 | |
| 3342 | Equivalent to os.chown(fd, uid, gid). |
| 3343 | |
| 3344 | [clinic start generated code]*/ |
| 3345 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3346 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3347 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3348 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3349 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3350 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3351 | int async_err = 0; |
| 3352 | |
| 3353 | do { |
| 3354 | Py_BEGIN_ALLOW_THREADS |
| 3355 | res = fchown(fd, uid, gid); |
| 3356 | Py_END_ALLOW_THREADS |
| 3357 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3358 | if (res != 0) |
| 3359 | return (!async_err) ? posix_error() : NULL; |
| 3360 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3361 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3362 | } |
| 3363 | #endif /* HAVE_FCHOWN */ |
| 3364 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3365 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3366 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3367 | /*[clinic input] |
| 3368 | os.lchown |
| 3369 | |
| 3370 | path : path_t |
| 3371 | uid: uid_t |
| 3372 | gid: gid_t |
| 3373 | |
| 3374 | Change the owner and group id of path to the numeric uid and gid. |
| 3375 | |
| 3376 | This function will not follow symbolic links. |
| 3377 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3378 | [clinic start generated code]*/ |
| 3379 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3380 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3381 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3382 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3383 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3384 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3385 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3386 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3387 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3388 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3389 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3390 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3391 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3392 | } |
| 3393 | #endif /* HAVE_LCHOWN */ |
| 3394 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3395 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3396 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3397 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3398 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3399 | #ifdef MS_WINDOWS |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3400 | wchar_t wbuf[MAXPATHLEN]; |
| 3401 | wchar_t *wbuf2 = wbuf; |
| 3402 | DWORD len; |
| 3403 | |
| 3404 | Py_BEGIN_ALLOW_THREADS |
| 3405 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
| 3406 | /* If the buffer is large enough, len does not include the |
| 3407 | terminating \0. If the buffer is too small, len includes |
| 3408 | the space needed for the terminator. */ |
| 3409 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | ec3e20a | 2019-06-28 18:01:59 +0200 | [diff] [blame] | 3410 | if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3411 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3412 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3413 | else { |
| 3414 | wbuf2 = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3415 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3416 | if (wbuf2) { |
| 3417 | len = GetCurrentDirectoryW(len, wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3418 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3419 | } |
| 3420 | Py_END_ALLOW_THREADS |
| 3421 | |
| 3422 | if (!wbuf2) { |
| 3423 | PyErr_NoMemory(); |
| 3424 | return NULL; |
| 3425 | } |
| 3426 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3427 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3428 | PyMem_RawFree(wbuf2); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3429 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3430 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3431 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3432 | PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 3433 | if (wbuf2 != wbuf) { |
| 3434 | PyMem_RawFree(wbuf2); |
| 3435 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3436 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3437 | if (use_bytes) { |
| 3438 | if (resobj == NULL) { |
| 3439 | return NULL; |
| 3440 | } |
| 3441 | Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj)); |
| 3442 | } |
| 3443 | |
| 3444 | return resobj; |
| 3445 | #else |
| 3446 | const size_t chunk = 1024; |
| 3447 | |
| 3448 | char *buf = NULL; |
| 3449 | char *cwd = NULL; |
| 3450 | size_t buflen = 0; |
| 3451 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3452 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3453 | do { |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3454 | char *newbuf; |
| 3455 | if (buflen <= PY_SSIZE_T_MAX - chunk) { |
| 3456 | buflen += chunk; |
| 3457 | newbuf = PyMem_RawRealloc(buf, buflen); |
| 3458 | } |
| 3459 | else { |
| 3460 | newbuf = NULL; |
| 3461 | } |
| 3462 | if (newbuf == NULL) { |
| 3463 | PyMem_RawFree(buf); |
| 3464 | buf = NULL; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3465 | break; |
| 3466 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3467 | buf = newbuf; |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3468 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3469 | cwd = getcwd(buf, buflen); |
| 3470 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3471 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3472 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3473 | if (buf == NULL) { |
| 3474 | return PyErr_NoMemory(); |
| 3475 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3476 | if (cwd == NULL) { |
| 3477 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3478 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3479 | } |
| 3480 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3481 | PyObject *obj; |
| 3482 | if (use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3483 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3484 | } |
| 3485 | else { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3486 | obj = PyUnicode_DecodeFSDefault(buf); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3487 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3488 | PyMem_RawFree(buf); |
| 3489 | |
| 3490 | return obj; |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3491 | #endif /* !MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3492 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3493 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3494 | |
| 3495 | /*[clinic input] |
| 3496 | os.getcwd |
| 3497 | |
| 3498 | Return a unicode string representing the current working directory. |
| 3499 | [clinic start generated code]*/ |
| 3500 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3501 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3502 | os_getcwd_impl(PyObject *module) |
| 3503 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3504 | { |
| 3505 | return posix_getcwd(0); |
| 3506 | } |
| 3507 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3508 | |
| 3509 | /*[clinic input] |
| 3510 | os.getcwdb |
| 3511 | |
| 3512 | Return a bytes string representing the current working directory. |
| 3513 | [clinic start generated code]*/ |
| 3514 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3515 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3516 | os_getcwdb_impl(PyObject *module) |
| 3517 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3518 | { |
| 3519 | return posix_getcwd(1); |
| 3520 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3521 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3522 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3523 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3524 | #define HAVE_LINK 1 |
| 3525 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3526 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3527 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3528 | /*[clinic input] |
| 3529 | |
| 3530 | os.link |
| 3531 | |
| 3532 | src : path_t |
| 3533 | dst : path_t |
| 3534 | * |
| 3535 | src_dir_fd : dir_fd = None |
| 3536 | dst_dir_fd : dir_fd = None |
| 3537 | follow_symlinks: bool = True |
| 3538 | |
| 3539 | Create a hard link to a file. |
| 3540 | |
| 3541 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3542 | descriptor open to a directory, and the respective path string (src or dst) |
| 3543 | should be relative; the path will then be relative to that directory. |
| 3544 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3545 | link, link will create a link to the symbolic link itself instead of the |
| 3546 | file the link points to. |
| 3547 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3548 | platform. If they are unavailable, using them will raise a |
| 3549 | NotImplementedError. |
| 3550 | [clinic start generated code]*/ |
| 3551 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3552 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3553 | 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] | 3554 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3555 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3556 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3557 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3558 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3559 | #else |
| 3560 | int result; |
| 3561 | #endif |
| 3562 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3563 | #ifndef HAVE_LINKAT |
| 3564 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3565 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3566 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3567 | } |
| 3568 | #endif |
| 3569 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3570 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3571 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3572 | PyErr_SetString(PyExc_NotImplementedError, |
| 3573 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3574 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3575 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3576 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3577 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3578 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3579 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3580 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3581 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3582 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3583 | if (!result) |
| 3584 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3585 | #else |
| 3586 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3587 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3588 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3589 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3590 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3591 | result = linkat(src_dir_fd, src->narrow, |
| 3592 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3593 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3594 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3595 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3596 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3597 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3599 | if (result) |
| 3600 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3601 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3602 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3603 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3604 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3605 | #endif |
| 3606 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3607 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3608 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3609 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3610 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3611 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3612 | PyObject *v; |
| 3613 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3614 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3615 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3616 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3617 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3618 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3619 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3620 | WIN32_FIND_DATAW wFileData; |
| 3621 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3622 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3623 | if (!path->wide) { /* Default arg: "." */ |
| 3624 | po_wchars = L"."; |
| 3625 | len = 1; |
| 3626 | } else { |
| 3627 | po_wchars = path->wide; |
| 3628 | len = wcslen(path->wide); |
| 3629 | } |
| 3630 | /* The +5 is so we can append "\\*.*\0" */ |
| 3631 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3632 | if (!wnamebuf) { |
| 3633 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3634 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3635 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3636 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3637 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3638 | wchar_t wch = wnamebuf[len-1]; |
| 3639 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3640 | wnamebuf[len++] = SEP; |
| 3641 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3642 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3643 | if ((list = PyList_New(0)) == NULL) { |
| 3644 | goto exit; |
| 3645 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3646 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3647 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3648 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3649 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3650 | int error = GetLastError(); |
| 3651 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3652 | goto exit; |
| 3653 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3654 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3655 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3656 | } |
| 3657 | do { |
| 3658 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3659 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3660 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3661 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3662 | wcslen(wFileData.cFileName)); |
| 3663 | if (path->narrow && v) { |
| 3664 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3665 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3666 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3667 | Py_DECREF(list); |
| 3668 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3669 | break; |
| 3670 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3671 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3672 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3673 | Py_DECREF(list); |
| 3674 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3675 | break; |
| 3676 | } |
| 3677 | Py_DECREF(v); |
| 3678 | } |
| 3679 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3680 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3681 | Py_END_ALLOW_THREADS |
| 3682 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3683 | it got to the end of the directory. */ |
| 3684 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3685 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3686 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3687 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3688 | } |
| 3689 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3690 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3691 | exit: |
| 3692 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3693 | if (FindClose(hFindFile) == FALSE) { |
| 3694 | if (list != NULL) { |
| 3695 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3696 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3697 | } |
| 3698 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3699 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3700 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3701 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3702 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3703 | } /* end of _listdir_windows_no_opendir */ |
| 3704 | |
| 3705 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3706 | |
| 3707 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3708 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3709 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3710 | PyObject *v; |
| 3711 | DIR *dirp = NULL; |
| 3712 | struct dirent *ep; |
| 3713 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3714 | #ifdef HAVE_FDOPENDIR |
| 3715 | int fd = -1; |
| 3716 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3717 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3718 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3719 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3720 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3721 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3722 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3723 | if (fd == -1) |
| 3724 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3725 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3726 | return_str = 1; |
| 3727 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3728 | Py_BEGIN_ALLOW_THREADS |
| 3729 | dirp = fdopendir(fd); |
| 3730 | Py_END_ALLOW_THREADS |
| 3731 | } |
| 3732 | else |
| 3733 | #endif |
| 3734 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3735 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3736 | if (path->narrow) { |
| 3737 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3738 | /* only return bytes if they specified a bytes-like object */ |
| 3739 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3740 | } |
| 3741 | else { |
| 3742 | name = "."; |
| 3743 | return_str = 1; |
| 3744 | } |
| 3745 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3746 | Py_BEGIN_ALLOW_THREADS |
| 3747 | dirp = opendir(name); |
| 3748 | Py_END_ALLOW_THREADS |
| 3749 | } |
| 3750 | |
| 3751 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3752 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3753 | #ifdef HAVE_FDOPENDIR |
| 3754 | if (fd != -1) { |
| 3755 | Py_BEGIN_ALLOW_THREADS |
| 3756 | close(fd); |
| 3757 | Py_END_ALLOW_THREADS |
| 3758 | } |
| 3759 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3760 | goto exit; |
| 3761 | } |
| 3762 | if ((list = PyList_New(0)) == NULL) { |
| 3763 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3764 | } |
| 3765 | for (;;) { |
| 3766 | errno = 0; |
| 3767 | Py_BEGIN_ALLOW_THREADS |
| 3768 | ep = readdir(dirp); |
| 3769 | Py_END_ALLOW_THREADS |
| 3770 | if (ep == NULL) { |
| 3771 | if (errno == 0) { |
| 3772 | break; |
| 3773 | } else { |
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 | } |
| 3779 | if (ep->d_name[0] == '.' && |
| 3780 | (NAMLEN(ep) == 1 || |
| 3781 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3782 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3783 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3784 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3785 | else |
| 3786 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3787 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3788 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3789 | break; |
| 3790 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3791 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3792 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3793 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3794 | break; |
| 3795 | } |
| 3796 | Py_DECREF(v); |
| 3797 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3798 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3799 | exit: |
| 3800 | if (dirp != NULL) { |
| 3801 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3802 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3803 | if (fd > -1) |
| 3804 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3805 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3806 | closedir(dirp); |
| 3807 | Py_END_ALLOW_THREADS |
| 3808 | } |
| 3809 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3810 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3811 | } /* end of _posix_listdir */ |
| 3812 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3813 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3814 | |
| 3815 | /*[clinic input] |
| 3816 | os.listdir |
| 3817 | |
| 3818 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3819 | |
| 3820 | Return a list containing the names of the files in the directory. |
| 3821 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3822 | 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] | 3823 | the filenames returned will also be bytes; in all other circumstances |
| 3824 | the filenames returned will be str. |
| 3825 | If path is None, uses the path='.'. |
| 3826 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3827 | the file descriptor must refer to a directory. |
| 3828 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3829 | |
| 3830 | The list is in arbitrary order. It does not include the special |
| 3831 | entries '.' and '..' even if they are present in the directory. |
| 3832 | |
| 3833 | |
| 3834 | [clinic start generated code]*/ |
| 3835 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3836 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3837 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3838 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3839 | { |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 3840 | if (PySys_Audit("os.listdir", "O", |
| 3841 | path->object ? path->object : Py_None) < 0) { |
| 3842 | return NULL; |
| 3843 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3844 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3845 | return _listdir_windows_no_opendir(path, NULL); |
| 3846 | #else |
| 3847 | return _posix_listdir(path, NULL); |
| 3848 | #endif |
| 3849 | } |
| 3850 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3851 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3852 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3853 | /*[clinic input] |
| 3854 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3855 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3856 | path: path_t |
| 3857 | / |
| 3858 | |
| 3859 | [clinic start generated code]*/ |
| 3860 | |
| 3861 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3862 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 3863 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3864 | { |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3865 | wchar_t *abspath; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3866 | |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3867 | /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */ |
| 3868 | if (_Py_abspath(path->wide, &abspath) < 0) { |
| 3869 | return win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3870 | } |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3871 | if (abspath == NULL) { |
| 3872 | return PyErr_NoMemory(); |
| 3873 | } |
| 3874 | |
| 3875 | PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath)); |
| 3876 | PyMem_RawFree(abspath); |
| 3877 | if (str == NULL) { |
| 3878 | return NULL; |
| 3879 | } |
| 3880 | if (path->narrow) { |
| 3881 | Py_SETREF(str, PyUnicode_EncodeFSDefault(str)); |
| 3882 | } |
| 3883 | return str; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3884 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3885 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3886 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3887 | /*[clinic input] |
| 3888 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3889 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3890 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3891 | / |
| 3892 | |
| 3893 | A helper function for samepath on windows. |
| 3894 | [clinic start generated code]*/ |
| 3895 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3896 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3897 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 3898 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3899 | { |
| 3900 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3901 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 3902 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3903 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3904 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3905 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3906 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3907 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3908 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3909 | 0, /* desired access */ |
| 3910 | 0, /* share mode */ |
| 3911 | NULL, /* security attributes */ |
| 3912 | OPEN_EXISTING, |
| 3913 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3914 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3915 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3916 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3917 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3918 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3919 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3920 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3921 | |
| 3922 | /* We have a good handle to the target, use it to determine the |
| 3923 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3924 | while (1) { |
| 3925 | Py_BEGIN_ALLOW_THREADS |
| 3926 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 3927 | buf_size, VOLUME_NAME_DOS); |
| 3928 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3929 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3930 | if (!result_length) { |
| 3931 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 3932 | path->object); |
| 3933 | goto cleanup; |
| 3934 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3935 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3936 | if (result_length < buf_size) { |
| 3937 | break; |
| 3938 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3939 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3940 | wchar_t *tmp; |
| 3941 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 3942 | result_length * sizeof(*tmp)); |
| 3943 | if (!tmp) { |
| 3944 | result = PyErr_NoMemory(); |
| 3945 | goto cleanup; |
| 3946 | } |
| 3947 | |
| 3948 | buf_size = result_length; |
| 3949 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3950 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3951 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3952 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 3953 | if (result && path->narrow) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3954 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 3955 | } |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3956 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3957 | cleanup: |
| 3958 | if (target_path != buf) { |
| 3959 | PyMem_Free(target_path); |
| 3960 | } |
| 3961 | CloseHandle(hFile); |
| 3962 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3963 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3964 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3965 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3966 | /*[clinic input] |
| 3967 | os._getvolumepathname |
| 3968 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3969 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3970 | |
| 3971 | A helper function for ismount on Win32. |
| 3972 | [clinic start generated code]*/ |
| 3973 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3974 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3975 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 3976 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3977 | { |
| 3978 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3979 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3980 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3981 | BOOL ret; |
| 3982 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3983 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3984 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3985 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 3986 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3987 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 3988 | return NULL; |
| 3989 | } |
| 3990 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3991 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3992 | if (mountpath == NULL) |
| 3993 | return PyErr_NoMemory(); |
| 3994 | |
| 3995 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3996 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3997 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3998 | Py_END_ALLOW_THREADS |
| 3999 | |
| 4000 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4001 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4002 | goto exit; |
| 4003 | } |
| 4004 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4005 | if (path->narrow) |
| 4006 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4007 | |
| 4008 | exit: |
| 4009 | PyMem_Free(mountpath); |
| 4010 | return result; |
| 4011 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4012 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4013 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4014 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4015 | |
| 4016 | /*[clinic input] |
| 4017 | os.mkdir |
| 4018 | |
| 4019 | path : path_t |
| 4020 | |
| 4021 | mode: int = 0o777 |
| 4022 | |
| 4023 | * |
| 4024 | |
| 4025 | dir_fd : dir_fd(requires='mkdirat') = None |
| 4026 | |
| 4027 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4028 | |
| 4029 | Create a directory. |
| 4030 | |
| 4031 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4032 | and path should be relative; path will then be relative to that directory. |
| 4033 | dir_fd may not be implemented on your platform. |
| 4034 | If it is unavailable, using it will raise a NotImplementedError. |
| 4035 | |
| 4036 | The mode argument is ignored on Windows. |
| 4037 | [clinic start generated code]*/ |
| 4038 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4039 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4040 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 4041 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4042 | { |
| 4043 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4044 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4045 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4046 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4047 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4048 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4049 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4050 | if (!result) |
| 4051 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4052 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4053 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4054 | #if HAVE_MKDIRAT |
| 4055 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4056 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4057 | else |
| 4058 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 4059 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4060 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4061 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4062 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4063 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4064 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4065 | if (result < 0) |
| 4066 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4067 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4068 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4071 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4072 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4073 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4074 | #include <sys/resource.h> |
| 4075 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4076 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4077 | |
| 4078 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4079 | /*[clinic input] |
| 4080 | os.nice |
| 4081 | |
| 4082 | increment: int |
| 4083 | / |
| 4084 | |
| 4085 | Add increment to the priority of process and return the new priority. |
| 4086 | [clinic start generated code]*/ |
| 4087 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4088 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4089 | os_nice_impl(PyObject *module, int increment) |
| 4090 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4091 | { |
| 4092 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4093 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4094 | /* There are two flavours of 'nice': one that returns the new |
| 4095 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 4096 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4097 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4098 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4099 | If we are of the nice family that returns the new priority, we |
| 4100 | need to clear errno before the call, and check if errno is filled |
| 4101 | before calling posix_error() on a returnvalue of -1, because the |
| 4102 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4103 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4104 | errno = 0; |
| 4105 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4106 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4107 | if (value == 0) |
| 4108 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4109 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4110 | if (value == -1 && errno != 0) |
| 4111 | /* either nice() or getpriority() returned an error */ |
| 4112 | return posix_error(); |
| 4113 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4114 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4115 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4116 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4117 | |
| 4118 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4119 | /*[clinic input] |
| 4120 | os.getpriority |
| 4121 | |
| 4122 | which: int |
| 4123 | who: int |
| 4124 | |
| 4125 | Return program scheduling priority. |
| 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_getpriority_impl(PyObject *module, int which, int who) |
| 4130 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4131 | { |
| 4132 | int retval; |
| 4133 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4134 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4135 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4136 | if (errno != 0) |
| 4137 | return posix_error(); |
| 4138 | return PyLong_FromLong((long)retval); |
| 4139 | } |
| 4140 | #endif /* HAVE_GETPRIORITY */ |
| 4141 | |
| 4142 | |
| 4143 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4144 | /*[clinic input] |
| 4145 | os.setpriority |
| 4146 | |
| 4147 | which: int |
| 4148 | who: int |
| 4149 | priority: int |
| 4150 | |
| 4151 | Set program scheduling priority. |
| 4152 | [clinic start generated code]*/ |
| 4153 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4154 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4155 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4156 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4157 | { |
| 4158 | int retval; |
| 4159 | |
| 4160 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4161 | if (retval == -1) |
| 4162 | return posix_error(); |
| 4163 | Py_RETURN_NONE; |
| 4164 | } |
| 4165 | #endif /* HAVE_SETPRIORITY */ |
| 4166 | |
| 4167 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4168 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4169 | 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] | 4170 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4171 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4172 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4173 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4174 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4175 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4176 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4177 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4178 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4179 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4180 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4181 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4182 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4183 | #ifndef HAVE_RENAMEAT |
| 4184 | if (dir_fd_specified) { |
| 4185 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4186 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4187 | } |
| 4188 | #endif |
| 4189 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4190 | #ifdef MS_WINDOWS |
| 4191 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4192 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4193 | Py_END_ALLOW_THREADS |
| 4194 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4195 | if (!result) |
| 4196 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4197 | |
| 4198 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4199 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4200 | PyErr_Format(PyExc_ValueError, |
| 4201 | "%s: src and dst must be the same type", function_name); |
| 4202 | return NULL; |
| 4203 | } |
| 4204 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4205 | Py_BEGIN_ALLOW_THREADS |
| 4206 | #ifdef HAVE_RENAMEAT |
| 4207 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4208 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4209 | else |
| 4210 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4211 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4212 | Py_END_ALLOW_THREADS |
| 4213 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4214 | if (result) |
| 4215 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4216 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4217 | Py_RETURN_NONE; |
| 4218 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4219 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4220 | |
| 4221 | /*[clinic input] |
| 4222 | os.rename |
| 4223 | |
| 4224 | src : path_t |
| 4225 | dst : path_t |
| 4226 | * |
| 4227 | src_dir_fd : dir_fd = None |
| 4228 | dst_dir_fd : dir_fd = None |
| 4229 | |
| 4230 | Rename a file or directory. |
| 4231 | |
| 4232 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4233 | descriptor open to a directory, and the respective path string (src or dst) |
| 4234 | should be relative; the path will then be relative to that directory. |
| 4235 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4236 | If they are unavailable, using them will raise a NotImplementedError. |
| 4237 | [clinic start generated code]*/ |
| 4238 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4239 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4240 | 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] | 4241 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4242 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4243 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4244 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4245 | } |
| 4246 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4247 | |
| 4248 | /*[clinic input] |
| 4249 | os.replace = os.rename |
| 4250 | |
| 4251 | Rename a file or directory, overwriting the destination. |
| 4252 | |
| 4253 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4254 | descriptor open to a directory, and the respective path string (src or dst) |
| 4255 | should be relative; the path will then be relative to that directory. |
| 4256 | 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] | 4257 | If they are unavailable, using them will raise a NotImplementedError. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4258 | [clinic start generated code]*/ |
| 4259 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4260 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4261 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4262 | int dst_dir_fd) |
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4263 | /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4264 | { |
| 4265 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4266 | } |
| 4267 | |
| 4268 | |
| 4269 | /*[clinic input] |
| 4270 | os.rmdir |
| 4271 | |
| 4272 | path: path_t |
| 4273 | * |
| 4274 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4275 | |
| 4276 | Remove a directory. |
| 4277 | |
| 4278 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4279 | and path should be relative; path will then be relative to that directory. |
| 4280 | dir_fd may not be implemented on your platform. |
| 4281 | If it is unavailable, using it will raise a NotImplementedError. |
| 4282 | [clinic start generated code]*/ |
| 4283 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4284 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4285 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4286 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4287 | { |
| 4288 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4289 | |
| 4290 | Py_BEGIN_ALLOW_THREADS |
| 4291 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4292 | /* Windows, success=1, UNIX, success=0 */ |
| 4293 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4294 | #else |
| 4295 | #ifdef HAVE_UNLINKAT |
| 4296 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4297 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4298 | else |
| 4299 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4300 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4301 | #endif |
| 4302 | Py_END_ALLOW_THREADS |
| 4303 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4304 | if (result) |
| 4305 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4306 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4307 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4310 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4311 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4312 | #ifdef MS_WINDOWS |
| 4313 | /*[clinic input] |
| 4314 | os.system -> long |
| 4315 | |
| 4316 | command: Py_UNICODE |
| 4317 | |
| 4318 | Execute the command in a subshell. |
| 4319 | [clinic start generated code]*/ |
| 4320 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4321 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4322 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4323 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4324 | { |
| 4325 | long result; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4326 | |
| 4327 | if (PySys_Audit("system", "(u)", command) < 0) { |
| 4328 | return -1; |
| 4329 | } |
| 4330 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4331 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4332 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4333 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4334 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4335 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4336 | return result; |
| 4337 | } |
| 4338 | #else /* MS_WINDOWS */ |
| 4339 | /*[clinic input] |
| 4340 | os.system -> long |
| 4341 | |
| 4342 | command: FSConverter |
| 4343 | |
| 4344 | Execute the command in a subshell. |
| 4345 | [clinic start generated code]*/ |
| 4346 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4347 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4348 | os_system_impl(PyObject *module, PyObject *command) |
| 4349 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4350 | { |
| 4351 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4352 | const char *bytes = PyBytes_AsString(command); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4353 | |
| 4354 | if (PySys_Audit("system", "(O)", command) < 0) { |
| 4355 | return -1; |
| 4356 | } |
| 4357 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4358 | Py_BEGIN_ALLOW_THREADS |
| 4359 | result = system(bytes); |
| 4360 | Py_END_ALLOW_THREADS |
| 4361 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4362 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4363 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4364 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4365 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4366 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4367 | /*[clinic input] |
| 4368 | os.umask |
| 4369 | |
| 4370 | mask: int |
| 4371 | / |
| 4372 | |
| 4373 | Set the current numeric umask and return the previous umask. |
| 4374 | [clinic start generated code]*/ |
| 4375 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4376 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4377 | os_umask_impl(PyObject *module, int mask) |
| 4378 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4379 | { |
| 4380 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4381 | if (i < 0) |
| 4382 | return posix_error(); |
| 4383 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4384 | } |
| 4385 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4386 | #ifdef MS_WINDOWS |
| 4387 | |
| 4388 | /* override the default DeleteFileW behavior so that directory |
| 4389 | symlinks can be removed with this function, the same as with |
| 4390 | Unix symlinks */ |
| 4391 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4392 | { |
| 4393 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4394 | WIN32_FIND_DATAW find_data; |
| 4395 | HANDLE find_data_handle; |
| 4396 | int is_directory = 0; |
| 4397 | int is_link = 0; |
| 4398 | |
| 4399 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4400 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4401 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4402 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4403 | it is a symlink */ |
| 4404 | if(is_directory && |
| 4405 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4406 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4407 | |
| 4408 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4409 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4410 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4411 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4412 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4413 | FindClose(find_data_handle); |
| 4414 | } |
| 4415 | } |
| 4416 | } |
| 4417 | |
| 4418 | if (is_directory && is_link) |
| 4419 | return RemoveDirectoryW(lpFileName); |
| 4420 | |
| 4421 | return DeleteFileW(lpFileName); |
| 4422 | } |
| 4423 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4424 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4425 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4426 | /*[clinic input] |
| 4427 | os.unlink |
| 4428 | |
| 4429 | path: path_t |
| 4430 | * |
| 4431 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4432 | |
| 4433 | Remove a file (same as remove()). |
| 4434 | |
| 4435 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4436 | and path should be relative; path will then be relative to that directory. |
| 4437 | dir_fd may not be implemented on your platform. |
| 4438 | If it is unavailable, using it will raise a NotImplementedError. |
| 4439 | |
| 4440 | [clinic start generated code]*/ |
| 4441 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4442 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4443 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4444 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4445 | { |
| 4446 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4447 | |
| 4448 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4449 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4450 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4451 | /* Windows, success=1, UNIX, success=0 */ |
| 4452 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4453 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4454 | #ifdef HAVE_UNLINKAT |
| 4455 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4456 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4457 | else |
| 4458 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4459 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4460 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4461 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4462 | Py_END_ALLOW_THREADS |
| 4463 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4464 | if (result) |
| 4465 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4466 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4467 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4470 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4471 | /*[clinic input] |
| 4472 | os.remove = os.unlink |
| 4473 | |
| 4474 | Remove a file (same as unlink()). |
| 4475 | |
| 4476 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4477 | and path should be relative; path will then be relative to that directory. |
| 4478 | dir_fd may not be implemented on your platform. |
| 4479 | If it is unavailable, using it will raise a NotImplementedError. |
| 4480 | [clinic start generated code]*/ |
| 4481 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4482 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4483 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4484 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4485 | { |
| 4486 | return os_unlink_impl(module, path, dir_fd); |
| 4487 | } |
| 4488 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4489 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4490 | static PyStructSequence_Field uname_result_fields[] = { |
| 4491 | {"sysname", "operating system name"}, |
| 4492 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4493 | {"release", "operating system release"}, |
| 4494 | {"version", "operating system version"}, |
| 4495 | {"machine", "hardware identifier"}, |
| 4496 | {NULL} |
| 4497 | }; |
| 4498 | |
| 4499 | PyDoc_STRVAR(uname_result__doc__, |
| 4500 | "uname_result: Result from os.uname().\n\n\ |
| 4501 | This object may be accessed either as a tuple of\n\ |
| 4502 | (sysname, nodename, release, version, machine),\n\ |
| 4503 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4504 | \n\ |
| 4505 | See os.uname for more information."); |
| 4506 | |
| 4507 | static PyStructSequence_Desc uname_result_desc = { |
| 4508 | "uname_result", /* name */ |
| 4509 | uname_result__doc__, /* doc */ |
| 4510 | uname_result_fields, |
| 4511 | 5 |
| 4512 | }; |
| 4513 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 4514 | static PyTypeObject* UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4515 | |
| 4516 | |
| 4517 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4518 | /*[clinic input] |
| 4519 | os.uname |
| 4520 | |
| 4521 | Return an object identifying the current operating system. |
| 4522 | |
| 4523 | The object behaves like a named tuple with the following fields: |
| 4524 | (sysname, nodename, release, version, machine) |
| 4525 | |
| 4526 | [clinic start generated code]*/ |
| 4527 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4528 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4529 | os_uname_impl(PyObject *module) |
| 4530 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4531 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4532 | struct utsname u; |
| 4533 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4534 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4535 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4536 | Py_BEGIN_ALLOW_THREADS |
| 4537 | res = uname(&u); |
| 4538 | Py_END_ALLOW_THREADS |
| 4539 | if (res < 0) |
| 4540 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4541 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 4542 | value = PyStructSequence_New(UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4543 | if (value == NULL) |
| 4544 | return NULL; |
| 4545 | |
| 4546 | #define SET(i, field) \ |
| 4547 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4548 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4549 | if (!o) { \ |
| 4550 | Py_DECREF(value); \ |
| 4551 | return NULL; \ |
| 4552 | } \ |
| 4553 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4554 | } \ |
| 4555 | |
| 4556 | SET(0, u.sysname); |
| 4557 | SET(1, u.nodename); |
| 4558 | SET(2, u.release); |
| 4559 | SET(3, u.version); |
| 4560 | SET(4, u.machine); |
| 4561 | |
| 4562 | #undef SET |
| 4563 | |
| 4564 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4565 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4566 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4567 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4568 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4569 | |
| 4570 | typedef struct { |
| 4571 | int now; |
| 4572 | time_t atime_s; |
| 4573 | long atime_ns; |
| 4574 | time_t mtime_s; |
| 4575 | long mtime_ns; |
| 4576 | } utime_t; |
| 4577 | |
| 4578 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4579 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4580 | * they also intentionally leak the declaration of a pointer named "time" |
| 4581 | */ |
| 4582 | #define UTIME_TO_TIMESPEC \ |
| 4583 | struct timespec ts[2]; \ |
| 4584 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4585 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4586 | time = NULL; \ |
| 4587 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4588 | ts[0].tv_sec = ut->atime_s; \ |
| 4589 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4590 | ts[1].tv_sec = ut->mtime_s; \ |
| 4591 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4592 | time = ts; \ |
| 4593 | } \ |
| 4594 | |
| 4595 | #define UTIME_TO_TIMEVAL \ |
| 4596 | struct timeval tv[2]; \ |
| 4597 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4598 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4599 | time = NULL; \ |
| 4600 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4601 | tv[0].tv_sec = ut->atime_s; \ |
| 4602 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4603 | tv[1].tv_sec = ut->mtime_s; \ |
| 4604 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4605 | time = tv; \ |
| 4606 | } \ |
| 4607 | |
| 4608 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4609 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4610 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4611 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4612 | time = NULL; \ |
| 4613 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4614 | u.actime = ut->atime_s; \ |
| 4615 | u.modtime = ut->mtime_s; \ |
| 4616 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | #define UTIME_TO_TIME_T \ |
| 4620 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4621 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4622 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4623 | time = NULL; \ |
| 4624 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4625 | timet[0] = ut->atime_s; \ |
| 4626 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4627 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4628 | } \ |
| 4629 | |
| 4630 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4631 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4632 | |
| 4633 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4634 | 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] | 4635 | { |
| 4636 | #ifdef HAVE_UTIMENSAT |
| 4637 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4638 | UTIME_TO_TIMESPEC; |
| 4639 | return utimensat(dir_fd, path, time, flags); |
| 4640 | #elif defined(HAVE_FUTIMESAT) |
| 4641 | UTIME_TO_TIMEVAL; |
| 4642 | /* |
| 4643 | * follow_symlinks will never be false here; |
| 4644 | * we only allow !follow_symlinks and dir_fd together |
| 4645 | * if we have utimensat() |
| 4646 | */ |
| 4647 | assert(follow_symlinks); |
| 4648 | return futimesat(dir_fd, path, time); |
| 4649 | #endif |
| 4650 | } |
| 4651 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4652 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4653 | #else |
| 4654 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4655 | #endif |
| 4656 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4657 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4658 | |
| 4659 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4660 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4661 | { |
| 4662 | #ifdef HAVE_FUTIMENS |
| 4663 | UTIME_TO_TIMESPEC; |
| 4664 | return futimens(fd, time); |
| 4665 | #else |
| 4666 | UTIME_TO_TIMEVAL; |
| 4667 | return futimes(fd, time); |
| 4668 | #endif |
| 4669 | } |
| 4670 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4671 | #define PATH_UTIME_HAVE_FD 1 |
| 4672 | #else |
| 4673 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4674 | #endif |
| 4675 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4676 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4677 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4678 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4679 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4680 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4681 | |
| 4682 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4683 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4684 | { |
| 4685 | #ifdef HAVE_UTIMENSAT |
| 4686 | UTIME_TO_TIMESPEC; |
| 4687 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4688 | #else |
| 4689 | UTIME_TO_TIMEVAL; |
| 4690 | return lutimes(path, time); |
| 4691 | #endif |
| 4692 | } |
| 4693 | |
| 4694 | #endif |
| 4695 | |
| 4696 | #ifndef MS_WINDOWS |
| 4697 | |
| 4698 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4699 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4700 | { |
| 4701 | #ifdef HAVE_UTIMENSAT |
| 4702 | UTIME_TO_TIMESPEC; |
| 4703 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4704 | #elif defined(HAVE_UTIMES) |
| 4705 | UTIME_TO_TIMEVAL; |
| 4706 | return utimes(path, time); |
| 4707 | #elif defined(HAVE_UTIME_H) |
| 4708 | UTIME_TO_UTIMBUF; |
| 4709 | return utime(path, time); |
| 4710 | #else |
| 4711 | UTIME_TO_TIME_T; |
| 4712 | return utime(path, time); |
| 4713 | #endif |
| 4714 | } |
| 4715 | |
| 4716 | #endif |
| 4717 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4718 | static int |
| 4719 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4720 | { |
| 4721 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4722 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4723 | divmod = PyNumber_Divmod(py_long, billion); |
| 4724 | if (!divmod) |
| 4725 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4726 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 4727 | PyErr_Format(PyExc_TypeError, |
| 4728 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
| 4729 | Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name); |
| 4730 | goto exit; |
| 4731 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4732 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4733 | if ((*s == -1) && PyErr_Occurred()) |
| 4734 | goto exit; |
| 4735 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4736 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4737 | goto exit; |
| 4738 | |
| 4739 | result = 1; |
| 4740 | exit: |
| 4741 | Py_XDECREF(divmod); |
| 4742 | return result; |
| 4743 | } |
| 4744 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4745 | |
| 4746 | /*[clinic input] |
| 4747 | os.utime |
| 4748 | |
| 4749 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4750 | times: object = None |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4751 | * |
| 4752 | ns: object = NULL |
| 4753 | dir_fd: dir_fd(requires='futimensat') = None |
| 4754 | follow_symlinks: bool=True |
| 4755 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4756 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4757 | |
| 4758 | Set the access and modified time of path. |
| 4759 | |
| 4760 | path may always be specified as a string. |
| 4761 | On some platforms, path may also be specified as an open file descriptor. |
| 4762 | If this functionality is unavailable, using it raises an exception. |
| 4763 | |
| 4764 | If times is not None, it must be a tuple (atime, mtime); |
| 4765 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4766 | 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] | 4767 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4768 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4769 | 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] | 4770 | Specifying tuples for both times and ns is an error. |
| 4771 | |
| 4772 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4773 | and path should be relative; path will then be relative to that directory. |
| 4774 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4775 | link, utime will modify the symbolic link itself instead of the file the |
| 4776 | link points to. |
| 4777 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4778 | as an open file descriptor. |
| 4779 | dir_fd and follow_symlinks may not be available on your platform. |
| 4780 | If they are unavailable, using them will raise a NotImplementedError. |
| 4781 | |
| 4782 | [clinic start generated code]*/ |
| 4783 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4784 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4785 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4786 | int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4787 | /*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4788 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4789 | #ifdef MS_WINDOWS |
| 4790 | HANDLE hFile; |
| 4791 | FILETIME atime, mtime; |
| 4792 | #else |
| 4793 | int result; |
| 4794 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4795 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4796 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4797 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4798 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4799 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4800 | if (times != Py_None && ns) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4801 | PyErr_SetString(PyExc_ValueError, |
| 4802 | "utime: you may specify either 'times'" |
| 4803 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4804 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4805 | } |
| 4806 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4807 | if (times != Py_None) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4808 | time_t a_sec, m_sec; |
| 4809 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4810 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4811 | PyErr_SetString(PyExc_TypeError, |
| 4812 | "utime: 'times' must be either" |
| 4813 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4814 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4815 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4816 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4817 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4818 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4819 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4820 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4821 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4822 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4823 | utime.atime_s = a_sec; |
| 4824 | utime.atime_ns = a_nsec; |
| 4825 | utime.mtime_s = m_sec; |
| 4826 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4827 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4828 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4829 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4830 | PyErr_SetString(PyExc_TypeError, |
| 4831 | "utime: 'ns' must be a tuple of two ints"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4832 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4833 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4834 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4835 | 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] | 4836 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4837 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4838 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4839 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4840 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4841 | } |
| 4842 | else { |
| 4843 | /* times and ns are both None/unspecified. use "now". */ |
| 4844 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4845 | } |
| 4846 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4847 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4848 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4849 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4850 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4851 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4852 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4853 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4854 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4855 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4856 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4857 | #if !defined(HAVE_UTIMENSAT) |
| 4858 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4859 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4860 | "utime: cannot use dir_fd and follow_symlinks " |
| 4861 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4862 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4863 | } |
| 4864 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4865 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4866 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4867 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4868 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 4869 | NULL, OPEN_EXISTING, |
| 4870 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4871 | Py_END_ALLOW_THREADS |
| 4872 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4873 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4874 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4875 | } |
| 4876 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4877 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4878 | GetSystemTimeAsFileTime(&mtime); |
| 4879 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4880 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4881 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4882 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4883 | _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] | 4884 | } |
| 4885 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4886 | /* Avoid putting the file name into the error here, |
| 4887 | as that may confuse the user into believing that |
| 4888 | something is wrong with the file, when it also |
| 4889 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4890 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4891 | CloseHandle(hFile); |
| 4892 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4893 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4894 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4895 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4896 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4897 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4898 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4899 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4900 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4901 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4902 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4903 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4904 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4905 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4906 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4907 | else |
| 4908 | #endif |
| 4909 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4910 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4911 | if (path->fd != -1) |
| 4912 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4913 | else |
| 4914 | #endif |
| 4915 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4916 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4917 | |
| 4918 | Py_END_ALLOW_THREADS |
| 4919 | |
| 4920 | if (result < 0) { |
| 4921 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4922 | posix_error(); |
| 4923 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4924 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4925 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4926 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4927 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4928 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4931 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4932 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4933 | |
| 4934 | /*[clinic input] |
| 4935 | os._exit |
| 4936 | |
| 4937 | status: int |
| 4938 | |
| 4939 | Exit to the system with specified status, without normal exit processing. |
| 4940 | [clinic start generated code]*/ |
| 4941 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4942 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4943 | os__exit_impl(PyObject *module, int status) |
| 4944 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4945 | { |
| 4946 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4947 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4950 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4951 | #define EXECV_CHAR wchar_t |
| 4952 | #else |
| 4953 | #define EXECV_CHAR char |
| 4954 | #endif |
| 4955 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 4956 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4957 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4958 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4959 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4960 | Py_ssize_t i; |
| 4961 | for (i = 0; i < count; i++) |
| 4962 | PyMem_Free(array[i]); |
| 4963 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4964 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4965 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4966 | static int |
| 4967 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4968 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4969 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4970 | PyObject *ub; |
| 4971 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4972 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4973 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4974 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4975 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 4976 | if (*out) |
| 4977 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4978 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4979 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4980 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4981 | size = PyBytes_GET_SIZE(ub); |
| 4982 | *out = PyMem_Malloc(size + 1); |
| 4983 | if (*out) { |
| 4984 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 4985 | result = 1; |
| 4986 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4987 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4988 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4989 | Py_DECREF(ub); |
| 4990 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4991 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4992 | #endif |
| 4993 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 4994 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4995 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4996 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4997 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4998 | Py_ssize_t i, pos, envc; |
| 4999 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5000 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5001 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5002 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5003 | i = PyMapping_Size(env); |
| 5004 | if (i < 0) |
| 5005 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5006 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5007 | if (envlist == NULL) { |
| 5008 | PyErr_NoMemory(); |
| 5009 | return NULL; |
| 5010 | } |
| 5011 | envc = 0; |
| 5012 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5013 | if (!keys) |
| 5014 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5015 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5016 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5017 | goto error; |
| 5018 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5019 | PyErr_Format(PyExc_TypeError, |
| 5020 | "env.keys() or env.values() is not a list"); |
| 5021 | goto error; |
| 5022 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5023 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5024 | for (pos = 0; pos < i; pos++) { |
| 5025 | key = PyList_GetItem(keys, pos); |
| 5026 | val = PyList_GetItem(vals, pos); |
| 5027 | if (!key || !val) |
| 5028 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5029 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5030 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5031 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 5032 | goto error; |
| 5033 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 5034 | Py_DECREF(key2); |
| 5035 | goto error; |
| 5036 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5037 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 5038 | defining hidden environment variables. */ |
| 5039 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 5040 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 5041 | { |
| 5042 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5043 | Py_DECREF(key2); |
| 5044 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5045 | goto error; |
| 5046 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5047 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 5048 | #else |
| 5049 | if (!PyUnicode_FSConverter(key, &key2)) |
| 5050 | goto error; |
| 5051 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 5052 | Py_DECREF(key2); |
| 5053 | goto error; |
| 5054 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5055 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 5056 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 5057 | { |
| 5058 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5059 | Py_DECREF(key2); |
| 5060 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5061 | goto error; |
| 5062 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5063 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 5064 | PyBytes_AS_STRING(val2)); |
| 5065 | #endif |
| 5066 | Py_DECREF(key2); |
| 5067 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5068 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5069 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5070 | |
| 5071 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 5072 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5073 | goto error; |
| 5074 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5075 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5076 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5077 | } |
| 5078 | Py_DECREF(vals); |
| 5079 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5080 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5081 | envlist[envc] = 0; |
| 5082 | *envc_ptr = envc; |
| 5083 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5084 | |
| 5085 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5086 | Py_XDECREF(keys); |
| 5087 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5088 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5089 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5090 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5091 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5092 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5093 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5094 | { |
| 5095 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5096 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5097 | if (argvlist == NULL) { |
| 5098 | PyErr_NoMemory(); |
| 5099 | return NULL; |
| 5100 | } |
| 5101 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5102 | PyObject* item = PySequence_ITEM(argv, i); |
| 5103 | if (item == NULL) |
| 5104 | goto fail; |
| 5105 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5106 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5107 | goto fail; |
| 5108 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5109 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5110 | } |
| 5111 | argvlist[*argc] = NULL; |
| 5112 | return argvlist; |
| 5113 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5114 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5115 | free_string_array(argvlist, *argc); |
| 5116 | return NULL; |
| 5117 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5118 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5119 | #endif |
| 5120 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5121 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5122 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5123 | /*[clinic input] |
| 5124 | os.execv |
| 5125 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5126 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5127 | Path of executable file. |
| 5128 | argv: object |
| 5129 | Tuple or list of strings. |
| 5130 | / |
| 5131 | |
| 5132 | Execute an executable path with arguments, replacing current process. |
| 5133 | [clinic start generated code]*/ |
| 5134 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5135 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5136 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5137 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5138 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5139 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5140 | Py_ssize_t argc; |
| 5141 | |
| 5142 | /* execv has two arguments: (path, argv), where |
| 5143 | argv is a list or tuple of strings. */ |
| 5144 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5145 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5146 | PyErr_SetString(PyExc_TypeError, |
| 5147 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5148 | return NULL; |
| 5149 | } |
| 5150 | argc = PySequence_Size(argv); |
| 5151 | if (argc < 1) { |
| 5152 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5153 | return NULL; |
| 5154 | } |
| 5155 | |
| 5156 | argvlist = parse_arglist(argv, &argc); |
| 5157 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5158 | return NULL; |
| 5159 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5160 | if (!argvlist[0][0]) { |
| 5161 | PyErr_SetString(PyExc_ValueError, |
| 5162 | "execv() arg 2 first element cannot be empty"); |
| 5163 | free_string_array(argvlist, argc); |
| 5164 | return NULL; |
| 5165 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5166 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5167 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5168 | #ifdef HAVE_WEXECV |
| 5169 | _wexecv(path->wide, argvlist); |
| 5170 | #else |
| 5171 | execv(path->narrow, argvlist); |
| 5172 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5173 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5174 | |
| 5175 | /* If we get here it's definitely an error */ |
| 5176 | |
| 5177 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5178 | return posix_error(); |
| 5179 | } |
| 5180 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5181 | |
| 5182 | /*[clinic input] |
| 5183 | os.execve |
| 5184 | |
| 5185 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5186 | Path of executable file. |
| 5187 | argv: object |
| 5188 | Tuple or list of strings. |
| 5189 | env: object |
| 5190 | Dictionary of strings mapping to strings. |
| 5191 | |
| 5192 | Execute an executable path with arguments, replacing current process. |
| 5193 | [clinic start generated code]*/ |
| 5194 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5195 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5196 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5197 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5198 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5199 | EXECV_CHAR **argvlist = NULL; |
| 5200 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5201 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5202 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5203 | /* execve has three arguments: (path, argv, env), where |
| 5204 | argv is a list or tuple of strings and env is a dictionary |
| 5205 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5206 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5207 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5208 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5209 | "execve: argv must be a tuple or list"); |
| 5210 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5211 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5212 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5213 | if (argc < 1) { |
| 5214 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5215 | return NULL; |
| 5216 | } |
| 5217 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5218 | if (!PyMapping_Check(env)) { |
| 5219 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5220 | "execve: environment must be a mapping object"); |
| 5221 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5222 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5223 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5224 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5225 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5226 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5227 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5228 | if (!argvlist[0][0]) { |
| 5229 | PyErr_SetString(PyExc_ValueError, |
| 5230 | "execve: argv first element cannot be empty"); |
| 5231 | goto fail; |
| 5232 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5233 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5234 | envlist = parse_envlist(env, &envc); |
| 5235 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5236 | goto fail; |
| 5237 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5238 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5239 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5240 | if (path->fd > -1) |
| 5241 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5242 | else |
| 5243 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5244 | #ifdef HAVE_WEXECV |
| 5245 | _wexecve(path->wide, argvlist, envlist); |
| 5246 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5247 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5248 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5249 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5250 | |
| 5251 | /* If we get here it's definitely an error */ |
| 5252 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5253 | posix_path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5254 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5255 | free_string_array(envlist, envc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5256 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5257 | if (argvlist) |
| 5258 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5259 | return NULL; |
| 5260 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5261 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5262 | #endif /* HAVE_EXECV */ |
| 5263 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5264 | #ifdef HAVE_POSIX_SPAWN |
| 5265 | |
| 5266 | enum posix_spawn_file_actions_identifier { |
| 5267 | POSIX_SPAWN_OPEN, |
| 5268 | POSIX_SPAWN_CLOSE, |
| 5269 | POSIX_SPAWN_DUP2 |
| 5270 | }; |
| 5271 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5272 | #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] | 5273 | static int |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5274 | convert_sched_param(PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5275 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5276 | |
| 5277 | static int |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5278 | parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup, |
| 5279 | int resetids, int setsid, PyObject *setsigmask, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5280 | PyObject *setsigdef, PyObject *scheduler, |
| 5281 | posix_spawnattr_t *attrp) |
| 5282 | { |
| 5283 | long all_flags = 0; |
| 5284 | |
| 5285 | errno = posix_spawnattr_init(attrp); |
| 5286 | if (errno) { |
| 5287 | posix_error(); |
| 5288 | return -1; |
| 5289 | } |
| 5290 | |
| 5291 | if (setpgroup) { |
| 5292 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5293 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5294 | goto fail; |
| 5295 | } |
| 5296 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5297 | if (errno) { |
| 5298 | posix_error(); |
| 5299 | goto fail; |
| 5300 | } |
| 5301 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5302 | } |
| 5303 | |
| 5304 | if (resetids) { |
| 5305 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5306 | } |
| 5307 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5308 | if (setsid) { |
| 5309 | #ifdef POSIX_SPAWN_SETSID |
| 5310 | all_flags |= POSIX_SPAWN_SETSID; |
| 5311 | #elif defined(POSIX_SPAWN_SETSID_NP) |
| 5312 | all_flags |= POSIX_SPAWN_SETSID_NP; |
| 5313 | #else |
| 5314 | argument_unavailable_error(func_name, "setsid"); |
| 5315 | return -1; |
| 5316 | #endif |
| 5317 | } |
| 5318 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5319 | if (setsigmask) { |
| 5320 | sigset_t set; |
| 5321 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5322 | goto fail; |
| 5323 | } |
| 5324 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5325 | if (errno) { |
| 5326 | posix_error(); |
| 5327 | goto fail; |
| 5328 | } |
| 5329 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5330 | } |
| 5331 | |
| 5332 | if (setsigdef) { |
| 5333 | sigset_t set; |
| 5334 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5335 | goto fail; |
| 5336 | } |
| 5337 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5338 | if (errno) { |
| 5339 | posix_error(); |
| 5340 | goto fail; |
| 5341 | } |
| 5342 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5343 | } |
| 5344 | |
| 5345 | if (scheduler) { |
| 5346 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5347 | PyObject *py_schedpolicy; |
| 5348 | struct sched_param schedparam; |
| 5349 | |
| 5350 | if (!PyArg_ParseTuple(scheduler, "OO&" |
| 5351 | ";A scheduler tuple must have two elements", |
| 5352 | &py_schedpolicy, convert_sched_param, &schedparam)) { |
| 5353 | goto fail; |
| 5354 | } |
| 5355 | if (py_schedpolicy != Py_None) { |
| 5356 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5357 | |
| 5358 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5359 | goto fail; |
| 5360 | } |
| 5361 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5362 | if (errno) { |
| 5363 | posix_error(); |
| 5364 | goto fail; |
| 5365 | } |
| 5366 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5367 | } |
| 5368 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5369 | if (errno) { |
| 5370 | posix_error(); |
| 5371 | goto fail; |
| 5372 | } |
| 5373 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5374 | #else |
| 5375 | PyErr_SetString(PyExc_NotImplementedError, |
| 5376 | "The scheduler option is not supported in this system."); |
| 5377 | goto fail; |
| 5378 | #endif |
| 5379 | } |
| 5380 | |
| 5381 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5382 | if (errno) { |
| 5383 | posix_error(); |
| 5384 | goto fail; |
| 5385 | } |
| 5386 | |
| 5387 | return 0; |
| 5388 | |
| 5389 | fail: |
| 5390 | (void)posix_spawnattr_destroy(attrp); |
| 5391 | return -1; |
| 5392 | } |
| 5393 | |
| 5394 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5395 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5396 | posix_spawn_file_actions_t *file_actionsp, |
| 5397 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5398 | { |
| 5399 | PyObject *seq; |
| 5400 | PyObject *file_action = NULL; |
| 5401 | PyObject *tag_obj; |
| 5402 | |
| 5403 | seq = PySequence_Fast(file_actions, |
| 5404 | "file_actions must be a sequence or None"); |
| 5405 | if (seq == NULL) { |
| 5406 | return -1; |
| 5407 | } |
| 5408 | |
| 5409 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5410 | if (errno) { |
| 5411 | posix_error(); |
| 5412 | Py_DECREF(seq); |
| 5413 | return -1; |
| 5414 | } |
| 5415 | |
Zackery Spytz | d52a83a | 2019-06-26 14:54:20 -0600 | [diff] [blame] | 5416 | 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] | 5417 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5418 | Py_INCREF(file_action); |
| 5419 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5420 | PyErr_SetString(PyExc_TypeError, |
| 5421 | "Each file_actions element must be a non-empty tuple"); |
| 5422 | goto fail; |
| 5423 | } |
| 5424 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5425 | if (tag == -1 && PyErr_Occurred()) { |
| 5426 | goto fail; |
| 5427 | } |
| 5428 | |
| 5429 | /* Populate the file_actions object */ |
| 5430 | switch (tag) { |
| 5431 | case POSIX_SPAWN_OPEN: { |
| 5432 | int fd, oflag; |
| 5433 | PyObject *path; |
| 5434 | unsigned long mode; |
| 5435 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5436 | ";A open file_action tuple must have 5 elements", |
| 5437 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5438 | &oflag, &mode)) |
| 5439 | { |
| 5440 | goto fail; |
| 5441 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5442 | if (PyList_Append(temp_buffer, path)) { |
| 5443 | Py_DECREF(path); |
| 5444 | goto fail; |
| 5445 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5446 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5447 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5448 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5449 | if (errno) { |
| 5450 | posix_error(); |
| 5451 | goto fail; |
| 5452 | } |
| 5453 | break; |
| 5454 | } |
| 5455 | case POSIX_SPAWN_CLOSE: { |
| 5456 | int fd; |
| 5457 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5458 | ";A close file_action tuple must have 2 elements", |
| 5459 | &tag_obj, &fd)) |
| 5460 | { |
| 5461 | goto fail; |
| 5462 | } |
| 5463 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 5464 | if (errno) { |
| 5465 | posix_error(); |
| 5466 | goto fail; |
| 5467 | } |
| 5468 | break; |
| 5469 | } |
| 5470 | case POSIX_SPAWN_DUP2: { |
| 5471 | int fd1, fd2; |
| 5472 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 5473 | ";A dup2 file_action tuple must have 3 elements", |
| 5474 | &tag_obj, &fd1, &fd2)) |
| 5475 | { |
| 5476 | goto fail; |
| 5477 | } |
| 5478 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 5479 | fd1, fd2); |
| 5480 | if (errno) { |
| 5481 | posix_error(); |
| 5482 | goto fail; |
| 5483 | } |
| 5484 | break; |
| 5485 | } |
| 5486 | default: { |
| 5487 | PyErr_SetString(PyExc_TypeError, |
| 5488 | "Unknown file_actions identifier"); |
| 5489 | goto fail; |
| 5490 | } |
| 5491 | } |
| 5492 | Py_DECREF(file_action); |
| 5493 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5494 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5495 | Py_DECREF(seq); |
| 5496 | return 0; |
| 5497 | |
| 5498 | fail: |
| 5499 | Py_DECREF(seq); |
| 5500 | Py_DECREF(file_action); |
| 5501 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 5502 | return -1; |
| 5503 | } |
| 5504 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5505 | |
| 5506 | static PyObject * |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5507 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
| 5508 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5509 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5510 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5511 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5512 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5513 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5514 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5515 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5516 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5517 | posix_spawnattr_t attr; |
| 5518 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5519 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5520 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5521 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5522 | pid_t pid; |
| 5523 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5524 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5525 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5526 | 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] | 5527 | like posix.environ. */ |
| 5528 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5529 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5530 | PyErr_Format(PyExc_TypeError, |
| 5531 | "%s: argv must be a tuple or list", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5532 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5533 | } |
| 5534 | argc = PySequence_Size(argv); |
| 5535 | if (argc < 1) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5536 | PyErr_Format(PyExc_ValueError, |
| 5537 | "%s: argv must not be empty", func_name); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5538 | return NULL; |
| 5539 | } |
| 5540 | |
| 5541 | if (!PyMapping_Check(env)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5542 | PyErr_Format(PyExc_TypeError, |
| 5543 | "%s: environment must be a mapping object", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5544 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5545 | } |
| 5546 | |
| 5547 | argvlist = parse_arglist(argv, &argc); |
| 5548 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5549 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5550 | } |
| 5551 | if (!argvlist[0][0]) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5552 | PyErr_Format(PyExc_ValueError, |
| 5553 | "%s: argv first element cannot be empty", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5554 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
| 5557 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5558 | if (envlist == NULL) { |
| 5559 | goto exit; |
| 5560 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5561 | |
Anthony Shaw | 948ed8c | 2019-05-10 12:00:06 +1000 | [diff] [blame] | 5562 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5563 | /* There is a bug in old versions of glibc that makes some of the |
| 5564 | * helper functions for manipulating file actions not copy the provided |
| 5565 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 5566 | * copy the value of path for some old versions of glibc (<2.20). |
| 5567 | * The use of temp_buffer here is a workaround that keeps the |
| 5568 | * python objects that own the buffers alive until posix_spawn gets called. |
| 5569 | * Check https://bugs.python.org/issue33630 and |
| 5570 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 5571 | temp_buffer = PyList_New(0); |
| 5572 | if (!temp_buffer) { |
| 5573 | goto exit; |
| 5574 | } |
| 5575 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5576 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5577 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5578 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5579 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5580 | |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5581 | if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid, |
| 5582 | setsigmask, setsigdef, scheduler, &attr)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5583 | goto exit; |
| 5584 | } |
| 5585 | attrp = &attr; |
| 5586 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5587 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5588 | #ifdef HAVE_POSIX_SPAWNP |
| 5589 | if (use_posix_spawnp) { |
| 5590 | err_code = posix_spawnp(&pid, path->narrow, |
| 5591 | file_actionsp, attrp, argvlist, envlist); |
| 5592 | } |
| 5593 | else |
| 5594 | #endif /* HAVE_POSIX_SPAWNP */ |
| 5595 | { |
| 5596 | err_code = posix_spawn(&pid, path->narrow, |
| 5597 | file_actionsp, attrp, argvlist, envlist); |
| 5598 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5599 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5600 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5601 | if (err_code) { |
| 5602 | errno = err_code; |
| 5603 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5604 | goto exit; |
| 5605 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 5606 | #ifdef _Py_MEMORY_SANITIZER |
| 5607 | __msan_unpoison(&pid, sizeof(pid)); |
| 5608 | #endif |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5609 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5610 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5611 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5612 | if (file_actionsp) { |
| 5613 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5614 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5615 | if (attrp) { |
| 5616 | (void)posix_spawnattr_destroy(attrp); |
| 5617 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5618 | if (envlist) { |
| 5619 | free_string_array(envlist, envc); |
| 5620 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5621 | if (argvlist) { |
| 5622 | free_string_array(argvlist, argc); |
| 5623 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5624 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5625 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5626 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5627 | |
| 5628 | |
| 5629 | /*[clinic input] |
| 5630 | |
| 5631 | os.posix_spawn |
| 5632 | path: path_t |
| 5633 | Path of executable file. |
| 5634 | argv: object |
| 5635 | Tuple or list of strings. |
| 5636 | env: object |
| 5637 | Dictionary of strings mapping to strings. |
| 5638 | / |
| 5639 | * |
| 5640 | file_actions: object(c_default='NULL') = () |
| 5641 | A sequence of file action tuples. |
| 5642 | setpgroup: object = NULL |
| 5643 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5644 | resetids: bool(accept={int}) = False |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5645 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
| 5646 | setsid: bool(accept={int}) = False |
| 5647 | 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] | 5648 | setsigmask: object(c_default='NULL') = () |
| 5649 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5650 | setsigdef: object(c_default='NULL') = () |
| 5651 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5652 | scheduler: object = NULL |
| 5653 | A tuple with the scheduler policy (optional) and parameters. |
| 5654 | |
| 5655 | Execute the program specified by path in a new process. |
| 5656 | [clinic start generated code]*/ |
| 5657 | |
| 5658 | static PyObject * |
| 5659 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5660 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5661 | PyObject *setpgroup, int resetids, int setsid, |
| 5662 | PyObject *setsigmask, PyObject *setsigdef, |
| 5663 | PyObject *scheduler) |
| 5664 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5665 | { |
| 5666 | return py_posix_spawn(0, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5667 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5668 | scheduler); |
| 5669 | } |
| 5670 | #endif /* HAVE_POSIX_SPAWN */ |
| 5671 | |
| 5672 | |
| 5673 | |
| 5674 | #ifdef HAVE_POSIX_SPAWNP |
| 5675 | /*[clinic input] |
| 5676 | |
| 5677 | os.posix_spawnp |
| 5678 | path: path_t |
| 5679 | Path of executable file. |
| 5680 | argv: object |
| 5681 | Tuple or list of strings. |
| 5682 | env: object |
| 5683 | Dictionary of strings mapping to strings. |
| 5684 | / |
| 5685 | * |
| 5686 | file_actions: object(c_default='NULL') = () |
| 5687 | A sequence of file action tuples. |
| 5688 | setpgroup: object = NULL |
| 5689 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5690 | resetids: bool(accept={int}) = False |
| 5691 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5692 | setsid: bool(accept={int}) = False |
| 5693 | 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] | 5694 | setsigmask: object(c_default='NULL') = () |
| 5695 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5696 | setsigdef: object(c_default='NULL') = () |
| 5697 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5698 | scheduler: object = NULL |
| 5699 | A tuple with the scheduler policy (optional) and parameters. |
| 5700 | |
| 5701 | Execute the program specified by path in a new process. |
| 5702 | [clinic start generated code]*/ |
| 5703 | |
| 5704 | static PyObject * |
| 5705 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5706 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5707 | PyObject *setpgroup, int resetids, int setsid, |
| 5708 | PyObject *setsigmask, PyObject *setsigdef, |
| 5709 | PyObject *scheduler) |
| 5710 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5711 | { |
| 5712 | return py_posix_spawn(1, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5713 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5714 | scheduler); |
| 5715 | } |
| 5716 | #endif /* HAVE_POSIX_SPAWNP */ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5717 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5718 | #ifdef HAVE_RTPSPAWN |
| 5719 | static intptr_t |
| 5720 | _rtp_spawn(int mode, const char *rtpFileName, const char *argv[], |
| 5721 | const char *envp[]) |
| 5722 | { |
| 5723 | RTP_ID rtpid; |
| 5724 | int status; |
| 5725 | pid_t res; |
| 5726 | int async_err = 0; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5727 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5728 | /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes. |
| 5729 | uStackSize=0 cannot be used, the default stack size is too small for |
| 5730 | Python. */ |
| 5731 | if (envp) { |
| 5732 | rtpid = rtpSpawn(rtpFileName, argv, envp, |
| 5733 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5734 | } |
| 5735 | else { |
| 5736 | rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ, |
| 5737 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5738 | } |
| 5739 | if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) { |
| 5740 | do { |
| 5741 | res = waitpid((pid_t)rtpid, &status, 0); |
| 5742 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 5743 | |
| 5744 | if (res < 0) |
| 5745 | return RTP_ID_ERROR; |
| 5746 | return ((intptr_t)status); |
| 5747 | } |
| 5748 | return ((intptr_t)rtpid); |
| 5749 | } |
| 5750 | #endif |
| 5751 | |
| 5752 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5753 | /*[clinic input] |
| 5754 | os.spawnv |
| 5755 | |
| 5756 | mode: int |
| 5757 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5758 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5759 | Path of executable file. |
| 5760 | argv: object |
| 5761 | Tuple or list of strings. |
| 5762 | / |
| 5763 | |
| 5764 | Execute the program specified by path in a new process. |
| 5765 | [clinic start generated code]*/ |
| 5766 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5767 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5768 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5769 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5770 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5771 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5772 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5773 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5774 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5775 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5776 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5777 | /* spawnv has three arguments: (mode, path, argv), where |
| 5778 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5779 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5780 | if (PyList_Check(argv)) { |
| 5781 | argc = PyList_Size(argv); |
| 5782 | getitem = PyList_GetItem; |
| 5783 | } |
| 5784 | else if (PyTuple_Check(argv)) { |
| 5785 | argc = PyTuple_Size(argv); |
| 5786 | getitem = PyTuple_GetItem; |
| 5787 | } |
| 5788 | else { |
| 5789 | PyErr_SetString(PyExc_TypeError, |
| 5790 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5791 | return NULL; |
| 5792 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5793 | if (argc == 0) { |
| 5794 | PyErr_SetString(PyExc_ValueError, |
| 5795 | "spawnv() arg 2 cannot be empty"); |
| 5796 | return NULL; |
| 5797 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5798 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5799 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5800 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5801 | return PyErr_NoMemory(); |
| 5802 | } |
| 5803 | for (i = 0; i < argc; i++) { |
| 5804 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5805 | &argvlist[i])) { |
| 5806 | free_string_array(argvlist, i); |
| 5807 | PyErr_SetString( |
| 5808 | PyExc_TypeError, |
| 5809 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5810 | return NULL; |
| 5811 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5812 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5813 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5814 | PyErr_SetString( |
| 5815 | PyExc_ValueError, |
| 5816 | "spawnv() arg 2 first element cannot be empty"); |
| 5817 | return NULL; |
| 5818 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5819 | } |
| 5820 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5821 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5822 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5823 | if (mode == _OLD_P_OVERLAY) |
| 5824 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5825 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5826 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5827 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5828 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5829 | #ifdef HAVE_WSPAWNV |
| 5830 | spawnval = _wspawnv(mode, path->wide, argvlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5831 | #elif defined(HAVE_RTPSPAWN) |
| 5832 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5833 | #else |
| 5834 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 5835 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5836 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5837 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5838 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5839 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5840 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5841 | if (spawnval == -1) |
| 5842 | return posix_error(); |
| 5843 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5844 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5845 | } |
| 5846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5847 | /*[clinic input] |
| 5848 | os.spawnve |
| 5849 | |
| 5850 | mode: int |
| 5851 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5852 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5853 | Path of executable file. |
| 5854 | argv: object |
| 5855 | Tuple or list of strings. |
| 5856 | env: object |
| 5857 | Dictionary of strings mapping to strings. |
| 5858 | / |
| 5859 | |
| 5860 | Execute the program specified by path in a new process. |
| 5861 | [clinic start generated code]*/ |
| 5862 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5863 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5864 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5865 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5866 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5867 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5868 | EXECV_CHAR **argvlist; |
| 5869 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5870 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5871 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5872 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5873 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5874 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5875 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5876 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5877 | argv is a list or tuple of strings and env is a dictionary |
| 5878 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5879 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5880 | if (PyList_Check(argv)) { |
| 5881 | argc = PyList_Size(argv); |
| 5882 | getitem = PyList_GetItem; |
| 5883 | } |
| 5884 | else if (PyTuple_Check(argv)) { |
| 5885 | argc = PyTuple_Size(argv); |
| 5886 | getitem = PyTuple_GetItem; |
| 5887 | } |
| 5888 | else { |
| 5889 | PyErr_SetString(PyExc_TypeError, |
| 5890 | "spawnve() arg 2 must be a tuple or list"); |
| 5891 | goto fail_0; |
| 5892 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5893 | if (argc == 0) { |
| 5894 | PyErr_SetString(PyExc_ValueError, |
| 5895 | "spawnve() arg 2 cannot be empty"); |
| 5896 | goto fail_0; |
| 5897 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5898 | if (!PyMapping_Check(env)) { |
| 5899 | PyErr_SetString(PyExc_TypeError, |
| 5900 | "spawnve() arg 3 must be a mapping object"); |
| 5901 | goto fail_0; |
| 5902 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5903 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5904 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5905 | if (argvlist == NULL) { |
| 5906 | PyErr_NoMemory(); |
| 5907 | goto fail_0; |
| 5908 | } |
| 5909 | for (i = 0; i < argc; i++) { |
| 5910 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5911 | &argvlist[i])) |
| 5912 | { |
| 5913 | lastarg = i; |
| 5914 | goto fail_1; |
| 5915 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5916 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5917 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5918 | PyErr_SetString( |
| 5919 | PyExc_ValueError, |
| 5920 | "spawnv() arg 2 first element cannot be empty"); |
| 5921 | goto fail_1; |
| 5922 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5923 | } |
| 5924 | lastarg = argc; |
| 5925 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5926 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5927 | envlist = parse_envlist(env, &envc); |
| 5928 | if (envlist == NULL) |
| 5929 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5930 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5931 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5932 | if (mode == _OLD_P_OVERLAY) |
| 5933 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5934 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5935 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5936 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5937 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5938 | #ifdef HAVE_WSPAWNV |
| 5939 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5940 | #elif defined(HAVE_RTPSPAWN) |
| 5941 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, |
| 5942 | (const char **)envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5943 | #else |
| 5944 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 5945 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5946 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5947 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5948 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5949 | if (spawnval == -1) |
| 5950 | (void) posix_error(); |
| 5951 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5952 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5953 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5954 | while (--envc >= 0) |
| 5955 | PyMem_DEL(envlist[envc]); |
| 5956 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5957 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5958 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5959 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5960 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5961 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5962 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5963 | #endif /* HAVE_SPAWNV */ |
| 5964 | |
| 5965 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5966 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5967 | |
| 5968 | /* Helper function to validate arguments. |
| 5969 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 5970 | If obj is non-NULL it must be callable. */ |
| 5971 | static int |
| 5972 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 5973 | { |
| 5974 | if (obj && !PyCallable_Check(obj)) { |
| 5975 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
| 5976 | obj_name, Py_TYPE(obj)->tp_name); |
| 5977 | return -1; |
| 5978 | } |
| 5979 | return 0; |
| 5980 | } |
| 5981 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5982 | /*[clinic input] |
| 5983 | os.register_at_fork |
| 5984 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5985 | * |
| 5986 | before: object=NULL |
| 5987 | A callable to be called in the parent before the fork() syscall. |
| 5988 | after_in_child: object=NULL |
| 5989 | A callable to be called in the child after fork(). |
| 5990 | after_in_parent: object=NULL |
| 5991 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5992 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5993 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5994 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5995 | 'before' callbacks are called in reverse order. |
| 5996 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5997 | |
| 5998 | [clinic start generated code]*/ |
| 5999 | |
| 6000 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6001 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 6002 | PyObject *after_in_child, PyObject *after_in_parent) |
| 6003 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6004 | { |
| 6005 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6006 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6007 | if (!before && !after_in_child && !after_in_parent) { |
| 6008 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 6009 | return NULL; |
| 6010 | } |
| 6011 | if (check_null_or_callable(before, "before") || |
| 6012 | check_null_or_callable(after_in_child, "after_in_child") || |
| 6013 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6014 | return NULL; |
| 6015 | } |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 6016 | interp = _PyInterpreterState_Get(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6017 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6018 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6019 | return NULL; |
| 6020 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6021 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6022 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6023 | } |
| 6024 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 6025 | return NULL; |
| 6026 | } |
| 6027 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6028 | } |
| 6029 | #endif /* HAVE_FORK */ |
| 6030 | |
| 6031 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6032 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6033 | /*[clinic input] |
| 6034 | os.fork1 |
| 6035 | |
| 6036 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 6037 | |
| 6038 | Return 0 to child process and PID of child to parent process. |
| 6039 | [clinic start generated code]*/ |
| 6040 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6041 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6042 | os_fork1_impl(PyObject *module) |
| 6043 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6044 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6045 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6046 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6047 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6048 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6049 | return NULL; |
| 6050 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6051 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6052 | pid = fork1(); |
| 6053 | if (pid == 0) { |
| 6054 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6055 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6056 | } else { |
| 6057 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6058 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6059 | } |
| 6060 | if (pid == -1) |
| 6061 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6062 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6063 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6064 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6065 | |
| 6066 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6067 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6068 | /*[clinic input] |
| 6069 | os.fork |
| 6070 | |
| 6071 | Fork a child process. |
| 6072 | |
| 6073 | Return 0 to child process and PID of child to parent process. |
| 6074 | [clinic start generated code]*/ |
| 6075 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6076 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6077 | os_fork_impl(PyObject *module) |
| 6078 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6079 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6080 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6081 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6082 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6083 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6084 | return NULL; |
| 6085 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6086 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6087 | pid = fork(); |
| 6088 | if (pid == 0) { |
| 6089 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6090 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6091 | } else { |
| 6092 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6093 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6094 | } |
| 6095 | if (pid == -1) |
| 6096 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6097 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6098 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6099 | #endif /* HAVE_FORK */ |
| 6100 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6101 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6102 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6103 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6104 | /*[clinic input] |
| 6105 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6106 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6107 | policy: int |
| 6108 | |
| 6109 | Get the maximum scheduling priority for policy. |
| 6110 | [clinic start generated code]*/ |
| 6111 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6112 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6113 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 6114 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6115 | { |
| 6116 | int max; |
| 6117 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6118 | max = sched_get_priority_max(policy); |
| 6119 | if (max < 0) |
| 6120 | return posix_error(); |
| 6121 | return PyLong_FromLong(max); |
| 6122 | } |
| 6123 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6124 | |
| 6125 | /*[clinic input] |
| 6126 | os.sched_get_priority_min |
| 6127 | |
| 6128 | policy: int |
| 6129 | |
| 6130 | Get the minimum scheduling priority for policy. |
| 6131 | [clinic start generated code]*/ |
| 6132 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6133 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6134 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 6135 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6136 | { |
| 6137 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6138 | if (min < 0) |
| 6139 | return posix_error(); |
| 6140 | return PyLong_FromLong(min); |
| 6141 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6142 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 6143 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6144 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6145 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 6146 | /*[clinic input] |
| 6147 | os.sched_getscheduler |
| 6148 | pid: pid_t |
| 6149 | / |
| 6150 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6151 | Get the scheduling policy for the process identified by pid. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6152 | |
| 6153 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 6154 | [clinic start generated code]*/ |
| 6155 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6156 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6157 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6158 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6159 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6160 | int policy; |
| 6161 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6162 | policy = sched_getscheduler(pid); |
| 6163 | if (policy < 0) |
| 6164 | return posix_error(); |
| 6165 | return PyLong_FromLong(policy); |
| 6166 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6167 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6168 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6169 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6170 | #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] | 6171 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6172 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6173 | |
| 6174 | @classmethod |
| 6175 | os.sched_param.__new__ |
| 6176 | |
| 6177 | sched_priority: object |
| 6178 | A scheduling parameter. |
| 6179 | |
| 6180 | Current has only one field: sched_priority"); |
| 6181 | [clinic start generated code]*/ |
| 6182 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6183 | static PyObject * |
| 6184 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6185 | /*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6186 | { |
| 6187 | PyObject *res; |
| 6188 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6189 | res = PyStructSequence_New(type); |
| 6190 | if (!res) |
| 6191 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6192 | Py_INCREF(sched_priority); |
| 6193 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6194 | return res; |
| 6195 | } |
| 6196 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6197 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6198 | PyDoc_VAR(os_sched_param__doc__); |
| 6199 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6200 | static PyStructSequence_Field sched_param_fields[] = { |
| 6201 | {"sched_priority", "the scheduling priority"}, |
| 6202 | {0} |
| 6203 | }; |
| 6204 | |
| 6205 | static PyStructSequence_Desc sched_param_desc = { |
| 6206 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6207 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6208 | sched_param_fields, |
| 6209 | 1 |
| 6210 | }; |
| 6211 | |
| 6212 | static int |
| 6213 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 6214 | { |
| 6215 | long priority; |
| 6216 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6217 | if (Py_TYPE(param) != SchedParamType) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6218 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 6219 | return 0; |
| 6220 | } |
| 6221 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 6222 | if (priority == -1 && PyErr_Occurred()) |
| 6223 | return 0; |
| 6224 | if (priority > INT_MAX || priority < INT_MIN) { |
| 6225 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 6226 | return 0; |
| 6227 | } |
| 6228 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 6229 | return 1; |
| 6230 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6231 | #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] | 6232 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6233 | |
| 6234 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6235 | /*[clinic input] |
| 6236 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6237 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6238 | pid: pid_t |
| 6239 | policy: int |
| 6240 | param: sched_param |
| 6241 | / |
| 6242 | |
| 6243 | Set the scheduling policy for the process identified by pid. |
| 6244 | |
| 6245 | If pid is 0, the calling process is changed. |
| 6246 | param is an instance of sched_param. |
| 6247 | [clinic start generated code]*/ |
| 6248 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6249 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6250 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6251 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6252 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6253 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6254 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6255 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6256 | ** scheduling policy under Solaris/Illumos, and others. |
| 6257 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6258 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6259 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6260 | return posix_error(); |
| 6261 | Py_RETURN_NONE; |
| 6262 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6263 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6264 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6265 | |
| 6266 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6267 | /*[clinic input] |
| 6268 | os.sched_getparam |
| 6269 | pid: pid_t |
| 6270 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6271 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6272 | Returns scheduling parameters for the process identified by pid. |
| 6273 | |
| 6274 | If pid is 0, returns parameters for the calling process. |
| 6275 | Return value is an instance of sched_param. |
| 6276 | [clinic start generated code]*/ |
| 6277 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6278 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6279 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6280 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6281 | { |
| 6282 | struct sched_param param; |
| 6283 | PyObject *result; |
| 6284 | PyObject *priority; |
| 6285 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6286 | if (sched_getparam(pid, ¶m)) |
| 6287 | return posix_error(); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6288 | result = PyStructSequence_New(SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6289 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6290 | return NULL; |
| 6291 | priority = PyLong_FromLong(param.sched_priority); |
| 6292 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6293 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6294 | return NULL; |
| 6295 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6296 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6297 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6298 | } |
| 6299 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6300 | |
| 6301 | /*[clinic input] |
| 6302 | os.sched_setparam |
| 6303 | pid: pid_t |
| 6304 | param: sched_param |
| 6305 | / |
| 6306 | |
| 6307 | Set scheduling parameters for the process identified by pid. |
| 6308 | |
| 6309 | If pid is 0, sets parameters for the calling process. |
| 6310 | param should be an instance of sched_param. |
| 6311 | [clinic start generated code]*/ |
| 6312 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6313 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6314 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6315 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6316 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6317 | { |
| 6318 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6319 | return posix_error(); |
| 6320 | Py_RETURN_NONE; |
| 6321 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6322 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6323 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6324 | |
| 6325 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6326 | /*[clinic input] |
| 6327 | os.sched_rr_get_interval -> double |
| 6328 | pid: pid_t |
| 6329 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6330 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6331 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6332 | |
| 6333 | Value returned is a float. |
| 6334 | [clinic start generated code]*/ |
| 6335 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6336 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6337 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6338 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6339 | { |
| 6340 | struct timespec interval; |
| 6341 | if (sched_rr_get_interval(pid, &interval)) { |
| 6342 | posix_error(); |
| 6343 | return -1.0; |
| 6344 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6345 | #ifdef _Py_MEMORY_SANITIZER |
| 6346 | __msan_unpoison(&interval, sizeof(interval)); |
| 6347 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6348 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6349 | } |
| 6350 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6351 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6352 | |
| 6353 | /*[clinic input] |
| 6354 | os.sched_yield |
| 6355 | |
| 6356 | Voluntarily relinquish the CPU. |
| 6357 | [clinic start generated code]*/ |
| 6358 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6359 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6360 | os_sched_yield_impl(PyObject *module) |
| 6361 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6362 | { |
| 6363 | if (sched_yield()) |
| 6364 | return posix_error(); |
| 6365 | Py_RETURN_NONE; |
| 6366 | } |
| 6367 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6368 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6369 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6370 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6371 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6372 | /*[clinic input] |
| 6373 | os.sched_setaffinity |
| 6374 | pid: pid_t |
| 6375 | mask : object |
| 6376 | / |
| 6377 | |
| 6378 | Set the CPU affinity of the process identified by pid to mask. |
| 6379 | |
| 6380 | mask should be an iterable of integers identifying CPUs. |
| 6381 | [clinic start generated code]*/ |
| 6382 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6383 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6384 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6385 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6386 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6387 | int ncpus; |
| 6388 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6389 | cpu_set_t *cpu_set = NULL; |
| 6390 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6391 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6392 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6393 | if (iterator == NULL) |
| 6394 | return NULL; |
| 6395 | |
| 6396 | ncpus = NCPUS_START; |
| 6397 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6398 | cpu_set = CPU_ALLOC(ncpus); |
| 6399 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6400 | PyErr_NoMemory(); |
| 6401 | goto error; |
| 6402 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6403 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6404 | |
| 6405 | while ((item = PyIter_Next(iterator))) { |
| 6406 | long cpu; |
| 6407 | if (!PyLong_Check(item)) { |
| 6408 | PyErr_Format(PyExc_TypeError, |
| 6409 | "expected an iterator of ints, " |
| 6410 | "but iterator yielded %R", |
| 6411 | Py_TYPE(item)); |
| 6412 | Py_DECREF(item); |
| 6413 | goto error; |
| 6414 | } |
| 6415 | cpu = PyLong_AsLong(item); |
| 6416 | Py_DECREF(item); |
| 6417 | if (cpu < 0) { |
| 6418 | if (!PyErr_Occurred()) |
| 6419 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6420 | goto error; |
| 6421 | } |
| 6422 | if (cpu > INT_MAX - 1) { |
| 6423 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6424 | goto error; |
| 6425 | } |
| 6426 | if (cpu >= ncpus) { |
| 6427 | /* Grow CPU mask to fit the CPU number */ |
| 6428 | int newncpus = ncpus; |
| 6429 | cpu_set_t *newmask; |
| 6430 | size_t newsetsize; |
| 6431 | while (newncpus <= cpu) { |
| 6432 | if (newncpus > INT_MAX / 2) |
| 6433 | newncpus = cpu + 1; |
| 6434 | else |
| 6435 | newncpus = newncpus * 2; |
| 6436 | } |
| 6437 | newmask = CPU_ALLOC(newncpus); |
| 6438 | if (newmask == NULL) { |
| 6439 | PyErr_NoMemory(); |
| 6440 | goto error; |
| 6441 | } |
| 6442 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 6443 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6444 | memcpy(newmask, cpu_set, setsize); |
| 6445 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6446 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6447 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6448 | ncpus = newncpus; |
| 6449 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6450 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6451 | } |
Brandt Bucher | 45a30af | 2019-06-27 09:10:57 -0700 | [diff] [blame] | 6452 | if (PyErr_Occurred()) { |
| 6453 | goto error; |
| 6454 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6455 | Py_CLEAR(iterator); |
| 6456 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6457 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6458 | posix_error(); |
| 6459 | goto error; |
| 6460 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6461 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6462 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6463 | |
| 6464 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6465 | if (cpu_set) |
| 6466 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6467 | Py_XDECREF(iterator); |
| 6468 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6469 | } |
| 6470 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6471 | |
| 6472 | /*[clinic input] |
| 6473 | os.sched_getaffinity |
| 6474 | pid: pid_t |
| 6475 | / |
| 6476 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6477 | 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] | 6478 | |
| 6479 | The affinity is returned as a set of CPU identifiers. |
| 6480 | [clinic start generated code]*/ |
| 6481 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6482 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6483 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6484 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6485 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6486 | int cpu, ncpus, count; |
| 6487 | size_t setsize; |
| 6488 | cpu_set_t *mask = NULL; |
| 6489 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6490 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6491 | ncpus = NCPUS_START; |
| 6492 | while (1) { |
| 6493 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6494 | mask = CPU_ALLOC(ncpus); |
| 6495 | if (mask == NULL) |
| 6496 | return PyErr_NoMemory(); |
| 6497 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6498 | break; |
| 6499 | CPU_FREE(mask); |
| 6500 | if (errno != EINVAL) |
| 6501 | return posix_error(); |
| 6502 | if (ncpus > INT_MAX / 2) { |
| 6503 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6504 | "a large enough CPU set"); |
| 6505 | return NULL; |
| 6506 | } |
| 6507 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6508 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6509 | |
| 6510 | res = PySet_New(NULL); |
| 6511 | if (res == NULL) |
| 6512 | goto error; |
| 6513 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6514 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6515 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6516 | --count; |
| 6517 | if (cpu_num == NULL) |
| 6518 | goto error; |
| 6519 | if (PySet_Add(res, cpu_num)) { |
| 6520 | Py_DECREF(cpu_num); |
| 6521 | goto error; |
| 6522 | } |
| 6523 | Py_DECREF(cpu_num); |
| 6524 | } |
| 6525 | } |
| 6526 | CPU_FREE(mask); |
| 6527 | return res; |
| 6528 | |
| 6529 | error: |
| 6530 | if (mask) |
| 6531 | CPU_FREE(mask); |
| 6532 | Py_XDECREF(res); |
| 6533 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6534 | } |
| 6535 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6536 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6537 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6538 | #endif /* HAVE_SCHED_H */ |
| 6539 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6540 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6541 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6542 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6543 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6544 | #define DEV_PTY_FILE "/dev/ptc" |
| 6545 | #define HAVE_DEV_PTMX |
| 6546 | #else |
| 6547 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6548 | #endif |
| 6549 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6550 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6551 | #ifdef HAVE_PTY_H |
| 6552 | #include <pty.h> |
| 6553 | #else |
| 6554 | #ifdef HAVE_LIBUTIL_H |
| 6555 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6556 | #else |
| 6557 | #ifdef HAVE_UTIL_H |
| 6558 | #include <util.h> |
| 6559 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6560 | #endif /* HAVE_LIBUTIL_H */ |
| 6561 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6562 | #ifdef HAVE_STROPTS_H |
| 6563 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6564 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6565 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6566 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6567 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6568 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6569 | /*[clinic input] |
| 6570 | os.openpty |
| 6571 | |
| 6572 | Open a pseudo-terminal. |
| 6573 | |
| 6574 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6575 | for both the master and slave ends. |
| 6576 | [clinic start generated code]*/ |
| 6577 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6578 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6579 | os_openpty_impl(PyObject *module) |
| 6580 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6581 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6582 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6583 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6584 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6585 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6586 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6587 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 6588 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6589 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6590 | #endif |
| 6591 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6592 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6593 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6594 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6595 | goto posix_error; |
| 6596 | |
| 6597 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6598 | goto error; |
| 6599 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6600 | goto error; |
| 6601 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6602 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6603 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6604 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6605 | goto posix_error; |
| 6606 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6607 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6608 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6609 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6610 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6611 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6612 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6613 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6614 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6615 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6616 | goto posix_error; |
| 6617 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6618 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6619 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6620 | /* change permission of slave */ |
| 6621 | if (grantpt(master_fd) < 0) { |
| 6622 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6623 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6624 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6625 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6626 | /* unlock slave */ |
| 6627 | if (unlockpt(master_fd) < 0) { |
| 6628 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6629 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6630 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6631 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6632 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6633 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6634 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6635 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6636 | goto posix_error; |
| 6637 | |
| 6638 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6639 | if (slave_fd == -1) |
| 6640 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6641 | |
| 6642 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6643 | goto posix_error; |
| 6644 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6645 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6646 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6647 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6648 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6649 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6650 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6651 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6652 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6653 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6654 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6655 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6656 | posix_error: |
| 6657 | posix_error(); |
| 6658 | error: |
| 6659 | if (master_fd != -1) |
| 6660 | close(master_fd); |
| 6661 | if (slave_fd != -1) |
| 6662 | close(slave_fd); |
| 6663 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6664 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6665 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6666 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6667 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6668 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6669 | /*[clinic input] |
| 6670 | os.forkpty |
| 6671 | |
| 6672 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6673 | |
| 6674 | Returns a tuple of (pid, master_fd). |
| 6675 | Like fork(), return pid of 0 to the child process, |
| 6676 | and pid of child to the parent process. |
| 6677 | To both, return fd of newly opened pseudo-terminal. |
| 6678 | [clinic start generated code]*/ |
| 6679 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6680 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6681 | os_forkpty_impl(PyObject *module) |
| 6682 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6683 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6684 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6685 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6686 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6687 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6688 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6689 | return NULL; |
| 6690 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6691 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6692 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6693 | if (pid == 0) { |
| 6694 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6695 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6696 | } else { |
| 6697 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6698 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6699 | } |
| 6700 | if (pid == -1) |
| 6701 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6702 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6703 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6704 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6705 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6706 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6707 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6708 | /*[clinic input] |
| 6709 | os.getegid |
| 6710 | |
| 6711 | Return the current process's effective group id. |
| 6712 | [clinic start generated code]*/ |
| 6713 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6714 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6715 | os_getegid_impl(PyObject *module) |
| 6716 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6717 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6718 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6719 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6720 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6721 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6722 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6723 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6724 | /*[clinic input] |
| 6725 | os.geteuid |
| 6726 | |
| 6727 | Return the current process's effective user id. |
| 6728 | [clinic start generated code]*/ |
| 6729 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6730 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6731 | os_geteuid_impl(PyObject *module) |
| 6732 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6733 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6734 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6735 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6736 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6737 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6738 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6739 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6740 | /*[clinic input] |
| 6741 | os.getgid |
| 6742 | |
| 6743 | Return the current process's group id. |
| 6744 | [clinic start generated code]*/ |
| 6745 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6746 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6747 | os_getgid_impl(PyObject *module) |
| 6748 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6749 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6750 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6751 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6752 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6753 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6754 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6755 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6756 | /*[clinic input] |
| 6757 | os.getpid |
| 6758 | |
| 6759 | Return the current process id. |
| 6760 | [clinic start generated code]*/ |
| 6761 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6762 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6763 | os_getpid_impl(PyObject *module) |
| 6764 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6765 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6766 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6767 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6768 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6769 | |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6770 | #ifdef NGROUPS_MAX |
| 6771 | #define MAX_GROUPS NGROUPS_MAX |
| 6772 | #else |
| 6773 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6774 | #define MAX_GROUPS 64 |
| 6775 | #endif |
| 6776 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6777 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6778 | |
| 6779 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6780 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6781 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6782 | Returns a list of groups to which a user belongs.\n\n\ |
| 6783 | user: username to lookup\n\ |
| 6784 | group: base group id of the user"); |
| 6785 | |
| 6786 | static PyObject * |
| 6787 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6788 | { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6789 | const char *user; |
| 6790 | int i, ngroups; |
| 6791 | PyObject *list; |
| 6792 | #ifdef __APPLE__ |
| 6793 | int *groups, basegid; |
| 6794 | #else |
| 6795 | gid_t *groups, basegid; |
| 6796 | #endif |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6797 | |
| 6798 | /* |
| 6799 | * NGROUPS_MAX is defined by POSIX.1 as the maximum |
| 6800 | * number of supplimental groups a users can belong to. |
| 6801 | * We have to increment it by one because |
| 6802 | * getgrouplist() returns both the supplemental groups |
| 6803 | * and the primary group, i.e. all of the groups the |
| 6804 | * user belongs to. |
| 6805 | */ |
| 6806 | ngroups = 1 + MAX_GROUPS; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6807 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6808 | #ifdef __APPLE__ |
| 6809 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6810 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6811 | #else |
| 6812 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6813 | _Py_Gid_Converter, &basegid)) |
| 6814 | return NULL; |
| 6815 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6816 | |
| 6817 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6818 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6819 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6820 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6821 | #endif |
| 6822 | if (groups == NULL) |
| 6823 | return PyErr_NoMemory(); |
| 6824 | |
| 6825 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6826 | PyMem_Del(groups); |
| 6827 | return posix_error(); |
| 6828 | } |
| 6829 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6830 | #ifdef _Py_MEMORY_SANITIZER |
| 6831 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
| 6832 | __msan_unpoison(&ngroups, sizeof(ngroups)); |
| 6833 | __msan_unpoison(groups, ngroups*sizeof(*groups)); |
| 6834 | #endif |
| 6835 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6836 | list = PyList_New(ngroups); |
| 6837 | if (list == NULL) { |
| 6838 | PyMem_Del(groups); |
| 6839 | return NULL; |
| 6840 | } |
| 6841 | |
| 6842 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6843 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6844 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6845 | #else |
| 6846 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6847 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6848 | if (o == NULL) { |
| 6849 | Py_DECREF(list); |
| 6850 | PyMem_Del(groups); |
| 6851 | return NULL; |
| 6852 | } |
| 6853 | PyList_SET_ITEM(list, i, o); |
| 6854 | } |
| 6855 | |
| 6856 | PyMem_Del(groups); |
| 6857 | |
| 6858 | return list; |
| 6859 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6860 | #endif /* HAVE_GETGROUPLIST */ |
| 6861 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6862 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6863 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6864 | /*[clinic input] |
| 6865 | os.getgroups |
| 6866 | |
| 6867 | Return list of supplemental group IDs for the process. |
| 6868 | [clinic start generated code]*/ |
| 6869 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6870 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6871 | os_getgroups_impl(PyObject *module) |
| 6872 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6873 | { |
| 6874 | PyObject *result = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6875 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6876 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6877 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6878 | * This is a helper variable to store the intermediate result when |
| 6879 | * that happens. |
| 6880 | * |
| 6881 | * To keep the code readable the OSX behaviour is unconditional, |
| 6882 | * according to the POSIX spec this should be safe on all unix-y |
| 6883 | * systems. |
| 6884 | */ |
| 6885 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6886 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6887 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6888 | #ifdef __APPLE__ |
| 6889 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6890 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6891 | * always first call getgroups with length 0 to get the actual number |
| 6892 | * of groups. |
| 6893 | */ |
| 6894 | n = getgroups(0, NULL); |
| 6895 | if (n < 0) { |
| 6896 | return posix_error(); |
| 6897 | } else if (n <= MAX_GROUPS) { |
| 6898 | /* groups will fit in existing array */ |
| 6899 | alt_grouplist = grouplist; |
| 6900 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6901 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6902 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 6903 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6904 | } |
| 6905 | } |
| 6906 | |
| 6907 | n = getgroups(n, alt_grouplist); |
| 6908 | if (n == -1) { |
| 6909 | if (alt_grouplist != grouplist) { |
| 6910 | PyMem_Free(alt_grouplist); |
| 6911 | } |
| 6912 | return posix_error(); |
| 6913 | } |
| 6914 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6915 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6916 | if (n < 0) { |
| 6917 | if (errno == EINVAL) { |
| 6918 | n = getgroups(0, NULL); |
| 6919 | if (n == -1) { |
| 6920 | return posix_error(); |
| 6921 | } |
| 6922 | if (n == 0) { |
| 6923 | /* Avoid malloc(0) */ |
| 6924 | alt_grouplist = grouplist; |
| 6925 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6926 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6927 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 6928 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6929 | } |
| 6930 | n = getgroups(n, alt_grouplist); |
| 6931 | if (n == -1) { |
| 6932 | PyMem_Free(alt_grouplist); |
| 6933 | return posix_error(); |
| 6934 | } |
| 6935 | } |
| 6936 | } else { |
| 6937 | return posix_error(); |
| 6938 | } |
| 6939 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6940 | #endif |
| 6941 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6942 | result = PyList_New(n); |
| 6943 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6944 | int i; |
| 6945 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6946 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6947 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6948 | Py_DECREF(result); |
| 6949 | result = NULL; |
| 6950 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6951 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6952 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6953 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6954 | } |
| 6955 | |
| 6956 | if (alt_grouplist != grouplist) { |
| 6957 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6958 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6959 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6960 | return result; |
| 6961 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6962 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6963 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6964 | #ifdef HAVE_INITGROUPS |
| 6965 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6966 | "initgroups(username, gid) -> None\n\n\ |
| 6967 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6968 | the groups of which the specified username is a member, plus the specified\n\ |
| 6969 | group id."); |
| 6970 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6971 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6972 | static PyObject * |
| 6973 | posix_initgroups(PyObject *self, PyObject *args) |
| 6974 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6975 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6976 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6977 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6978 | #ifdef __APPLE__ |
| 6979 | int gid; |
| 6980 | #else |
| 6981 | gid_t gid; |
| 6982 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6983 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6984 | #ifdef __APPLE__ |
| 6985 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6986 | PyUnicode_FSConverter, &oname, |
| 6987 | &gid)) |
| 6988 | #else |
| 6989 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6990 | PyUnicode_FSConverter, &oname, |
| 6991 | _Py_Gid_Converter, &gid)) |
| 6992 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6993 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6994 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6995 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6996 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6997 | Py_DECREF(oname); |
| 6998 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6999 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7000 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7001 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7002 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7003 | #endif /* HAVE_INITGROUPS */ |
| 7004 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7005 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7006 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7007 | /*[clinic input] |
| 7008 | os.getpgid |
| 7009 | |
| 7010 | pid: pid_t |
| 7011 | |
| 7012 | Call the system call getpgid(), and return the result. |
| 7013 | [clinic start generated code]*/ |
| 7014 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7015 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7016 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 7017 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7018 | { |
| 7019 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7020 | if (pgid < 0) |
| 7021 | return posix_error(); |
| 7022 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7023 | } |
| 7024 | #endif /* HAVE_GETPGID */ |
| 7025 | |
| 7026 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7027 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7028 | /*[clinic input] |
| 7029 | os.getpgrp |
| 7030 | |
| 7031 | Return the current process group id. |
| 7032 | [clinic start generated code]*/ |
| 7033 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7034 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7035 | os_getpgrp_impl(PyObject *module) |
| 7036 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7037 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7038 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7039 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7040 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7041 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7042 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7043 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7044 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7045 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7046 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7047 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7048 | /*[clinic input] |
| 7049 | os.setpgrp |
| 7050 | |
| 7051 | Make the current process the leader of its process group. |
| 7052 | [clinic start generated code]*/ |
| 7053 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7054 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7055 | os_setpgrp_impl(PyObject *module) |
| 7056 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7057 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7058 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7059 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7060 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7061 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7062 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7063 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7064 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7065 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7066 | #endif /* HAVE_SETPGRP */ |
| 7067 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7068 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7069 | |
| 7070 | #ifdef MS_WINDOWS |
| 7071 | #include <tlhelp32.h> |
| 7072 | |
| 7073 | static PyObject* |
| 7074 | win32_getppid() |
| 7075 | { |
| 7076 | HANDLE snapshot; |
| 7077 | pid_t mypid; |
| 7078 | PyObject* result = NULL; |
| 7079 | BOOL have_record; |
| 7080 | PROCESSENTRY32 pe; |
| 7081 | |
| 7082 | mypid = getpid(); /* This function never fails */ |
| 7083 | |
| 7084 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 7085 | if (snapshot == INVALID_HANDLE_VALUE) |
| 7086 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 7087 | |
| 7088 | pe.dwSize = sizeof(pe); |
| 7089 | have_record = Process32First(snapshot, &pe); |
| 7090 | while (have_record) { |
| 7091 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 7092 | /* We could cache the ulong value in a static variable. */ |
| 7093 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 7094 | break; |
| 7095 | } |
| 7096 | |
| 7097 | have_record = Process32Next(snapshot, &pe); |
| 7098 | } |
| 7099 | |
| 7100 | /* If our loop exits and our pid was not found (result will be NULL) |
| 7101 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 7102 | * error anyway, so let's raise it. */ |
| 7103 | if (!result) |
| 7104 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7105 | |
| 7106 | CloseHandle(snapshot); |
| 7107 | |
| 7108 | return result; |
| 7109 | } |
| 7110 | #endif /*MS_WINDOWS*/ |
| 7111 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7112 | |
| 7113 | /*[clinic input] |
| 7114 | os.getppid |
| 7115 | |
| 7116 | Return the parent's process id. |
| 7117 | |
| 7118 | If the parent process has already exited, Windows machines will still |
| 7119 | return its id; others systems will return the id of the 'init' process (1). |
| 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_getppid_impl(PyObject *module) |
| 7124 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7125 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7126 | #ifdef MS_WINDOWS |
| 7127 | return win32_getppid(); |
| 7128 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7129 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7130 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7131 | } |
| 7132 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7133 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7134 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7135 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7136 | /*[clinic input] |
| 7137 | os.getlogin |
| 7138 | |
| 7139 | Return the actual login name. |
| 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_getlogin_impl(PyObject *module) |
| 7144 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7145 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7146 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7147 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7148 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7149 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7150 | |
| 7151 | if (GetUserNameW(user_name, &num_chars)) { |
| 7152 | /* num_chars is the number of unicode chars plus null terminator */ |
| 7153 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7154 | } |
| 7155 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7156 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7157 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7158 | char *name; |
| 7159 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7160 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7161 | errno = 0; |
| 7162 | name = getlogin(); |
| 7163 | if (name == NULL) { |
| 7164 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7165 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7166 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7167 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7168 | } |
| 7169 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7170 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7171 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7172 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7173 | return result; |
| 7174 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7175 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7176 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7177 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7178 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7179 | /*[clinic input] |
| 7180 | os.getuid |
| 7181 | |
| 7182 | Return the current process's user id. |
| 7183 | [clinic start generated code]*/ |
| 7184 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7185 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7186 | os_getuid_impl(PyObject *module) |
| 7187 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7188 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7189 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7190 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7191 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7192 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7193 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7194 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7195 | #define HAVE_KILL |
| 7196 | #endif /* MS_WINDOWS */ |
| 7197 | |
| 7198 | #ifdef HAVE_KILL |
| 7199 | /*[clinic input] |
| 7200 | os.kill |
| 7201 | |
| 7202 | pid: pid_t |
| 7203 | signal: Py_ssize_t |
| 7204 | / |
| 7205 | |
| 7206 | Kill a process with a signal. |
| 7207 | [clinic start generated code]*/ |
| 7208 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7209 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7210 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 7211 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7212 | #ifndef MS_WINDOWS |
| 7213 | { |
| 7214 | if (kill(pid, (int)signal) == -1) |
| 7215 | return posix_error(); |
| 7216 | Py_RETURN_NONE; |
| 7217 | } |
| 7218 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7219 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7220 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7221 | DWORD sig = (DWORD)signal; |
| 7222 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7223 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7224 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7225 | /* Console processes which share a common console can be sent CTRL+C or |
| 7226 | CTRL+BREAK events, provided they handle said events. */ |
| 7227 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7228 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7229 | err = GetLastError(); |
| 7230 | PyErr_SetFromWindowsErr(err); |
| 7231 | } |
| 7232 | else |
| 7233 | Py_RETURN_NONE; |
| 7234 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7235 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7236 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 7237 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7238 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7239 | if (handle == NULL) { |
| 7240 | err = GetLastError(); |
| 7241 | return PyErr_SetFromWindowsErr(err); |
| 7242 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7243 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7244 | if (TerminateProcess(handle, sig) == 0) { |
| 7245 | err = GetLastError(); |
| 7246 | result = PyErr_SetFromWindowsErr(err); |
| 7247 | } else { |
| 7248 | Py_INCREF(Py_None); |
| 7249 | result = Py_None; |
| 7250 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7251 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7252 | CloseHandle(handle); |
| 7253 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7254 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7255 | #endif /* !MS_WINDOWS */ |
| 7256 | #endif /* HAVE_KILL */ |
| 7257 | |
| 7258 | |
| 7259 | #ifdef HAVE_KILLPG |
| 7260 | /*[clinic input] |
| 7261 | os.killpg |
| 7262 | |
| 7263 | pgid: pid_t |
| 7264 | signal: int |
| 7265 | / |
| 7266 | |
| 7267 | Kill a process group with a signal. |
| 7268 | [clinic start generated code]*/ |
| 7269 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7270 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7271 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7272 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7273 | { |
| 7274 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7275 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7276 | take the same type. Moreover, pid_t is always at least as wide as |
| 7277 | int (else compilation of this module fails), which is safe. */ |
| 7278 | if (killpg(pgid, signal) == -1) |
| 7279 | return posix_error(); |
| 7280 | Py_RETURN_NONE; |
| 7281 | } |
| 7282 | #endif /* HAVE_KILLPG */ |
| 7283 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7284 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7285 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7286 | #ifdef HAVE_SYS_LOCK_H |
| 7287 | #include <sys/lock.h> |
| 7288 | #endif |
| 7289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7290 | /*[clinic input] |
| 7291 | os.plock |
| 7292 | op: int |
| 7293 | / |
| 7294 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7295 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7296 | [clinic start generated code]*/ |
| 7297 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7298 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7299 | os_plock_impl(PyObject *module, int op) |
| 7300 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7301 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7302 | if (plock(op) == -1) |
| 7303 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7304 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7305 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7306 | #endif /* HAVE_PLOCK */ |
| 7307 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7308 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7309 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7310 | /*[clinic input] |
| 7311 | os.setuid |
| 7312 | |
| 7313 | uid: uid_t |
| 7314 | / |
| 7315 | |
| 7316 | Set the current process's user id. |
| 7317 | [clinic start generated code]*/ |
| 7318 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7319 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7320 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7321 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7322 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7323 | if (setuid(uid) < 0) |
| 7324 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7325 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7326 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7327 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7328 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7329 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7330 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7331 | /*[clinic input] |
| 7332 | os.seteuid |
| 7333 | |
| 7334 | euid: uid_t |
| 7335 | / |
| 7336 | |
| 7337 | Set the current process's effective user id. |
| 7338 | [clinic start generated code]*/ |
| 7339 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7340 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7341 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7342 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7343 | { |
| 7344 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7345 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7346 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7347 | } |
| 7348 | #endif /* HAVE_SETEUID */ |
| 7349 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7350 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7351 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7352 | /*[clinic input] |
| 7353 | os.setegid |
| 7354 | |
| 7355 | egid: gid_t |
| 7356 | / |
| 7357 | |
| 7358 | Set the current process's effective group id. |
| 7359 | [clinic start generated code]*/ |
| 7360 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7361 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7362 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7363 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7364 | { |
| 7365 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7366 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7367 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7368 | } |
| 7369 | #endif /* HAVE_SETEGID */ |
| 7370 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7371 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7372 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7373 | /*[clinic input] |
| 7374 | os.setreuid |
| 7375 | |
| 7376 | ruid: uid_t |
| 7377 | euid: uid_t |
| 7378 | / |
| 7379 | |
| 7380 | Set the current process's real and effective user ids. |
| 7381 | [clinic start generated code]*/ |
| 7382 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7383 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7384 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 7385 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7386 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7387 | if (setreuid(ruid, euid) < 0) { |
| 7388 | return posix_error(); |
| 7389 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7390 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7391 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7392 | } |
| 7393 | #endif /* HAVE_SETREUID */ |
| 7394 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7395 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7396 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7397 | /*[clinic input] |
| 7398 | os.setregid |
| 7399 | |
| 7400 | rgid: gid_t |
| 7401 | egid: gid_t |
| 7402 | / |
| 7403 | |
| 7404 | Set the current process's real and effective group ids. |
| 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_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 7409 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7410 | { |
| 7411 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7412 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7413 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7414 | } |
| 7415 | #endif /* HAVE_SETREGID */ |
| 7416 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7417 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7418 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7419 | /*[clinic input] |
| 7420 | os.setgid |
| 7421 | gid: gid_t |
| 7422 | / |
| 7423 | |
| 7424 | Set the current process's group id. |
| 7425 | [clinic start generated code]*/ |
| 7426 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7427 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7428 | os_setgid_impl(PyObject *module, gid_t gid) |
| 7429 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7430 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7431 | if (setgid(gid) < 0) |
| 7432 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7433 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7434 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7435 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7436 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7437 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7438 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7439 | /*[clinic input] |
| 7440 | os.setgroups |
| 7441 | |
| 7442 | groups: object |
| 7443 | / |
| 7444 | |
| 7445 | Set the groups of the current process to list. |
| 7446 | [clinic start generated code]*/ |
| 7447 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7448 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7449 | os_setgroups(PyObject *module, PyObject *groups) |
| 7450 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7451 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7452 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7453 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7454 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7455 | if (!PySequence_Check(groups)) { |
| 7456 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 7457 | return NULL; |
| 7458 | } |
| 7459 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7460 | if (len < 0) { |
| 7461 | return NULL; |
| 7462 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7463 | if (len > MAX_GROUPS) { |
| 7464 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7465 | return NULL; |
| 7466 | } |
| 7467 | for(i = 0; i < len; i++) { |
| 7468 | PyObject *elem; |
| 7469 | elem = PySequence_GetItem(groups, i); |
| 7470 | if (!elem) |
| 7471 | return NULL; |
| 7472 | if (!PyLong_Check(elem)) { |
| 7473 | PyErr_SetString(PyExc_TypeError, |
| 7474 | "groups must be integers"); |
| 7475 | Py_DECREF(elem); |
| 7476 | return NULL; |
| 7477 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7478 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7479 | Py_DECREF(elem); |
| 7480 | return NULL; |
| 7481 | } |
| 7482 | } |
| 7483 | Py_DECREF(elem); |
| 7484 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7485 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7486 | if (setgroups(len, grouplist) < 0) |
| 7487 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7488 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7489 | } |
| 7490 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7491 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7492 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7493 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7494 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7495 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7496 | PyObject *result; |
| 7497 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 7498 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7499 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7500 | if (pid == -1) |
| 7501 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7502 | |
Zackery Spytz | 682107c | 2019-09-09 09:48:32 -0600 | [diff] [blame] | 7503 | // If wait succeeded but no child was ready to report status, ru will not |
| 7504 | // have been populated. |
| 7505 | if (pid == 0) { |
| 7506 | memset(ru, 0, sizeof(*ru)); |
| 7507 | } |
| 7508 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7509 | if (struct_rusage == NULL) { |
| 7510 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7511 | if (m == NULL) |
| 7512 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 7513 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7514 | Py_DECREF(m); |
| 7515 | if (struct_rusage == NULL) |
| 7516 | return NULL; |
| 7517 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7518 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7519 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7520 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 7521 | if (!result) |
| 7522 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7523 | |
| 7524 | #ifndef doubletime |
| 7525 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7526 | #endif |
| 7527 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7528 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7529 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7530 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7531 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7532 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7533 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7534 | SET_INT(result, 2, ru->ru_maxrss); |
| 7535 | SET_INT(result, 3, ru->ru_ixrss); |
| 7536 | SET_INT(result, 4, ru->ru_idrss); |
| 7537 | SET_INT(result, 5, ru->ru_isrss); |
| 7538 | SET_INT(result, 6, ru->ru_minflt); |
| 7539 | SET_INT(result, 7, ru->ru_majflt); |
| 7540 | SET_INT(result, 8, ru->ru_nswap); |
| 7541 | SET_INT(result, 9, ru->ru_inblock); |
| 7542 | SET_INT(result, 10, ru->ru_oublock); |
| 7543 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7544 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7545 | SET_INT(result, 13, ru->ru_nsignals); |
| 7546 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7547 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7548 | #undef SET_INT |
| 7549 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7550 | if (PyErr_Occurred()) { |
| 7551 | Py_DECREF(result); |
| 7552 | return NULL; |
| 7553 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7554 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7555 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7556 | } |
| 7557 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7558 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7559 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7560 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7561 | /*[clinic input] |
| 7562 | os.wait3 |
| 7563 | |
| 7564 | options: int |
| 7565 | Wait for completion of a child process. |
| 7566 | |
| 7567 | Returns a tuple of information about the child process: |
| 7568 | (pid, status, rusage) |
| 7569 | [clinic start generated code]*/ |
| 7570 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7571 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7572 | os_wait3_impl(PyObject *module, int options) |
| 7573 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7574 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7575 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7576 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7577 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7578 | WAIT_TYPE status; |
| 7579 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7580 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7581 | do { |
| 7582 | Py_BEGIN_ALLOW_THREADS |
| 7583 | pid = wait3(&status, options, &ru); |
| 7584 | Py_END_ALLOW_THREADS |
| 7585 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7586 | if (pid < 0) |
| 7587 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7588 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7589 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7590 | } |
| 7591 | #endif /* HAVE_WAIT3 */ |
| 7592 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7593 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7594 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7595 | /*[clinic input] |
| 7596 | |
| 7597 | os.wait4 |
| 7598 | |
| 7599 | pid: pid_t |
| 7600 | options: int |
| 7601 | |
| 7602 | Wait for completion of a specific child process. |
| 7603 | |
| 7604 | Returns a tuple of information about the child process: |
| 7605 | (pid, status, rusage) |
| 7606 | [clinic start generated code]*/ |
| 7607 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7608 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7609 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7610 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7611 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7612 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7613 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7614 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7615 | WAIT_TYPE status; |
| 7616 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7617 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7618 | do { |
| 7619 | Py_BEGIN_ALLOW_THREADS |
| 7620 | res = wait4(pid, &status, options, &ru); |
| 7621 | Py_END_ALLOW_THREADS |
| 7622 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7623 | if (res < 0) |
| 7624 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7625 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7626 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7627 | } |
| 7628 | #endif /* HAVE_WAIT4 */ |
| 7629 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7630 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7631 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7632 | /*[clinic input] |
| 7633 | os.waitid |
| 7634 | |
| 7635 | idtype: idtype_t |
| 7636 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7637 | id: id_t |
| 7638 | The id to wait on. |
| 7639 | options: int |
| 7640 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7641 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7642 | / |
| 7643 | |
| 7644 | Returns the result of waiting for a process or processes. |
| 7645 | |
| 7646 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7647 | no children in a waitable state. |
| 7648 | [clinic start generated code]*/ |
| 7649 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7650 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7651 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7652 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7653 | { |
| 7654 | PyObject *result; |
| 7655 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7656 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7657 | siginfo_t si; |
| 7658 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7659 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7660 | do { |
| 7661 | Py_BEGIN_ALLOW_THREADS |
| 7662 | res = waitid(idtype, id, &si, options); |
| 7663 | Py_END_ALLOW_THREADS |
| 7664 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7665 | if (res < 0) |
| 7666 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7667 | |
| 7668 | if (si.si_pid == 0) |
| 7669 | Py_RETURN_NONE; |
| 7670 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 7671 | result = PyStructSequence_New(WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7672 | if (!result) |
| 7673 | return NULL; |
| 7674 | |
| 7675 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7676 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7677 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7678 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7679 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7680 | if (PyErr_Occurred()) { |
| 7681 | Py_DECREF(result); |
| 7682 | return NULL; |
| 7683 | } |
| 7684 | |
| 7685 | return result; |
| 7686 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7687 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7688 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7689 | |
| 7690 | #if defined(HAVE_WAITPID) |
| 7691 | /*[clinic input] |
| 7692 | os.waitpid |
| 7693 | pid: pid_t |
| 7694 | options: int |
| 7695 | / |
| 7696 | |
| 7697 | Wait for completion of a given child process. |
| 7698 | |
| 7699 | Returns a tuple of information regarding the child process: |
| 7700 | (pid, status) |
| 7701 | |
| 7702 | The options argument is ignored on Windows. |
| 7703 | [clinic start generated code]*/ |
| 7704 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7705 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7706 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7707 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7708 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7709 | pid_t res; |
| 7710 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7711 | WAIT_TYPE status; |
| 7712 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7713 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7714 | do { |
| 7715 | Py_BEGIN_ALLOW_THREADS |
| 7716 | res = waitpid(pid, &status, options); |
| 7717 | Py_END_ALLOW_THREADS |
| 7718 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7719 | if (res < 0) |
| 7720 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7721 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7722 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7723 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7724 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7725 | /* 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] | 7726 | /*[clinic input] |
| 7727 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7728 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7729 | options: int |
| 7730 | / |
| 7731 | |
| 7732 | Wait for completion of a given process. |
| 7733 | |
| 7734 | Returns a tuple of information regarding the process: |
| 7735 | (pid, status << 8) |
| 7736 | |
| 7737 | The options argument is ignored on Windows. |
| 7738 | [clinic start generated code]*/ |
| 7739 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7740 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7741 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7742 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7743 | { |
| 7744 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7745 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7746 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7747 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7748 | do { |
| 7749 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7750 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7751 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7752 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7753 | Py_END_ALLOW_THREADS |
| 7754 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7755 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7756 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7757 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7758 | /* 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] | 7759 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7760 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7761 | #endif |
| 7762 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7763 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7764 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7765 | /*[clinic input] |
| 7766 | os.wait |
| 7767 | |
| 7768 | Wait for completion of a child process. |
| 7769 | |
| 7770 | Returns a tuple of information about the child process: |
| 7771 | (pid, status) |
| 7772 | [clinic start generated code]*/ |
| 7773 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7774 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7775 | os_wait_impl(PyObject *module) |
| 7776 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7777 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7778 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7779 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7780 | WAIT_TYPE status; |
| 7781 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7782 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7783 | do { |
| 7784 | Py_BEGIN_ALLOW_THREADS |
| 7785 | pid = wait(&status); |
| 7786 | Py_END_ALLOW_THREADS |
| 7787 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7788 | if (pid < 0) |
| 7789 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7790 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7791 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7792 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7793 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7794 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7795 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7796 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7797 | /*[clinic input] |
| 7798 | os.readlink |
| 7799 | |
| 7800 | path: path_t |
| 7801 | * |
| 7802 | dir_fd: dir_fd(requires='readlinkat') = None |
| 7803 | |
| 7804 | Return a string representing the path to which the symbolic link points. |
| 7805 | |
| 7806 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7807 | and path should be relative; path will then be relative to that directory. |
| 7808 | |
| 7809 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 7810 | using it will raise a NotImplementedError. |
| 7811 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7812 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7813 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7814 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 7815 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7816 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7817 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 7818 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7819 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7820 | |
| 7821 | Py_BEGIN_ALLOW_THREADS |
| 7822 | #ifdef HAVE_READLINKAT |
| 7823 | if (dir_fd != DEFAULT_DIR_FD) |
| 7824 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 7825 | else |
| 7826 | #endif |
| 7827 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 7828 | Py_END_ALLOW_THREADS |
| 7829 | |
| 7830 | if (length < 0) { |
| 7831 | return path_error(path); |
| 7832 | } |
| 7833 | buffer[length] = '\0'; |
| 7834 | |
| 7835 | if (PyUnicode_Check(path->object)) |
| 7836 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7837 | else |
| 7838 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7839 | #elif defined(MS_WINDOWS) |
| 7840 | DWORD n_bytes_returned; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7841 | DWORD io_result = 0; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7842 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7843 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7844 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 7845 | PyObject *result = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7847 | /* First get a handle to the reparse point */ |
| 7848 | Py_BEGIN_ALLOW_THREADS |
| 7849 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7850 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7851 | 0, |
| 7852 | 0, |
| 7853 | 0, |
| 7854 | OPEN_EXISTING, |
| 7855 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7856 | 0); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7857 | if (reparse_point_handle != INVALID_HANDLE_VALUE) { |
| 7858 | /* New call DeviceIoControl to read the reparse point */ |
| 7859 | io_result = DeviceIoControl( |
| 7860 | reparse_point_handle, |
| 7861 | FSCTL_GET_REPARSE_POINT, |
| 7862 | 0, 0, /* in buffer */ |
| 7863 | target_buffer, sizeof(target_buffer), |
| 7864 | &n_bytes_returned, |
| 7865 | 0 /* we're not using OVERLAPPED_IO */ |
| 7866 | ); |
| 7867 | CloseHandle(reparse_point_handle); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7868 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7869 | Py_END_ALLOW_THREADS |
| 7870 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7871 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7872 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7873 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7874 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7875 | wchar_t *name = NULL; |
| 7876 | Py_ssize_t nameLen = 0; |
| 7877 | if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7878 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7879 | name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7880 | rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 7881 | nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7882 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7883 | else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) |
| 7884 | { |
| 7885 | name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer + |
| 7886 | rdb->MountPointReparseBuffer.SubstituteNameOffset); |
| 7887 | nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
| 7888 | } |
| 7889 | else |
| 7890 | { |
| 7891 | PyErr_SetString(PyExc_ValueError, "not a symbolic link"); |
| 7892 | } |
| 7893 | if (name) { |
| 7894 | if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) { |
| 7895 | /* Our buffer is mutable, so this is okay */ |
| 7896 | name[1] = L'\\'; |
| 7897 | } |
| 7898 | result = PyUnicode_FromWideChar(name, nameLen); |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 7899 | if (result && path->narrow) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7900 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
| 7901 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7902 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7903 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7904 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7905 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7906 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7907 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7908 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7909 | |
| 7910 | #if defined(MS_WINDOWS) |
| 7911 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7912 | /* Remove the last portion of the path - return 0 on success */ |
| 7913 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7914 | _dirnameW(WCHAR *path) |
| 7915 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7916 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7917 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 7918 | if (length == MAX_PATH) { |
| 7919 | return -1; |
| 7920 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7921 | |
| 7922 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7923 | for(ptr = path + length; ptr != path; ptr--) { |
| 7924 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7925 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7926 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7927 | } |
| 7928 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7929 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7930 | } |
| 7931 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7932 | /* Is this path absolute? */ |
| 7933 | static int |
| 7934 | _is_absW(const WCHAR *path) |
| 7935 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7936 | return path[0] == L'\\' || path[0] == L'/' || |
| 7937 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7938 | } |
| 7939 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7940 | /* join root and rest with a backslash - return 0 on success */ |
| 7941 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7942 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7943 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7944 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7945 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7946 | } |
| 7947 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7948 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 7949 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7950 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7951 | |
| 7952 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 7953 | return -1; |
| 7954 | } |
| 7955 | |
| 7956 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7957 | } |
| 7958 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7959 | /* Return True if the path at src relative to dest is a directory */ |
| 7960 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7961 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7962 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7963 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7964 | WCHAR dest_parent[MAX_PATH]; |
| 7965 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7966 | |
| 7967 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7968 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 7969 | _dirnameW(dest_parent)) { |
| 7970 | return 0; |
| 7971 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7972 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7973 | if (_joinW(src_resolved, dest_parent, src)) { |
| 7974 | return 0; |
| 7975 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7976 | return ( |
| 7977 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7978 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7979 | ); |
| 7980 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7981 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7982 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7983 | |
| 7984 | /*[clinic input] |
| 7985 | os.symlink |
| 7986 | src: path_t |
| 7987 | dst: path_t |
| 7988 | target_is_directory: bool = False |
| 7989 | * |
| 7990 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7991 | |
| 7992 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7993 | |
| 7994 | Create a symbolic link pointing to src named dst. |
| 7995 | |
| 7996 | target_is_directory is required on Windows if the target is to be |
| 7997 | interpreted as a directory. (On Windows, symlink requires |
| 7998 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7999 | target_is_directory is ignored on non-Windows platforms. |
| 8000 | |
| 8001 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8002 | and path should be relative; path will then be relative to that directory. |
| 8003 | dir_fd may not be implemented on your platform. |
| 8004 | If it is unavailable, using it will raise a NotImplementedError. |
| 8005 | |
| 8006 | [clinic start generated code]*/ |
| 8007 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8008 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8009 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8010 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8011 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8012 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8013 | #ifdef MS_WINDOWS |
| 8014 | DWORD result; |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8015 | DWORD flags = 0; |
| 8016 | |
| 8017 | /* Assumed true, set to false if detected to not be available. */ |
| 8018 | static int windows_has_symlink_unprivileged_flag = TRUE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8019 | #else |
| 8020 | int result; |
| 8021 | #endif |
| 8022 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8023 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8024 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8025 | if (windows_has_symlink_unprivileged_flag) { |
| 8026 | /* Allow non-admin symlinks if system allows it. */ |
| 8027 | flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
| 8028 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8029 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8030 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8031 | _Py_BEGIN_SUPPRESS_IPH |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8032 | /* if src is a directory, ensure flags==1 (target_is_directory bit) */ |
| 8033 | if (target_is_directory || _check_dirW(src->wide, dst->wide)) { |
| 8034 | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 8035 | } |
| 8036 | |
| 8037 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8038 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8039 | Py_END_ALLOW_THREADS |
| 8040 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8041 | if (windows_has_symlink_unprivileged_flag && !result && |
| 8042 | ERROR_INVALID_PARAMETER == GetLastError()) { |
| 8043 | |
| 8044 | Py_BEGIN_ALLOW_THREADS |
| 8045 | _Py_BEGIN_SUPPRESS_IPH |
| 8046 | /* This error might be caused by |
| 8047 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported. |
| 8048 | Try again, and update windows_has_symlink_unprivileged_flag if we |
| 8049 | are successful this time. |
| 8050 | |
| 8051 | NOTE: There is a risk of a race condition here if there are other |
| 8052 | conditions than the flag causing ERROR_INVALID_PARAMETER, and |
| 8053 | another process (or thread) changes that condition in between our |
| 8054 | calls to CreateSymbolicLink. |
| 8055 | */ |
| 8056 | flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 8057 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
| 8058 | _Py_END_SUPPRESS_IPH |
| 8059 | Py_END_ALLOW_THREADS |
| 8060 | |
| 8061 | if (result || ERROR_INVALID_PARAMETER != GetLastError()) { |
| 8062 | windows_has_symlink_unprivileged_flag = FALSE; |
| 8063 | } |
| 8064 | } |
| 8065 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8066 | if (!result) |
| 8067 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8068 | |
| 8069 | #else |
| 8070 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8071 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 8072 | PyErr_SetString(PyExc_ValueError, |
| 8073 | "symlink: src and dst must be the same type"); |
| 8074 | return NULL; |
| 8075 | } |
| 8076 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8077 | Py_BEGIN_ALLOW_THREADS |
| 8078 | #if HAVE_SYMLINKAT |
| 8079 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8080 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8081 | else |
| 8082 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8083 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8084 | Py_END_ALLOW_THREADS |
| 8085 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8086 | if (result) |
| 8087 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8088 | #endif |
| 8089 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8090 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8091 | } |
| 8092 | #endif /* HAVE_SYMLINK */ |
| 8093 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8094 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8095 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8096 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8097 | static PyStructSequence_Field times_result_fields[] = { |
| 8098 | {"user", "user time"}, |
| 8099 | {"system", "system time"}, |
| 8100 | {"children_user", "user time of children"}, |
| 8101 | {"children_system", "system time of children"}, |
| 8102 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 8103 | {NULL} |
| 8104 | }; |
| 8105 | |
| 8106 | PyDoc_STRVAR(times_result__doc__, |
| 8107 | "times_result: Result from os.times().\n\n\ |
| 8108 | This object may be accessed either as a tuple of\n\ |
| 8109 | (user, system, children_user, children_system, elapsed),\n\ |
| 8110 | or via the attributes user, system, children_user, children_system,\n\ |
| 8111 | and elapsed.\n\ |
| 8112 | \n\ |
| 8113 | See os.times for more information."); |
| 8114 | |
| 8115 | static PyStructSequence_Desc times_result_desc = { |
| 8116 | "times_result", /* name */ |
| 8117 | times_result__doc__, /* doc */ |
| 8118 | times_result_fields, |
| 8119 | 5 |
| 8120 | }; |
| 8121 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 8122 | static PyTypeObject* TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8123 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8124 | #ifdef MS_WINDOWS |
| 8125 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 8126 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8127 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8128 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8129 | |
| 8130 | static PyObject * |
| 8131 | build_times_result(double user, double system, |
| 8132 | double children_user, double children_system, |
| 8133 | double elapsed) |
| 8134 | { |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 8135 | PyObject *value = PyStructSequence_New(TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8136 | if (value == NULL) |
| 8137 | return NULL; |
| 8138 | |
| 8139 | #define SET(i, field) \ |
| 8140 | { \ |
| 8141 | PyObject *o = PyFloat_FromDouble(field); \ |
| 8142 | if (!o) { \ |
| 8143 | Py_DECREF(value); \ |
| 8144 | return NULL; \ |
| 8145 | } \ |
| 8146 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 8147 | } \ |
| 8148 | |
| 8149 | SET(0, user); |
| 8150 | SET(1, system); |
| 8151 | SET(2, children_user); |
| 8152 | SET(3, children_system); |
| 8153 | SET(4, elapsed); |
| 8154 | |
| 8155 | #undef SET |
| 8156 | |
| 8157 | return value; |
| 8158 | } |
| 8159 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8160 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8161 | #ifndef MS_WINDOWS |
| 8162 | #define NEED_TICKS_PER_SECOND |
| 8163 | static long ticks_per_second = -1; |
| 8164 | #endif /* MS_WINDOWS */ |
| 8165 | |
| 8166 | /*[clinic input] |
| 8167 | os.times |
| 8168 | |
| 8169 | Return a collection containing process timing information. |
| 8170 | |
| 8171 | The object returned behaves like a named tuple with these fields: |
| 8172 | (utime, stime, cutime, cstime, elapsed_time) |
| 8173 | All fields are floating point numbers. |
| 8174 | [clinic start generated code]*/ |
| 8175 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8176 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8177 | os_times_impl(PyObject *module) |
| 8178 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8179 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8180 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8181 | FILETIME create, exit, kernel, user; |
| 8182 | HANDLE hProc; |
| 8183 | hProc = GetCurrentProcess(); |
| 8184 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 8185 | /* The fields of a FILETIME structure are the hi and lo part |
| 8186 | of a 64-bit value expressed in 100 nanosecond units. |
| 8187 | 1e7 is one second in such units; 1e-7 the inverse. |
| 8188 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 8189 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8190 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8191 | (double)(user.dwHighDateTime*429.4967296 + |
| 8192 | user.dwLowDateTime*1e-7), |
| 8193 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 8194 | kernel.dwLowDateTime*1e-7), |
| 8195 | (double)0, |
| 8196 | (double)0, |
| 8197 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8198 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8199 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8200 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8201 | |
| 8202 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8203 | struct tms t; |
| 8204 | clock_t c; |
| 8205 | errno = 0; |
| 8206 | c = times(&t); |
| 8207 | if (c == (clock_t) -1) |
| 8208 | return posix_error(); |
| 8209 | return build_times_result( |
| 8210 | (double)t.tms_utime / ticks_per_second, |
| 8211 | (double)t.tms_stime / ticks_per_second, |
| 8212 | (double)t.tms_cutime / ticks_per_second, |
| 8213 | (double)t.tms_cstime / ticks_per_second, |
| 8214 | (double)c / ticks_per_second); |
| 8215 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8216 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8217 | #endif /* HAVE_TIMES */ |
| 8218 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8219 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8220 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8221 | /*[clinic input] |
| 8222 | os.getsid |
| 8223 | |
| 8224 | pid: pid_t |
| 8225 | / |
| 8226 | |
| 8227 | Call the system call getsid(pid) and return the result. |
| 8228 | [clinic start generated code]*/ |
| 8229 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8230 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8231 | os_getsid_impl(PyObject *module, pid_t pid) |
| 8232 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8233 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8234 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8235 | sid = getsid(pid); |
| 8236 | if (sid < 0) |
| 8237 | return posix_error(); |
| 8238 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8239 | } |
| 8240 | #endif /* HAVE_GETSID */ |
| 8241 | |
| 8242 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8243 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8244 | /*[clinic input] |
| 8245 | os.setsid |
| 8246 | |
| 8247 | Call the system call setsid(). |
| 8248 | [clinic start generated code]*/ |
| 8249 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8250 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8251 | os_setsid_impl(PyObject *module) |
| 8252 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8253 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8254 | if (setsid() < 0) |
| 8255 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8256 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8257 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8258 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8259 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8260 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8261 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8262 | /*[clinic input] |
| 8263 | os.setpgid |
| 8264 | |
| 8265 | pid: pid_t |
| 8266 | pgrp: pid_t |
| 8267 | / |
| 8268 | |
| 8269 | Call the system call setpgid(pid, pgrp). |
| 8270 | [clinic start generated code]*/ |
| 8271 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8272 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8273 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 8274 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8275 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8276 | if (setpgid(pid, pgrp) < 0) |
| 8277 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8278 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8279 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8280 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8281 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8282 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8283 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8284 | /*[clinic input] |
| 8285 | os.tcgetpgrp |
| 8286 | |
| 8287 | fd: int |
| 8288 | / |
| 8289 | |
| 8290 | Return the process group associated with the terminal specified by fd. |
| 8291 | [clinic start generated code]*/ |
| 8292 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8293 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8294 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8295 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8296 | { |
| 8297 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8298 | if (pgid < 0) |
| 8299 | return posix_error(); |
| 8300 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8301 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8302 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8303 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8304 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8305 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8306 | /*[clinic input] |
| 8307 | os.tcsetpgrp |
| 8308 | |
| 8309 | fd: int |
| 8310 | pgid: pid_t |
| 8311 | / |
| 8312 | |
| 8313 | Set the process group associated with the terminal specified by fd. |
| 8314 | [clinic start generated code]*/ |
| 8315 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8316 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8317 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8318 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8319 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8320 | if (tcsetpgrp(fd, pgid) < 0) |
| 8321 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8322 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8323 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8324 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8325 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8326 | /* Functions acting on file descriptors */ |
| 8327 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8328 | #ifdef O_CLOEXEC |
| 8329 | extern int _Py_open_cloexec_works; |
| 8330 | #endif |
| 8331 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8332 | |
| 8333 | /*[clinic input] |
| 8334 | os.open -> int |
| 8335 | path: path_t |
| 8336 | flags: int |
| 8337 | mode: int = 0o777 |
| 8338 | * |
| 8339 | dir_fd: dir_fd(requires='openat') = None |
| 8340 | |
| 8341 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 8342 | |
| 8343 | Open a file for low level IO. Returns a file descriptor (integer). |
| 8344 | |
| 8345 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8346 | and path should be relative; path will then be relative to that directory. |
| 8347 | dir_fd may not be implemented on your platform. |
| 8348 | If it is unavailable, using it will raise a NotImplementedError. |
| 8349 | [clinic start generated code]*/ |
| 8350 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8351 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8352 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 8353 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8354 | { |
| 8355 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8356 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8357 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8358 | #ifdef O_CLOEXEC |
| 8359 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 8360 | #elif !defined(MS_WINDOWS) |
| 8361 | int *atomic_flag_works = NULL; |
| 8362 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8363 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8364 | #ifdef MS_WINDOWS |
| 8365 | flags |= O_NOINHERIT; |
| 8366 | #elif defined(O_CLOEXEC) |
| 8367 | flags |= O_CLOEXEC; |
| 8368 | #endif |
| 8369 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 8370 | if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) { |
| 8371 | return -1; |
| 8372 | } |
| 8373 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8374 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8375 | do { |
| 8376 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8377 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8378 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8379 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8380 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8381 | if (dir_fd != DEFAULT_DIR_FD) |
| 8382 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 8383 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8384 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8385 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8386 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8387 | Py_END_ALLOW_THREADS |
| 8388 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8389 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8390 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8391 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8392 | if (!async_err) |
| 8393 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8394 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8395 | } |
| 8396 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8397 | #ifndef MS_WINDOWS |
| 8398 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 8399 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8400 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8401 | } |
| 8402 | #endif |
| 8403 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8404 | return fd; |
| 8405 | } |
| 8406 | |
| 8407 | |
| 8408 | /*[clinic input] |
| 8409 | os.close |
| 8410 | |
| 8411 | fd: int |
| 8412 | |
| 8413 | Close a file descriptor. |
| 8414 | [clinic start generated code]*/ |
| 8415 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8416 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8417 | os_close_impl(PyObject *module, int fd) |
| 8418 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8419 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8420 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8421 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 8422 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 8423 | * for more details. |
| 8424 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8425 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8426 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8427 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8428 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8429 | Py_END_ALLOW_THREADS |
| 8430 | if (res < 0) |
| 8431 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8432 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8433 | } |
| 8434 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8435 | |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8436 | #ifdef HAVE_FDWALK |
| 8437 | static int |
| 8438 | _fdwalk_close_func(void *lohi, int fd) |
| 8439 | { |
| 8440 | int lo = ((int *)lohi)[0]; |
| 8441 | int hi = ((int *)lohi)[1]; |
| 8442 | |
| 8443 | if (fd >= hi) |
| 8444 | return 1; |
| 8445 | else if (fd >= lo) |
| 8446 | close(fd); |
| 8447 | return 0; |
| 8448 | } |
| 8449 | #endif /* HAVE_FDWALK */ |
| 8450 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8451 | /*[clinic input] |
| 8452 | os.closerange |
| 8453 | |
| 8454 | fd_low: int |
| 8455 | fd_high: int |
| 8456 | / |
| 8457 | |
| 8458 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 8459 | [clinic start generated code]*/ |
| 8460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8461 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8462 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 8463 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8464 | { |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8465 | #ifdef HAVE_FDWALK |
| 8466 | int lohi[2]; |
| 8467 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8468 | int i; |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8469 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8470 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8471 | _Py_BEGIN_SUPPRESS_IPH |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8472 | #ifdef HAVE_FDWALK |
| 8473 | lohi[0] = Py_MAX(fd_low, 0); |
| 8474 | lohi[1] = fd_high; |
| 8475 | fdwalk(_fdwalk_close_func, lohi); |
| 8476 | #else |
Benjamin Peterson | 207116b | 2016-09-08 11:28:06 -0700 | [diff] [blame] | 8477 | for (i = Py_MAX(fd_low, 0); i < fd_high; i++) |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 8478 | close(i); |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8479 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8480 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8481 | Py_END_ALLOW_THREADS |
| 8482 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8483 | } |
| 8484 | |
| 8485 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8486 | /*[clinic input] |
| 8487 | os.dup -> int |
| 8488 | |
| 8489 | fd: int |
| 8490 | / |
| 8491 | |
| 8492 | Return a duplicate of a file descriptor. |
| 8493 | [clinic start generated code]*/ |
| 8494 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8495 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8496 | os_dup_impl(PyObject *module, int fd) |
| 8497 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8498 | { |
| 8499 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8500 | } |
| 8501 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8502 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8503 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8504 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8505 | fd: int |
| 8506 | fd2: int |
| 8507 | inheritable: bool=True |
| 8508 | |
| 8509 | Duplicate file descriptor. |
| 8510 | [clinic start generated code]*/ |
| 8511 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8512 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8513 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8514 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8515 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8516 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8517 | #if defined(HAVE_DUP3) && \ |
| 8518 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8519 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8520 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8521 | #endif |
| 8522 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8523 | if (fd < 0 || fd2 < 0) { |
| 8524 | posix_error(); |
| 8525 | return -1; |
| 8526 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8527 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8528 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8529 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8530 | * upon close(), and therefore below. |
| 8531 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8532 | #ifdef MS_WINDOWS |
| 8533 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8534 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8535 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8536 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8537 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8538 | if (res < 0) { |
| 8539 | posix_error(); |
| 8540 | return -1; |
| 8541 | } |
| 8542 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8543 | |
| 8544 | /* Character files like console cannot be make non-inheritable */ |
| 8545 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8546 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8547 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8548 | } |
| 8549 | |
| 8550 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8551 | Py_BEGIN_ALLOW_THREADS |
| 8552 | if (!inheritable) |
| 8553 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8554 | else |
| 8555 | res = dup2(fd, fd2); |
| 8556 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8557 | if (res < 0) { |
| 8558 | posix_error(); |
| 8559 | return -1; |
| 8560 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8561 | |
| 8562 | #else |
| 8563 | |
| 8564 | #ifdef HAVE_DUP3 |
| 8565 | if (!inheritable && dup3_works != 0) { |
| 8566 | Py_BEGIN_ALLOW_THREADS |
| 8567 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8568 | Py_END_ALLOW_THREADS |
| 8569 | if (res < 0) { |
| 8570 | if (dup3_works == -1) |
| 8571 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8572 | if (dup3_works) { |
| 8573 | posix_error(); |
| 8574 | return -1; |
| 8575 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8576 | } |
| 8577 | } |
| 8578 | |
| 8579 | if (inheritable || dup3_works == 0) |
| 8580 | { |
| 8581 | #endif |
| 8582 | Py_BEGIN_ALLOW_THREADS |
| 8583 | res = dup2(fd, fd2); |
| 8584 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8585 | if (res < 0) { |
| 8586 | posix_error(); |
| 8587 | return -1; |
| 8588 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8589 | |
| 8590 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8591 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8592 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8593 | } |
| 8594 | #ifdef HAVE_DUP3 |
| 8595 | } |
| 8596 | #endif |
| 8597 | |
| 8598 | #endif |
| 8599 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8600 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8601 | } |
| 8602 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8603 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8604 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8605 | /*[clinic input] |
| 8606 | os.lockf |
| 8607 | |
| 8608 | fd: int |
| 8609 | An open file descriptor. |
| 8610 | command: int |
| 8611 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8612 | length: Py_off_t |
| 8613 | The number of bytes to lock, starting at the current position. |
| 8614 | / |
| 8615 | |
| 8616 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8617 | |
| 8618 | [clinic start generated code]*/ |
| 8619 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8620 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8621 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8622 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8623 | { |
| 8624 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8625 | |
| 8626 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8627 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8628 | Py_END_ALLOW_THREADS |
| 8629 | |
| 8630 | if (res < 0) |
| 8631 | return posix_error(); |
| 8632 | |
| 8633 | Py_RETURN_NONE; |
| 8634 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8635 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8636 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8637 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8638 | /*[clinic input] |
| 8639 | os.lseek -> Py_off_t |
| 8640 | |
| 8641 | fd: int |
| 8642 | position: Py_off_t |
| 8643 | how: int |
| 8644 | / |
| 8645 | |
| 8646 | Set the position of a file descriptor. Return the new position. |
| 8647 | |
| 8648 | Return the new cursor position in number of bytes |
| 8649 | relative to the beginning of the file. |
| 8650 | [clinic start generated code]*/ |
| 8651 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8652 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8653 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8654 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8655 | { |
| 8656 | Py_off_t result; |
| 8657 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8658 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8659 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8660 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8661 | case 0: how = SEEK_SET; break; |
| 8662 | case 1: how = SEEK_CUR; break; |
| 8663 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8664 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8665 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8666 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8667 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8668 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8669 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8670 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8671 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8672 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8673 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8674 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8675 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8676 | if (result < 0) |
| 8677 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8678 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8679 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8680 | } |
| 8681 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8682 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8683 | /*[clinic input] |
| 8684 | os.read |
| 8685 | fd: int |
| 8686 | length: Py_ssize_t |
| 8687 | / |
| 8688 | |
| 8689 | Read from a file descriptor. Returns a bytes object. |
| 8690 | [clinic start generated code]*/ |
| 8691 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8692 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8693 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 8694 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8695 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8696 | Py_ssize_t n; |
| 8697 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8698 | |
| 8699 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8700 | errno = EINVAL; |
| 8701 | return posix_error(); |
| 8702 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8703 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 8704 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8705 | |
| 8706 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8707 | if (buffer == NULL) |
| 8708 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8709 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8710 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8711 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8712 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8713 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8714 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8715 | |
| 8716 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8717 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8718 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8719 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8720 | } |
| 8721 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8722 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8723 | || defined(__APPLE__))) \ |
| 8724 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 8725 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 8726 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8727 | 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] | 8728 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8729 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8730 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8731 | *iov = PyMem_New(struct iovec, cnt); |
| 8732 | if (*iov == NULL) { |
| 8733 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8734 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8735 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8736 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8737 | *buf = PyMem_New(Py_buffer, cnt); |
| 8738 | if (*buf == NULL) { |
| 8739 | PyMem_Del(*iov); |
| 8740 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8741 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8742 | } |
| 8743 | |
| 8744 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8745 | PyObject *item = PySequence_GetItem(seq, i); |
| 8746 | if (item == NULL) |
| 8747 | goto fail; |
| 8748 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8749 | Py_DECREF(item); |
| 8750 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8751 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8752 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8753 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8754 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8755 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8756 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8757 | |
| 8758 | fail: |
| 8759 | PyMem_Del(*iov); |
| 8760 | for (j = 0; j < i; j++) { |
| 8761 | PyBuffer_Release(&(*buf)[j]); |
| 8762 | } |
| 8763 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8764 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8765 | } |
| 8766 | |
| 8767 | static void |
| 8768 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8769 | { |
| 8770 | int i; |
| 8771 | PyMem_Del(iov); |
| 8772 | for (i = 0; i < cnt; i++) { |
| 8773 | PyBuffer_Release(&buf[i]); |
| 8774 | } |
| 8775 | PyMem_Del(buf); |
| 8776 | } |
| 8777 | #endif |
| 8778 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8779 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8780 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8781 | /*[clinic input] |
| 8782 | os.readv -> Py_ssize_t |
| 8783 | |
| 8784 | fd: int |
| 8785 | buffers: object |
| 8786 | / |
| 8787 | |
| 8788 | Read from a file descriptor fd into an iterable of buffers. |
| 8789 | |
| 8790 | The buffers should be mutable buffers accepting bytes. |
| 8791 | readv will transfer data into each buffer until it is full |
| 8792 | and then move on to the next buffer in the sequence to hold |
| 8793 | the rest of the data. |
| 8794 | |
| 8795 | readv returns the total number of bytes read, |
| 8796 | which may be less than the total capacity of all the buffers. |
| 8797 | [clinic start generated code]*/ |
| 8798 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8799 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8800 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 8801 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8802 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8803 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8804 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8805 | struct iovec *iov; |
| 8806 | Py_buffer *buf; |
| 8807 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8808 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8809 | PyErr_SetString(PyExc_TypeError, |
| 8810 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8811 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8812 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8813 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8814 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8815 | if (cnt < 0) |
| 8816 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8817 | |
| 8818 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 8819 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8820 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8821 | do { |
| 8822 | Py_BEGIN_ALLOW_THREADS |
| 8823 | n = readv(fd, iov, cnt); |
| 8824 | Py_END_ALLOW_THREADS |
| 8825 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8826 | |
| 8827 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8828 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8829 | if (!async_err) |
| 8830 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8831 | return -1; |
| 8832 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8833 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8834 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8835 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8836 | #endif /* HAVE_READV */ |
| 8837 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8838 | |
| 8839 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8840 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8841 | os.pread |
| 8842 | |
| 8843 | fd: int |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 8844 | length: Py_ssize_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8845 | offset: Py_off_t |
| 8846 | / |
| 8847 | |
| 8848 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 8849 | |
| 8850 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 8851 | the beginning of the file. The file offset remains unchanged. |
| 8852 | [clinic start generated code]*/ |
| 8853 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8854 | static PyObject * |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 8855 | os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset) |
| 8856 | /*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8857 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8858 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8859 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8860 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8861 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8862 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8863 | errno = EINVAL; |
| 8864 | return posix_error(); |
| 8865 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8866 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8867 | if (buffer == NULL) |
| 8868 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8869 | |
| 8870 | do { |
| 8871 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8872 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8873 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8874 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8875 | Py_END_ALLOW_THREADS |
| 8876 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8877 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8878 | if (n < 0) { |
| 8879 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8880 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8881 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8882 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8883 | _PyBytes_Resize(&buffer, n); |
| 8884 | return buffer; |
| 8885 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8886 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8887 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 8888 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 8889 | /*[clinic input] |
| 8890 | os.preadv -> Py_ssize_t |
| 8891 | |
| 8892 | fd: int |
| 8893 | buffers: object |
| 8894 | offset: Py_off_t |
| 8895 | flags: int = 0 |
| 8896 | / |
| 8897 | |
| 8898 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 8899 | |
| 8900 | Combines the functionality of readv() and pread(). As readv(), it will |
| 8901 | transfer data into each buffer until it is full and then move on to the next |
| 8902 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 8903 | specifies the file offset at which the input operation is to be performed. It |
| 8904 | will return the total number of bytes read (which can be less than the total |
| 8905 | capacity of all the objects). |
| 8906 | |
| 8907 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 8908 | |
| 8909 | - RWF_HIPRI |
| 8910 | - RWF_NOWAIT |
| 8911 | |
| 8912 | Using non-zero flags requires Linux 4.6 or newer. |
| 8913 | [clinic start generated code]*/ |
| 8914 | |
| 8915 | static Py_ssize_t |
| 8916 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 8917 | int flags) |
| 8918 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 8919 | { |
| 8920 | Py_ssize_t cnt, n; |
| 8921 | int async_err = 0; |
| 8922 | struct iovec *iov; |
| 8923 | Py_buffer *buf; |
| 8924 | |
| 8925 | if (!PySequence_Check(buffers)) { |
| 8926 | PyErr_SetString(PyExc_TypeError, |
| 8927 | "preadv2() arg 2 must be a sequence"); |
| 8928 | return -1; |
| 8929 | } |
| 8930 | |
| 8931 | cnt = PySequence_Size(buffers); |
| 8932 | if (cnt < 0) { |
| 8933 | return -1; |
| 8934 | } |
| 8935 | |
| 8936 | #ifndef HAVE_PREADV2 |
| 8937 | if(flags != 0) { |
| 8938 | argument_unavailable_error("preadv2", "flags"); |
| 8939 | return -1; |
| 8940 | } |
| 8941 | #endif |
| 8942 | |
| 8943 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 8944 | return -1; |
| 8945 | } |
| 8946 | #ifdef HAVE_PREADV2 |
| 8947 | do { |
| 8948 | Py_BEGIN_ALLOW_THREADS |
| 8949 | _Py_BEGIN_SUPPRESS_IPH |
| 8950 | n = preadv2(fd, iov, cnt, offset, flags); |
| 8951 | _Py_END_SUPPRESS_IPH |
| 8952 | Py_END_ALLOW_THREADS |
| 8953 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8954 | #else |
| 8955 | do { |
| 8956 | Py_BEGIN_ALLOW_THREADS |
| 8957 | _Py_BEGIN_SUPPRESS_IPH |
| 8958 | n = preadv(fd, iov, cnt, offset); |
| 8959 | _Py_END_SUPPRESS_IPH |
| 8960 | Py_END_ALLOW_THREADS |
| 8961 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8962 | #endif |
| 8963 | |
| 8964 | iov_cleanup(iov, buf, cnt); |
| 8965 | if (n < 0) { |
| 8966 | if (!async_err) { |
| 8967 | posix_error(); |
| 8968 | } |
| 8969 | return -1; |
| 8970 | } |
| 8971 | |
| 8972 | return n; |
| 8973 | } |
| 8974 | #endif /* HAVE_PREADV */ |
| 8975 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8976 | |
| 8977 | /*[clinic input] |
| 8978 | os.write -> Py_ssize_t |
| 8979 | |
| 8980 | fd: int |
| 8981 | data: Py_buffer |
| 8982 | / |
| 8983 | |
| 8984 | Write a bytes object to a file descriptor. |
| 8985 | [clinic start generated code]*/ |
| 8986 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8987 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8988 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 8989 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8990 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8991 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8992 | } |
| 8993 | |
| 8994 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8995 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8996 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8997 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8998 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8999 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9000 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9001 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9002 | static PyObject * |
| 9003 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 9004 | { |
| 9005 | int in, out; |
| 9006 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9007 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9008 | off_t offset; |
| 9009 | |
| 9010 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 9011 | #ifndef __APPLE__ |
| 9012 | Py_ssize_t len; |
| 9013 | #endif |
| 9014 | PyObject *headers = NULL, *trailers = NULL; |
| 9015 | Py_buffer *hbuf, *tbuf; |
| 9016 | off_t sbytes; |
| 9017 | struct sf_hdtr sf; |
| 9018 | int flags = 0; |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 9019 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 9020 | static char *keywords[] = {"out", "in", |
| 9021 | "offset", "count", |
| 9022 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9023 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 9024 | sf.headers = NULL; |
| 9025 | sf.trailers = NULL; |
| 9026 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9027 | #ifdef __APPLE__ |
| 9028 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9029 | 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] | 9030 | #else |
| 9031 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9032 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9033 | #endif |
| 9034 | &headers, &trailers, &flags)) |
| 9035 | return NULL; |
| 9036 | if (headers != NULL) { |
| 9037 | if (!PySequence_Check(headers)) { |
| 9038 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9039 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9040 | return NULL; |
| 9041 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9042 | Py_ssize_t i = PySequence_Size(headers); |
| 9043 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9044 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9045 | if (i > INT_MAX) { |
| 9046 | PyErr_SetString(PyExc_OverflowError, |
| 9047 | "sendfile() header is too large"); |
| 9048 | return NULL; |
| 9049 | } |
| 9050 | if (i > 0) { |
| 9051 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9052 | if (iov_setup(&(sf.headers), &hbuf, |
| 9053 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9054 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9055 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9056 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 9057 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 9058 | # define OFF_T_MAX 0x7fffffffffffffff |
| 9059 | if (sbytes >= OFF_T_MAX - blen) { |
| 9060 | PyErr_SetString(PyExc_OverflowError, |
| 9061 | "sendfile() header is too large"); |
| 9062 | return NULL; |
| 9063 | } |
| 9064 | sbytes += blen; |
| 9065 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9066 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9067 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9068 | } |
| 9069 | } |
| 9070 | if (trailers != NULL) { |
| 9071 | if (!PySequence_Check(trailers)) { |
| 9072 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9073 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9074 | return NULL; |
| 9075 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9076 | Py_ssize_t i = PySequence_Size(trailers); |
| 9077 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9078 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9079 | if (i > INT_MAX) { |
| 9080 | PyErr_SetString(PyExc_OverflowError, |
| 9081 | "sendfile() trailer is too large"); |
| 9082 | return NULL; |
| 9083 | } |
| 9084 | if (i > 0) { |
| 9085 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9086 | if (iov_setup(&(sf.trailers), &tbuf, |
| 9087 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9088 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9089 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9090 | } |
| 9091 | } |
| 9092 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9093 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9094 | do { |
| 9095 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9096 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9097 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9098 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9099 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9100 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9101 | Py_END_ALLOW_THREADS |
| 9102 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9103 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9104 | |
| 9105 | if (sf.headers != NULL) |
| 9106 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 9107 | if (sf.trailers != NULL) |
| 9108 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 9109 | |
| 9110 | if (ret < 0) { |
| 9111 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 9112 | if (sbytes != 0) { |
| 9113 | // some data has been sent |
| 9114 | goto done; |
| 9115 | } |
| 9116 | else { |
| 9117 | // no data has been sent; upper application is supposed |
| 9118 | // to retry on EAGAIN or EBUSY |
| 9119 | return posix_error(); |
| 9120 | } |
| 9121 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9122 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9123 | } |
| 9124 | goto done; |
| 9125 | |
| 9126 | done: |
| 9127 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 9128 | return Py_BuildValue("l", sbytes); |
| 9129 | #else |
| 9130 | return Py_BuildValue("L", sbytes); |
| 9131 | #endif |
| 9132 | |
| 9133 | #else |
| 9134 | Py_ssize_t count; |
| 9135 | PyObject *offobj; |
| 9136 | static char *keywords[] = {"out", "in", |
| 9137 | "offset", "count", NULL}; |
| 9138 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 9139 | keywords, &out, &in, &offobj, &count)) |
| 9140 | return NULL; |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 9141 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9142 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9143 | do { |
| 9144 | Py_BEGIN_ALLOW_THREADS |
| 9145 | ret = sendfile(out, in, NULL, count); |
| 9146 | Py_END_ALLOW_THREADS |
| 9147 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9148 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9149 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9150 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9151 | } |
| 9152 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9153 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 9154 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9155 | |
| 9156 | do { |
| 9157 | Py_BEGIN_ALLOW_THREADS |
| 9158 | ret = sendfile(out, in, &offset, count); |
| 9159 | Py_END_ALLOW_THREADS |
| 9160 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9161 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9162 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9163 | return Py_BuildValue("n", ret); |
| 9164 | #endif |
| 9165 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9166 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9167 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9168 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9169 | #if defined(__APPLE__) |
| 9170 | /*[clinic input] |
| 9171 | os._fcopyfile |
| 9172 | |
| 9173 | infd: int |
| 9174 | outfd: int |
| 9175 | flags: int |
| 9176 | / |
| 9177 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9178 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9179 | [clinic start generated code]*/ |
| 9180 | |
| 9181 | static PyObject * |
| 9182 | os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags) |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9183 | /*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9184 | { |
| 9185 | int ret; |
| 9186 | |
| 9187 | Py_BEGIN_ALLOW_THREADS |
| 9188 | ret = fcopyfile(infd, outfd, NULL, flags); |
| 9189 | Py_END_ALLOW_THREADS |
| 9190 | if (ret < 0) |
| 9191 | return posix_error(); |
| 9192 | Py_RETURN_NONE; |
| 9193 | } |
| 9194 | #endif |
| 9195 | |
| 9196 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9197 | /*[clinic input] |
| 9198 | os.fstat |
| 9199 | |
| 9200 | fd : int |
| 9201 | |
| 9202 | Perform a stat system call on the given file descriptor. |
| 9203 | |
| 9204 | Like stat(), but for an open file descriptor. |
| 9205 | Equivalent to os.stat(fd). |
| 9206 | [clinic start generated code]*/ |
| 9207 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9208 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9209 | os_fstat_impl(PyObject *module, int fd) |
| 9210 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9211 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9212 | STRUCT_STAT st; |
| 9213 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9214 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9215 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9216 | do { |
| 9217 | Py_BEGIN_ALLOW_THREADS |
| 9218 | res = FSTAT(fd, &st); |
| 9219 | Py_END_ALLOW_THREADS |
| 9220 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9221 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9222 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9223 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9224 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9225 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9226 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9227 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9228 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9229 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9230 | } |
| 9231 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9232 | |
| 9233 | /*[clinic input] |
| 9234 | os.isatty -> bool |
| 9235 | fd: int |
| 9236 | / |
| 9237 | |
| 9238 | Return True if the fd is connected to a terminal. |
| 9239 | |
| 9240 | Return True if the file descriptor is an open file descriptor |
| 9241 | connected to the slave end of a terminal. |
| 9242 | [clinic start generated code]*/ |
| 9243 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9244 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9245 | os_isatty_impl(PyObject *module, int fd) |
| 9246 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9247 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9248 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9249 | _Py_BEGIN_SUPPRESS_IPH |
| 9250 | return_value = isatty(fd); |
| 9251 | _Py_END_SUPPRESS_IPH |
| 9252 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9253 | } |
| 9254 | |
| 9255 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9256 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9257 | /*[clinic input] |
| 9258 | os.pipe |
| 9259 | |
| 9260 | Create a pipe. |
| 9261 | |
| 9262 | Returns a tuple of two file descriptors: |
| 9263 | (read_fd, write_fd) |
| 9264 | [clinic start generated code]*/ |
| 9265 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9266 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9267 | os_pipe_impl(PyObject *module) |
| 9268 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9269 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9270 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9271 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9272 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9273 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9274 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9275 | #else |
| 9276 | int res; |
| 9277 | #endif |
| 9278 | |
| 9279 | #ifdef MS_WINDOWS |
| 9280 | attr.nLength = sizeof(attr); |
| 9281 | attr.lpSecurityDescriptor = NULL; |
| 9282 | attr.bInheritHandle = FALSE; |
| 9283 | |
| 9284 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9285 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9286 | ok = CreatePipe(&read, &write, &attr, 0); |
| 9287 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9288 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 9289 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9290 | if (fds[0] == -1 || fds[1] == -1) { |
| 9291 | CloseHandle(read); |
| 9292 | CloseHandle(write); |
| 9293 | ok = 0; |
| 9294 | } |
| 9295 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9296 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9297 | Py_END_ALLOW_THREADS |
| 9298 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9299 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9300 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9301 | #else |
| 9302 | |
| 9303 | #ifdef HAVE_PIPE2 |
| 9304 | Py_BEGIN_ALLOW_THREADS |
| 9305 | res = pipe2(fds, O_CLOEXEC); |
| 9306 | Py_END_ALLOW_THREADS |
| 9307 | |
| 9308 | if (res != 0 && errno == ENOSYS) |
| 9309 | { |
| 9310 | #endif |
| 9311 | Py_BEGIN_ALLOW_THREADS |
| 9312 | res = pipe(fds); |
| 9313 | Py_END_ALLOW_THREADS |
| 9314 | |
| 9315 | if (res == 0) { |
| 9316 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 9317 | close(fds[0]); |
| 9318 | close(fds[1]); |
| 9319 | return NULL; |
| 9320 | } |
| 9321 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 9322 | close(fds[0]); |
| 9323 | close(fds[1]); |
| 9324 | return NULL; |
| 9325 | } |
| 9326 | } |
| 9327 | #ifdef HAVE_PIPE2 |
| 9328 | } |
| 9329 | #endif |
| 9330 | |
| 9331 | if (res != 0) |
| 9332 | return PyErr_SetFromErrno(PyExc_OSError); |
| 9333 | #endif /* !MS_WINDOWS */ |
| 9334 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9335 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9336 | #endif /* HAVE_PIPE */ |
| 9337 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9338 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9339 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9340 | /*[clinic input] |
| 9341 | os.pipe2 |
| 9342 | |
| 9343 | flags: int |
| 9344 | / |
| 9345 | |
| 9346 | Create a pipe with flags set atomically. |
| 9347 | |
| 9348 | Returns a tuple of two file descriptors: |
| 9349 | (read_fd, write_fd) |
| 9350 | |
| 9351 | flags can be constructed by ORing together one or more of these values: |
| 9352 | O_NONBLOCK, O_CLOEXEC. |
| 9353 | [clinic start generated code]*/ |
| 9354 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9355 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9356 | os_pipe2_impl(PyObject *module, int flags) |
| 9357 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9358 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9359 | int fds[2]; |
| 9360 | int res; |
| 9361 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9362 | res = pipe2(fds, flags); |
| 9363 | if (res != 0) |
| 9364 | return posix_error(); |
| 9365 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 9366 | } |
| 9367 | #endif /* HAVE_PIPE2 */ |
| 9368 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9369 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9370 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9371 | /*[clinic input] |
| 9372 | os.writev -> Py_ssize_t |
| 9373 | fd: int |
| 9374 | buffers: object |
| 9375 | / |
| 9376 | |
| 9377 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 9378 | |
| 9379 | Returns the total number of bytes written. |
| 9380 | buffers must be a sequence of bytes-like objects. |
| 9381 | [clinic start generated code]*/ |
| 9382 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9383 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9384 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 9385 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9386 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9387 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9388 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9389 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9390 | struct iovec *iov; |
| 9391 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9392 | |
| 9393 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9394 | PyErr_SetString(PyExc_TypeError, |
| 9395 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9396 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9397 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9398 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9399 | if (cnt < 0) |
| 9400 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9401 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9402 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9403 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9404 | } |
| 9405 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9406 | do { |
| 9407 | Py_BEGIN_ALLOW_THREADS |
| 9408 | result = writev(fd, iov, cnt); |
| 9409 | Py_END_ALLOW_THREADS |
| 9410 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9411 | |
| 9412 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9413 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9414 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9415 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9416 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9417 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9418 | #endif /* HAVE_WRITEV */ |
| 9419 | |
| 9420 | |
| 9421 | #ifdef HAVE_PWRITE |
| 9422 | /*[clinic input] |
| 9423 | os.pwrite -> Py_ssize_t |
| 9424 | |
| 9425 | fd: int |
| 9426 | buffer: Py_buffer |
| 9427 | offset: Py_off_t |
| 9428 | / |
| 9429 | |
| 9430 | Write bytes to a file descriptor starting at a particular offset. |
| 9431 | |
| 9432 | Write buffer to fd, starting at offset bytes from the beginning of |
| 9433 | the file. Returns the number of bytes writte. Does not change the |
| 9434 | current file offset. |
| 9435 | [clinic start generated code]*/ |
| 9436 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9437 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9438 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 9439 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9440 | { |
| 9441 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9442 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9443 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9444 | do { |
| 9445 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9446 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9447 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9448 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9449 | Py_END_ALLOW_THREADS |
| 9450 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9451 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9452 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9453 | posix_error(); |
| 9454 | return size; |
| 9455 | } |
| 9456 | #endif /* HAVE_PWRITE */ |
| 9457 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9458 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9459 | /*[clinic input] |
| 9460 | os.pwritev -> Py_ssize_t |
| 9461 | |
| 9462 | fd: int |
| 9463 | buffers: object |
| 9464 | offset: Py_off_t |
| 9465 | flags: int = 0 |
| 9466 | / |
| 9467 | |
| 9468 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 9469 | |
| 9470 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 9471 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 9472 | buffer is written before proceeding to second, and so on. The operating system may |
| 9473 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 9474 | This function writes the contents of each object to the file descriptor and returns |
| 9475 | the total number of bytes written. |
| 9476 | |
| 9477 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9478 | |
| 9479 | - RWF_DSYNC |
| 9480 | - RWF_SYNC |
| 9481 | |
| 9482 | Using non-zero flags requires Linux 4.7 or newer. |
| 9483 | [clinic start generated code]*/ |
| 9484 | |
| 9485 | static Py_ssize_t |
| 9486 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9487 | int flags) |
| 9488 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/ |
| 9489 | { |
| 9490 | Py_ssize_t cnt; |
| 9491 | Py_ssize_t result; |
| 9492 | int async_err = 0; |
| 9493 | struct iovec *iov; |
| 9494 | Py_buffer *buf; |
| 9495 | |
| 9496 | if (!PySequence_Check(buffers)) { |
| 9497 | PyErr_SetString(PyExc_TypeError, |
| 9498 | "pwritev() arg 2 must be a sequence"); |
| 9499 | return -1; |
| 9500 | } |
| 9501 | |
| 9502 | cnt = PySequence_Size(buffers); |
| 9503 | if (cnt < 0) { |
| 9504 | return -1; |
| 9505 | } |
| 9506 | |
| 9507 | #ifndef HAVE_PWRITEV2 |
| 9508 | if(flags != 0) { |
| 9509 | argument_unavailable_error("pwritev2", "flags"); |
| 9510 | return -1; |
| 9511 | } |
| 9512 | #endif |
| 9513 | |
| 9514 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9515 | return -1; |
| 9516 | } |
| 9517 | #ifdef HAVE_PWRITEV2 |
| 9518 | do { |
| 9519 | Py_BEGIN_ALLOW_THREADS |
| 9520 | _Py_BEGIN_SUPPRESS_IPH |
| 9521 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9522 | _Py_END_SUPPRESS_IPH |
| 9523 | Py_END_ALLOW_THREADS |
| 9524 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9525 | #else |
| 9526 | do { |
| 9527 | Py_BEGIN_ALLOW_THREADS |
| 9528 | _Py_BEGIN_SUPPRESS_IPH |
| 9529 | result = pwritev(fd, iov, cnt, offset); |
| 9530 | _Py_END_SUPPRESS_IPH |
| 9531 | Py_END_ALLOW_THREADS |
| 9532 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9533 | #endif |
| 9534 | |
| 9535 | iov_cleanup(iov, buf, cnt); |
| 9536 | if (result < 0) { |
| 9537 | if (!async_err) { |
| 9538 | posix_error(); |
| 9539 | } |
| 9540 | return -1; |
| 9541 | } |
| 9542 | |
| 9543 | return result; |
| 9544 | } |
| 9545 | #endif /* HAVE_PWRITEV */ |
| 9546 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9547 | #ifdef HAVE_COPY_FILE_RANGE |
| 9548 | /*[clinic input] |
| 9549 | |
| 9550 | os.copy_file_range |
| 9551 | src: int |
| 9552 | Source file descriptor. |
| 9553 | dst: int |
| 9554 | Destination file descriptor. |
| 9555 | count: Py_ssize_t |
| 9556 | Number of bytes to copy. |
| 9557 | offset_src: object = None |
| 9558 | Starting offset in src. |
| 9559 | offset_dst: object = None |
| 9560 | Starting offset in dst. |
| 9561 | |
| 9562 | Copy count bytes from one file descriptor to another. |
| 9563 | |
| 9564 | If offset_src is None, then src is read from the current position; |
| 9565 | respectively for offset_dst. |
| 9566 | [clinic start generated code]*/ |
| 9567 | |
| 9568 | static PyObject * |
| 9569 | os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, |
| 9570 | PyObject *offset_src, PyObject *offset_dst) |
| 9571 | /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/ |
| 9572 | { |
| 9573 | off_t offset_src_val, offset_dst_val; |
| 9574 | off_t *p_offset_src = NULL; |
| 9575 | off_t *p_offset_dst = NULL; |
| 9576 | Py_ssize_t ret; |
| 9577 | int async_err = 0; |
| 9578 | /* The flags argument is provided to allow |
| 9579 | * for future extensions and currently must be to 0. */ |
| 9580 | int flags = 0; |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9581 | |
| 9582 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9583 | if (count < 0) { |
| 9584 | PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); |
| 9585 | return NULL; |
| 9586 | } |
| 9587 | |
| 9588 | if (offset_src != Py_None) { |
| 9589 | if (!Py_off_t_converter(offset_src, &offset_src_val)) { |
| 9590 | return NULL; |
| 9591 | } |
| 9592 | p_offset_src = &offset_src_val; |
| 9593 | } |
| 9594 | |
| 9595 | if (offset_dst != Py_None) { |
| 9596 | if (!Py_off_t_converter(offset_dst, &offset_dst_val)) { |
| 9597 | return NULL; |
| 9598 | } |
| 9599 | p_offset_dst = &offset_dst_val; |
| 9600 | } |
| 9601 | |
| 9602 | do { |
| 9603 | Py_BEGIN_ALLOW_THREADS |
| 9604 | ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags); |
| 9605 | Py_END_ALLOW_THREADS |
| 9606 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9607 | |
| 9608 | if (ret < 0) { |
| 9609 | return (!async_err) ? posix_error() : NULL; |
| 9610 | } |
| 9611 | |
| 9612 | return PyLong_FromSsize_t(ret); |
| 9613 | } |
| 9614 | #endif /* HAVE_COPY_FILE_RANGE*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9615 | |
| 9616 | #ifdef HAVE_MKFIFO |
| 9617 | /*[clinic input] |
| 9618 | os.mkfifo |
| 9619 | |
| 9620 | path: path_t |
| 9621 | mode: int=0o666 |
| 9622 | * |
| 9623 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9624 | |
| 9625 | Create a "fifo" (a POSIX named pipe). |
| 9626 | |
| 9627 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9628 | and path should be relative; path will then be relative to that directory. |
| 9629 | dir_fd may not be implemented on your platform. |
| 9630 | If it is unavailable, using it will raise a NotImplementedError. |
| 9631 | [clinic start generated code]*/ |
| 9632 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9633 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9634 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 9635 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9636 | { |
| 9637 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9638 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9639 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9640 | do { |
| 9641 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9642 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9643 | if (dir_fd != DEFAULT_DIR_FD) |
| 9644 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 9645 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9646 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9647 | result = mkfifo(path->narrow, mode); |
| 9648 | Py_END_ALLOW_THREADS |
| 9649 | } while (result != 0 && errno == EINTR && |
| 9650 | !(async_err = PyErr_CheckSignals())); |
| 9651 | if (result != 0) |
| 9652 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9653 | |
| 9654 | Py_RETURN_NONE; |
| 9655 | } |
| 9656 | #endif /* HAVE_MKFIFO */ |
| 9657 | |
| 9658 | |
| 9659 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 9660 | /*[clinic input] |
| 9661 | os.mknod |
| 9662 | |
| 9663 | path: path_t |
| 9664 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9665 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9666 | * |
| 9667 | dir_fd: dir_fd(requires='mknodat')=None |
| 9668 | |
| 9669 | Create a node in the file system. |
| 9670 | |
| 9671 | Create a node in the file system (file, device special file or named pipe) |
| 9672 | at path. mode specifies both the permissions to use and the |
| 9673 | type of node to be created, being combined (bitwise OR) with one of |
| 9674 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 9675 | device defines the newly created device special file (probably using |
| 9676 | os.makedev()). Otherwise device is ignored. |
| 9677 | |
| 9678 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9679 | and path should be relative; path will then be relative to that directory. |
| 9680 | dir_fd may not be implemented on your platform. |
| 9681 | If it is unavailable, using it will raise a NotImplementedError. |
| 9682 | [clinic start generated code]*/ |
| 9683 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9684 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9685 | 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] | 9686 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9687 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9688 | { |
| 9689 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9690 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9691 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9692 | do { |
| 9693 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9694 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9695 | if (dir_fd != DEFAULT_DIR_FD) |
| 9696 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 9697 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9698 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9699 | result = mknod(path->narrow, mode, device); |
| 9700 | Py_END_ALLOW_THREADS |
| 9701 | } while (result != 0 && errno == EINTR && |
| 9702 | !(async_err = PyErr_CheckSignals())); |
| 9703 | if (result != 0) |
| 9704 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9705 | |
| 9706 | Py_RETURN_NONE; |
| 9707 | } |
| 9708 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 9709 | |
| 9710 | |
| 9711 | #ifdef HAVE_DEVICE_MACROS |
| 9712 | /*[clinic input] |
| 9713 | os.major -> unsigned_int |
| 9714 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9715 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9716 | / |
| 9717 | |
| 9718 | Extracts a device major number from a raw device number. |
| 9719 | [clinic start generated code]*/ |
| 9720 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9721 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9722 | os_major_impl(PyObject *module, dev_t device) |
| 9723 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9724 | { |
| 9725 | return major(device); |
| 9726 | } |
| 9727 | |
| 9728 | |
| 9729 | /*[clinic input] |
| 9730 | os.minor -> unsigned_int |
| 9731 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9732 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9733 | / |
| 9734 | |
| 9735 | Extracts a device minor number from a raw device number. |
| 9736 | [clinic start generated code]*/ |
| 9737 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9738 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9739 | os_minor_impl(PyObject *module, dev_t device) |
| 9740 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9741 | { |
| 9742 | return minor(device); |
| 9743 | } |
| 9744 | |
| 9745 | |
| 9746 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9747 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9748 | |
| 9749 | major: int |
| 9750 | minor: int |
| 9751 | / |
| 9752 | |
| 9753 | Composes a raw device number from the major and minor device numbers. |
| 9754 | [clinic start generated code]*/ |
| 9755 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9756 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9757 | os_makedev_impl(PyObject *module, int major, int minor) |
| 9758 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9759 | { |
| 9760 | return makedev(major, minor); |
| 9761 | } |
| 9762 | #endif /* HAVE_DEVICE_MACROS */ |
| 9763 | |
| 9764 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9765 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9766 | /*[clinic input] |
| 9767 | os.ftruncate |
| 9768 | |
| 9769 | fd: int |
| 9770 | length: Py_off_t |
| 9771 | / |
| 9772 | |
| 9773 | Truncate a file, specified by file descriptor, to a specific length. |
| 9774 | [clinic start generated code]*/ |
| 9775 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9776 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9777 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 9778 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9779 | { |
| 9780 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9781 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9782 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 9783 | if (PySys_Audit("os.truncate", "in", fd, length) < 0) { |
| 9784 | return NULL; |
| 9785 | } |
| 9786 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9787 | do { |
| 9788 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9789 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9790 | #ifdef MS_WINDOWS |
| 9791 | result = _chsize_s(fd, length); |
| 9792 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9793 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9794 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9795 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9796 | Py_END_ALLOW_THREADS |
| 9797 | } while (result != 0 && errno == EINTR && |
| 9798 | !(async_err = PyErr_CheckSignals())); |
| 9799 | if (result != 0) |
| 9800 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9801 | Py_RETURN_NONE; |
| 9802 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9803 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9804 | |
| 9805 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9806 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9807 | /*[clinic input] |
| 9808 | os.truncate |
| 9809 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 9810 | length: Py_off_t |
| 9811 | |
| 9812 | Truncate a file, specified by path, to a specific length. |
| 9813 | |
| 9814 | On some platforms, path may also be specified as an open file descriptor. |
| 9815 | If this functionality is unavailable, using it raises an exception. |
| 9816 | [clinic start generated code]*/ |
| 9817 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9818 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9819 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 9820 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9821 | { |
| 9822 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9823 | #ifdef MS_WINDOWS |
| 9824 | int fd; |
| 9825 | #endif |
| 9826 | |
| 9827 | if (path->fd != -1) |
| 9828 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9829 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 9830 | if (PySys_Audit("os.truncate", "On", path->object, length) < 0) { |
| 9831 | return NULL; |
| 9832 | } |
| 9833 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9834 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9835 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9836 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 9837 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 9838 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9839 | result = -1; |
| 9840 | else { |
| 9841 | result = _chsize_s(fd, length); |
| 9842 | close(fd); |
| 9843 | if (result < 0) |
| 9844 | errno = result; |
| 9845 | } |
| 9846 | #else |
| 9847 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9848 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9849 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9850 | Py_END_ALLOW_THREADS |
| 9851 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 9852 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9853 | |
| 9854 | Py_RETURN_NONE; |
| 9855 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9856 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9857 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9858 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9859 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 9860 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 9861 | defined, which is the case in Python on AIX. AIX bug report: |
| 9862 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 9863 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 9864 | # define POSIX_FADVISE_AIX_BUG |
| 9865 | #endif |
| 9866 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9867 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9868 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9869 | /*[clinic input] |
| 9870 | os.posix_fallocate |
| 9871 | |
| 9872 | fd: int |
| 9873 | offset: Py_off_t |
| 9874 | length: Py_off_t |
| 9875 | / |
| 9876 | |
| 9877 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 9878 | |
| 9879 | Ensure that the file specified by fd encompasses a range of bytes |
| 9880 | starting at offset bytes from the beginning and continuing for length bytes. |
| 9881 | [clinic start generated code]*/ |
| 9882 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9883 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9884 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9885 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9886 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9887 | { |
| 9888 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9889 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9890 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9891 | do { |
| 9892 | Py_BEGIN_ALLOW_THREADS |
| 9893 | result = posix_fallocate(fd, offset, length); |
| 9894 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9895 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9896 | |
| 9897 | if (result == 0) |
| 9898 | Py_RETURN_NONE; |
| 9899 | |
| 9900 | if (async_err) |
| 9901 | return NULL; |
| 9902 | |
| 9903 | errno = result; |
| 9904 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9905 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9906 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9907 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9908 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9909 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9910 | /*[clinic input] |
| 9911 | os.posix_fadvise |
| 9912 | |
| 9913 | fd: int |
| 9914 | offset: Py_off_t |
| 9915 | length: Py_off_t |
| 9916 | advice: int |
| 9917 | / |
| 9918 | |
| 9919 | Announce an intention to access data in a specific pattern. |
| 9920 | |
| 9921 | Announce an intention to access data in a specific pattern, thus allowing |
| 9922 | the kernel to make optimizations. |
| 9923 | The advice applies to the region of the file specified by fd starting at |
| 9924 | offset and continuing for length bytes. |
| 9925 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 9926 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 9927 | POSIX_FADV_DONTNEED. |
| 9928 | [clinic start generated code]*/ |
| 9929 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9930 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9931 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9932 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9933 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9934 | { |
| 9935 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9936 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9937 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9938 | do { |
| 9939 | Py_BEGIN_ALLOW_THREADS |
| 9940 | result = posix_fadvise(fd, offset, length, advice); |
| 9941 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9942 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9943 | |
| 9944 | if (result == 0) |
| 9945 | Py_RETURN_NONE; |
| 9946 | |
| 9947 | if (async_err) |
| 9948 | return NULL; |
| 9949 | |
| 9950 | errno = result; |
| 9951 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9952 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9953 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9954 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9955 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9956 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 9957 | /* Save putenv() parameters as values here, so we can collect them when they |
| 9958 | * get re-set with another call for the same key. */ |
| 9959 | static PyObject *posix_putenv_garbage; |
| 9960 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9961 | static void |
| 9962 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9963 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9964 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 9965 | * this will cause previous value to be collected. This has to |
| 9966 | * happen after the real putenv() call because the old value |
| 9967 | * was still accessible until then. */ |
| 9968 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 9969 | /* really not much we can do; just leak */ |
| 9970 | PyErr_Clear(); |
| 9971 | else |
| 9972 | Py_DECREF(value); |
| 9973 | } |
| 9974 | |
| 9975 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 9976 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9977 | /*[clinic input] |
| 9978 | os.putenv |
| 9979 | |
| 9980 | name: unicode |
| 9981 | value: unicode |
| 9982 | / |
| 9983 | |
| 9984 | Change or add an environment variable. |
| 9985 | [clinic start generated code]*/ |
| 9986 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9987 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9988 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 9989 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9990 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 9991 | const wchar_t *env; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9992 | Py_ssize_t size; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9993 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9994 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 9995 | defining hidden environment variables. */ |
| 9996 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 9997 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 9998 | { |
| 9999 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10000 | return NULL; |
| 10001 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10002 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 10003 | if (unicode == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10004 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10005 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10006 | |
| 10007 | env = PyUnicode_AsUnicodeAndSize(unicode, &size); |
| 10008 | if (env == NULL) |
| 10009 | goto error; |
| 10010 | if (size > _MAX_ENV) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 10011 | PyErr_Format(PyExc_ValueError, |
| 10012 | "the environment variable is longer than %u characters", |
| 10013 | _MAX_ENV); |
| 10014 | goto error; |
| 10015 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10016 | if (wcslen(env) != (size_t)size) { |
| 10017 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10018 | goto error; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10019 | } |
| 10020 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10021 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10022 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10023 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10024 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10025 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10026 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10027 | Py_RETURN_NONE; |
| 10028 | |
| 10029 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10030 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10031 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10032 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10033 | #else /* MS_WINDOWS */ |
| 10034 | /*[clinic input] |
| 10035 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10036 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10037 | name: FSConverter |
| 10038 | value: FSConverter |
| 10039 | / |
| 10040 | |
| 10041 | Change or add an environment variable. |
| 10042 | [clinic start generated code]*/ |
| 10043 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10044 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10045 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10046 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10047 | { |
| 10048 | PyObject *bytes = NULL; |
| 10049 | char *env; |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10050 | const char *name_string = PyBytes_AS_STRING(name); |
| 10051 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10052 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10053 | if (strchr(name_string, '=') != NULL) { |
| 10054 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10055 | return NULL; |
| 10056 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10057 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 10058 | if (bytes == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10059 | return NULL; |
| 10060 | } |
| 10061 | |
| 10062 | env = PyBytes_AS_STRING(bytes); |
| 10063 | if (putenv(env)) { |
| 10064 | Py_DECREF(bytes); |
| 10065 | return posix_error(); |
| 10066 | } |
| 10067 | |
| 10068 | posix_putenv_garbage_setitem(name, bytes); |
| 10069 | Py_RETURN_NONE; |
| 10070 | } |
| 10071 | #endif /* MS_WINDOWS */ |
| 10072 | #endif /* HAVE_PUTENV */ |
| 10073 | |
| 10074 | |
| 10075 | #ifdef HAVE_UNSETENV |
| 10076 | /*[clinic input] |
| 10077 | os.unsetenv |
| 10078 | name: FSConverter |
| 10079 | / |
| 10080 | |
| 10081 | Delete an environment variable. |
| 10082 | [clinic start generated code]*/ |
| 10083 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10084 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10085 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10086 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10087 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10088 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10089 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10090 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10091 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10092 | #ifdef HAVE_BROKEN_UNSETENV |
| 10093 | unsetenv(PyBytes_AS_STRING(name)); |
| 10094 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 10095 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10096 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10097 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10098 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10099 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10100 | /* Remove the key from posix_putenv_garbage; |
| 10101 | * this will cause it to be collected. This has to |
| 10102 | * happen after the real unsetenv() call because the |
| 10103 | * old value was still accessible until then. |
| 10104 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 10105 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10106 | /* really not much we can do; just leak */ |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 10107 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 10108 | return NULL; |
| 10109 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10110 | PyErr_Clear(); |
| 10111 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10112 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10113 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10114 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10115 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10116 | |
| 10117 | /*[clinic input] |
| 10118 | os.strerror |
| 10119 | |
| 10120 | code: int |
| 10121 | / |
| 10122 | |
| 10123 | Translate an error code to a message string. |
| 10124 | [clinic start generated code]*/ |
| 10125 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10126 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10127 | os_strerror_impl(PyObject *module, int code) |
| 10128 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10129 | { |
| 10130 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10131 | if (message == NULL) { |
| 10132 | PyErr_SetString(PyExc_ValueError, |
| 10133 | "strerror() argument out of range"); |
| 10134 | return NULL; |
| 10135 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 10136 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10137 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10138 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10139 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10140 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10141 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10142 | /*[clinic input] |
| 10143 | os.WCOREDUMP -> bool |
| 10144 | |
| 10145 | status: int |
| 10146 | / |
| 10147 | |
| 10148 | Return True if the process returning status was dumped to a core file. |
| 10149 | [clinic start generated code]*/ |
| 10150 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10151 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10152 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 10153 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10154 | { |
| 10155 | WAIT_TYPE wait_status; |
| 10156 | WAIT_STATUS_INT(wait_status) = status; |
| 10157 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10158 | } |
| 10159 | #endif /* WCOREDUMP */ |
| 10160 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10161 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10162 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10163 | /*[clinic input] |
| 10164 | os.WIFCONTINUED -> bool |
| 10165 | |
| 10166 | status: int |
| 10167 | |
| 10168 | Return True if a particular process was continued from a job control stop. |
| 10169 | |
| 10170 | Return True if the process returning status was continued from a |
| 10171 | job control stop. |
| 10172 | [clinic start generated code]*/ |
| 10173 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10174 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10175 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 10176 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10177 | { |
| 10178 | WAIT_TYPE wait_status; |
| 10179 | WAIT_STATUS_INT(wait_status) = status; |
| 10180 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10181 | } |
| 10182 | #endif /* WIFCONTINUED */ |
| 10183 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10184 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10185 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10186 | /*[clinic input] |
| 10187 | os.WIFSTOPPED -> bool |
| 10188 | |
| 10189 | status: int |
| 10190 | |
| 10191 | Return True if the process returning status was stopped. |
| 10192 | [clinic start generated code]*/ |
| 10193 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10194 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10195 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 10196 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10197 | { |
| 10198 | WAIT_TYPE wait_status; |
| 10199 | WAIT_STATUS_INT(wait_status) = status; |
| 10200 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10201 | } |
| 10202 | #endif /* WIFSTOPPED */ |
| 10203 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10204 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10205 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10206 | /*[clinic input] |
| 10207 | os.WIFSIGNALED -> bool |
| 10208 | |
| 10209 | status: int |
| 10210 | |
| 10211 | Return True if the process returning status was terminated by a signal. |
| 10212 | [clinic start generated code]*/ |
| 10213 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10214 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10215 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 10216 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10217 | { |
| 10218 | WAIT_TYPE wait_status; |
| 10219 | WAIT_STATUS_INT(wait_status) = status; |
| 10220 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10221 | } |
| 10222 | #endif /* WIFSIGNALED */ |
| 10223 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10224 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10225 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10226 | /*[clinic input] |
| 10227 | os.WIFEXITED -> bool |
| 10228 | |
| 10229 | status: int |
| 10230 | |
| 10231 | Return True if the process returning status exited via the exit() system call. |
| 10232 | [clinic start generated code]*/ |
| 10233 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10234 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10235 | os_WIFEXITED_impl(PyObject *module, int status) |
| 10236 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10237 | { |
| 10238 | WAIT_TYPE wait_status; |
| 10239 | WAIT_STATUS_INT(wait_status) = status; |
| 10240 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10241 | } |
| 10242 | #endif /* WIFEXITED */ |
| 10243 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10244 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 10245 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10246 | /*[clinic input] |
| 10247 | os.WEXITSTATUS -> int |
| 10248 | |
| 10249 | status: int |
| 10250 | |
| 10251 | Return the process return code from status. |
| 10252 | [clinic start generated code]*/ |
| 10253 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10254 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10255 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 10256 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10257 | { |
| 10258 | WAIT_TYPE wait_status; |
| 10259 | WAIT_STATUS_INT(wait_status) = status; |
| 10260 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10261 | } |
| 10262 | #endif /* WEXITSTATUS */ |
| 10263 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10264 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10265 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10266 | /*[clinic input] |
| 10267 | os.WTERMSIG -> int |
| 10268 | |
| 10269 | status: int |
| 10270 | |
| 10271 | Return the signal that terminated the process that provided the status value. |
| 10272 | [clinic start generated code]*/ |
| 10273 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10274 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10275 | os_WTERMSIG_impl(PyObject *module, int status) |
| 10276 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10277 | { |
| 10278 | WAIT_TYPE wait_status; |
| 10279 | WAIT_STATUS_INT(wait_status) = status; |
| 10280 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10281 | } |
| 10282 | #endif /* WTERMSIG */ |
| 10283 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10284 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10285 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10286 | /*[clinic input] |
| 10287 | os.WSTOPSIG -> int |
| 10288 | |
| 10289 | status: int |
| 10290 | |
| 10291 | Return the signal that stopped the process that provided the status value. |
| 10292 | [clinic start generated code]*/ |
| 10293 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10294 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10295 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 10296 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10297 | { |
| 10298 | WAIT_TYPE wait_status; |
| 10299 | WAIT_STATUS_INT(wait_status) = status; |
| 10300 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10301 | } |
| 10302 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10303 | #endif /* HAVE_SYS_WAIT_H */ |
| 10304 | |
| 10305 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10306 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 10307 | #ifdef _SCO_DS |
| 10308 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 10309 | needed definitions in sys/statvfs.h */ |
| 10310 | #define _SVID3 |
| 10311 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10312 | #include <sys/statvfs.h> |
| 10313 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10314 | static PyObject* |
| 10315 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 10316 | PyObject *v = PyStructSequence_New(StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10317 | if (v == NULL) |
| 10318 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10319 | |
| 10320 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10321 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10322 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10323 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 10324 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 10325 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 10326 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 10327 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 10328 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 10329 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10330 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10331 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10332 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10333 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10334 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10335 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10336 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10337 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10338 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10339 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10340 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10341 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10342 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10343 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10344 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10345 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10346 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10347 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10348 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10349 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 10350 | * (issue #32390). */ |
| 10351 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 10352 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 10353 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 10354 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10355 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 10356 | if (PyErr_Occurred()) { |
| 10357 | Py_DECREF(v); |
| 10358 | return NULL; |
| 10359 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10360 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10361 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10362 | } |
| 10363 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10364 | |
| 10365 | /*[clinic input] |
| 10366 | os.fstatvfs |
| 10367 | fd: int |
| 10368 | / |
| 10369 | |
| 10370 | Perform an fstatvfs system call on the given fd. |
| 10371 | |
| 10372 | Equivalent to statvfs(fd). |
| 10373 | [clinic start generated code]*/ |
| 10374 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10375 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10376 | os_fstatvfs_impl(PyObject *module, int fd) |
| 10377 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10378 | { |
| 10379 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10380 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10381 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10382 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10383 | do { |
| 10384 | Py_BEGIN_ALLOW_THREADS |
| 10385 | result = fstatvfs(fd, &st); |
| 10386 | Py_END_ALLOW_THREADS |
| 10387 | } while (result != 0 && errno == EINTR && |
| 10388 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10389 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10390 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10391 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10392 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10393 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10394 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10395 | |
| 10396 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10397 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10398 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10399 | /*[clinic input] |
| 10400 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10401 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10402 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 10403 | |
| 10404 | Perform a statvfs system call on the given path. |
| 10405 | |
| 10406 | path may always be specified as a string. |
| 10407 | On some platforms, path may also be specified as an open file descriptor. |
| 10408 | If this functionality is unavailable, using it raises an exception. |
| 10409 | [clinic start generated code]*/ |
| 10410 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10411 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10412 | os_statvfs_impl(PyObject *module, path_t *path) |
| 10413 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10414 | { |
| 10415 | int result; |
| 10416 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10417 | |
| 10418 | Py_BEGIN_ALLOW_THREADS |
| 10419 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10420 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10421 | #ifdef __APPLE__ |
| 10422 | /* handle weak-linking on Mac OS X 10.3 */ |
| 10423 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10424 | fd_specified("statvfs", path->fd); |
| 10425 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10426 | } |
| 10427 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10428 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10429 | } |
| 10430 | else |
| 10431 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10432 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10433 | Py_END_ALLOW_THREADS |
| 10434 | |
| 10435 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10436 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10437 | } |
| 10438 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10439 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10440 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10441 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 10442 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10443 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10444 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10445 | /*[clinic input] |
| 10446 | os._getdiskusage |
| 10447 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10448 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10449 | |
| 10450 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 10451 | [clinic start generated code]*/ |
| 10452 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10453 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10454 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 10455 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10456 | { |
| 10457 | BOOL retval; |
| 10458 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10459 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10460 | |
| 10461 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10462 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10463 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10464 | if (retval == 0) { |
| 10465 | if (GetLastError() == ERROR_DIRECTORY) { |
| 10466 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10467 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10468 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 10469 | if (dir_path == NULL) { |
| 10470 | return PyErr_NoMemory(); |
| 10471 | } |
| 10472 | |
| 10473 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 10474 | |
| 10475 | if (_dirnameW(dir_path) != -1) { |
| 10476 | Py_BEGIN_ALLOW_THREADS |
| 10477 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 10478 | Py_END_ALLOW_THREADS |
| 10479 | } |
| 10480 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 10481 | err = GetLastError(); |
| 10482 | PyMem_Free(dir_path); |
| 10483 | if (retval) { |
| 10484 | goto success; |
| 10485 | } |
| 10486 | } |
| 10487 | return PyErr_SetFromWindowsErr(err); |
| 10488 | } |
| 10489 | |
| 10490 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10491 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 10492 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10493 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10494 | |
| 10495 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10496 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 10497 | * It maps strings representing configuration variable names to |
| 10498 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10499 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10500 | * rarely-used constants. There are three separate tables that use |
| 10501 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10502 | * |
| 10503 | * This code is always included, even if none of the interfaces that |
| 10504 | * need it are included. The #if hackery needed to avoid it would be |
| 10505 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10506 | */ |
| 10507 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10508 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10509 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10510 | }; |
| 10511 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10512 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10513 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10514 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10515 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10516 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10517 | int value = _PyLong_AsInt(arg); |
| 10518 | if (value == -1 && PyErr_Occurred()) |
| 10519 | return 0; |
| 10520 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10521 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10522 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10523 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10524 | /* look up the value in the table using a binary search */ |
| 10525 | size_t lo = 0; |
| 10526 | size_t mid; |
| 10527 | size_t hi = tablesize; |
| 10528 | int cmp; |
| 10529 | const char *confname; |
| 10530 | if (!PyUnicode_Check(arg)) { |
| 10531 | PyErr_SetString(PyExc_TypeError, |
| 10532 | "configuration names must be strings or integers"); |
| 10533 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10534 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10535 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10536 | if (confname == NULL) |
| 10537 | return 0; |
| 10538 | while (lo < hi) { |
| 10539 | mid = (lo + hi) / 2; |
| 10540 | cmp = strcmp(confname, table[mid].name); |
| 10541 | if (cmp < 0) |
| 10542 | hi = mid; |
| 10543 | else if (cmp > 0) |
| 10544 | lo = mid + 1; |
| 10545 | else { |
| 10546 | *valuep = table[mid].value; |
| 10547 | return 1; |
| 10548 | } |
| 10549 | } |
| 10550 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 10551 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10552 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10553 | } |
| 10554 | |
| 10555 | |
| 10556 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 10557 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10558 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10559 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10560 | #endif |
| 10561 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10562 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10563 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10564 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10565 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10566 | #endif |
| 10567 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10568 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10569 | #endif |
| 10570 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10571 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10572 | #endif |
| 10573 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10574 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10575 | #endif |
| 10576 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10577 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10578 | #endif |
| 10579 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10580 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10581 | #endif |
| 10582 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10583 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10584 | #endif |
| 10585 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10586 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10587 | #endif |
| 10588 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10589 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10590 | #endif |
| 10591 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10592 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10593 | #endif |
| 10594 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10595 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10596 | #endif |
| 10597 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10598 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10599 | #endif |
| 10600 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10601 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10602 | #endif |
| 10603 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10604 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10605 | #endif |
| 10606 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10607 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10608 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10609 | #ifdef _PC_ACL_ENABLED |
| 10610 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10611 | #endif |
| 10612 | #ifdef _PC_MIN_HOLE_SIZE |
| 10613 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10614 | #endif |
| 10615 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10616 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10617 | #endif |
| 10618 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10619 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10620 | #endif |
| 10621 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10622 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10623 | #endif |
| 10624 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10625 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10626 | #endif |
| 10627 | #ifdef _PC_REC_XFER_ALIGN |
| 10628 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10629 | #endif |
| 10630 | #ifdef _PC_SYMLINK_MAX |
| 10631 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 10632 | #endif |
| 10633 | #ifdef _PC_XATTR_ENABLED |
| 10634 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 10635 | #endif |
| 10636 | #ifdef _PC_XATTR_EXISTS |
| 10637 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 10638 | #endif |
| 10639 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 10640 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 10641 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10642 | }; |
| 10643 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10644 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10645 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10646 | { |
| 10647 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 10648 | sizeof(posix_constants_pathconf) |
| 10649 | / sizeof(struct constdef)); |
| 10650 | } |
| 10651 | #endif |
| 10652 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10653 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10654 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10655 | /*[clinic input] |
| 10656 | os.fpathconf -> long |
| 10657 | |
| 10658 | fd: int |
| 10659 | name: path_confname |
| 10660 | / |
| 10661 | |
| 10662 | Return the configuration limit name for the file descriptor fd. |
| 10663 | |
| 10664 | If there is no limit, return -1. |
| 10665 | [clinic start generated code]*/ |
| 10666 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10667 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10668 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 10669 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10670 | { |
| 10671 | long limit; |
| 10672 | |
| 10673 | errno = 0; |
| 10674 | limit = fpathconf(fd, name); |
| 10675 | if (limit == -1 && errno != 0) |
| 10676 | posix_error(); |
| 10677 | |
| 10678 | return limit; |
| 10679 | } |
| 10680 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10681 | |
| 10682 | |
| 10683 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10684 | /*[clinic input] |
| 10685 | os.pathconf -> long |
| 10686 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 10687 | name: path_confname |
| 10688 | |
| 10689 | Return the configuration limit name for the file or directory path. |
| 10690 | |
| 10691 | If there is no limit, return -1. |
| 10692 | On some platforms, path may also be specified as an open file descriptor. |
| 10693 | If this functionality is unavailable, using it raises an exception. |
| 10694 | [clinic start generated code]*/ |
| 10695 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10696 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10697 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 10698 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10699 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10700 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10701 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10702 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10703 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10704 | if (path->fd != -1) |
| 10705 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10706 | else |
| 10707 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10708 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10709 | if (limit == -1 && errno != 0) { |
| 10710 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 10711 | /* could be a path or name problem */ |
| 10712 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10713 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10714 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10715 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10716 | |
| 10717 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10718 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10719 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10720 | |
| 10721 | #ifdef HAVE_CONFSTR |
| 10722 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10723 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10724 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10725 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10726 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10727 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10728 | #endif |
| 10729 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10730 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10731 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10732 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10733 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10734 | #endif |
| 10735 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10736 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10737 | #endif |
| 10738 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10739 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10740 | #endif |
| 10741 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10742 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10743 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10744 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10745 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10746 | #endif |
| 10747 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10748 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10749 | #endif |
| 10750 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10751 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10752 | #endif |
| 10753 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10754 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10755 | #endif |
| 10756 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10757 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10758 | #endif |
| 10759 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10760 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10761 | #endif |
| 10762 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10763 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10764 | #endif |
| 10765 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10766 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10767 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10768 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10769 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10770 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10771 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10772 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10773 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10774 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10775 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10776 | #endif |
| 10777 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10778 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10779 | #endif |
| 10780 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10781 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10782 | #endif |
| 10783 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10784 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10785 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10786 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10787 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10788 | #endif |
| 10789 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10790 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10791 | #endif |
| 10792 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10793 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10794 | #endif |
| 10795 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10796 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10797 | #endif |
| 10798 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10799 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10800 | #endif |
| 10801 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10802 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10803 | #endif |
| 10804 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10805 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10806 | #endif |
| 10807 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10808 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10809 | #endif |
| 10810 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10811 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10812 | #endif |
| 10813 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10814 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10815 | #endif |
| 10816 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10817 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10818 | #endif |
| 10819 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10820 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10821 | #endif |
| 10822 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10823 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10824 | #endif |
| 10825 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10826 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10827 | #endif |
| 10828 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10829 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10830 | #endif |
| 10831 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10832 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10833 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10834 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10835 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10836 | #endif |
| 10837 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10838 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10839 | #endif |
| 10840 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10841 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10842 | #endif |
| 10843 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10844 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10845 | #endif |
| 10846 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10847 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10848 | #endif |
| 10849 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10850 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10851 | #endif |
| 10852 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10853 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10854 | #endif |
| 10855 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10856 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10857 | #endif |
| 10858 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10859 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10860 | #endif |
| 10861 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10862 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10863 | #endif |
| 10864 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10865 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10866 | #endif |
| 10867 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10868 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10869 | #endif |
| 10870 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10871 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10872 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10873 | }; |
| 10874 | |
| 10875 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10876 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10877 | { |
| 10878 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 10879 | sizeof(posix_constants_confstr) |
| 10880 | / sizeof(struct constdef)); |
| 10881 | } |
| 10882 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10883 | |
| 10884 | /*[clinic input] |
| 10885 | os.confstr |
| 10886 | |
| 10887 | name: confstr_confname |
| 10888 | / |
| 10889 | |
| 10890 | Return a string-valued system configuration variable. |
| 10891 | [clinic start generated code]*/ |
| 10892 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10893 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10894 | os_confstr_impl(PyObject *module, int name) |
| 10895 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10896 | { |
| 10897 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10898 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10899 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10900 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10901 | errno = 0; |
| 10902 | len = confstr(name, buffer, sizeof(buffer)); |
| 10903 | if (len == 0) { |
| 10904 | if (errno) { |
| 10905 | posix_error(); |
| 10906 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10907 | } |
| 10908 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10909 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10910 | } |
| 10911 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10912 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10913 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 10914 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10915 | char *buf = PyMem_Malloc(len); |
| 10916 | if (buf == NULL) |
| 10917 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 10918 | len2 = confstr(name, buf, len); |
| 10919 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 10920 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10921 | PyMem_Free(buf); |
| 10922 | } |
| 10923 | else |
| 10924 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10925 | return result; |
| 10926 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10927 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10928 | |
| 10929 | |
| 10930 | #ifdef HAVE_SYSCONF |
| 10931 | static struct constdef posix_constants_sysconf[] = { |
| 10932 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10933 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10934 | #endif |
| 10935 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10936 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10937 | #endif |
| 10938 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10939 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10940 | #endif |
| 10941 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10942 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10943 | #endif |
| 10944 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10945 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10946 | #endif |
| 10947 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10948 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10949 | #endif |
| 10950 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10951 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10952 | #endif |
| 10953 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10954 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10955 | #endif |
| 10956 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10957 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10958 | #endif |
| 10959 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10960 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10961 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10962 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10963 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10964 | #endif |
| 10965 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10966 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10967 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10968 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10969 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10970 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10971 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10972 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10973 | #endif |
| 10974 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10975 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10976 | #endif |
| 10977 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10978 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10979 | #endif |
| 10980 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10981 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10982 | #endif |
| 10983 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10984 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10985 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10986 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10987 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10988 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10989 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10990 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10991 | #endif |
| 10992 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10993 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10994 | #endif |
| 10995 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10996 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10997 | #endif |
| 10998 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10999 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11000 | #endif |
| 11001 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11002 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11003 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11004 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11005 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11006 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11007 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11008 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11009 | #endif |
| 11010 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11011 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11012 | #endif |
| 11013 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11014 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11015 | #endif |
| 11016 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11017 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11018 | #endif |
| 11019 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11020 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11021 | #endif |
| 11022 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11023 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11024 | #endif |
| 11025 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11026 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11027 | #endif |
| 11028 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11029 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11030 | #endif |
| 11031 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11032 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11033 | #endif |
| 11034 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11035 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11036 | #endif |
| 11037 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11038 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11039 | #endif |
| 11040 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11041 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11042 | #endif |
| 11043 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11044 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11045 | #endif |
| 11046 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11047 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11048 | #endif |
| 11049 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11050 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11051 | #endif |
| 11052 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11053 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11054 | #endif |
| 11055 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11056 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11057 | #endif |
| 11058 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11059 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11060 | #endif |
| 11061 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11062 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11063 | #endif |
| 11064 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11065 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11066 | #endif |
| 11067 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11068 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11069 | #endif |
| 11070 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11071 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11072 | #endif |
| 11073 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11074 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11075 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11076 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11077 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11078 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11079 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11080 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11081 | #endif |
| 11082 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11083 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11084 | #endif |
| 11085 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11086 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11087 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11088 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11089 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11090 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11091 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11092 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11093 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11094 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11095 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11096 | #endif |
| 11097 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11098 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11099 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11100 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11101 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11102 | #endif |
| 11103 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11104 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11105 | #endif |
| 11106 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11107 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11108 | #endif |
| 11109 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11110 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11111 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11112 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11113 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11114 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11115 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11116 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11117 | #endif |
| 11118 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11119 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11120 | #endif |
| 11121 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11122 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11123 | #endif |
| 11124 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11125 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11126 | #endif |
| 11127 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11128 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11129 | #endif |
| 11130 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11131 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11132 | #endif |
| 11133 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11134 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11135 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11136 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11137 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11138 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11139 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11140 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11141 | #endif |
| 11142 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11143 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11144 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11145 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11146 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11147 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11148 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11149 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11150 | #endif |
| 11151 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11152 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11153 | #endif |
| 11154 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11155 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11156 | #endif |
| 11157 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11158 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11159 | #endif |
| 11160 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11161 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11162 | #endif |
| 11163 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11164 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11165 | #endif |
| 11166 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11167 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11168 | #endif |
| 11169 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11170 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11171 | #endif |
| 11172 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11173 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11174 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11175 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11176 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11177 | #endif |
| 11178 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11179 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11180 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11181 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11182 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11183 | #endif |
| 11184 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11185 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11186 | #endif |
| 11187 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11188 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11189 | #endif |
| 11190 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11191 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11192 | #endif |
| 11193 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11194 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11195 | #endif |
| 11196 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11197 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11198 | #endif |
| 11199 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11200 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11201 | #endif |
| 11202 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11203 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11204 | #endif |
| 11205 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11206 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11207 | #endif |
| 11208 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11209 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11210 | #endif |
| 11211 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11212 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11213 | #endif |
| 11214 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11215 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11216 | #endif |
| 11217 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11218 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11219 | #endif |
| 11220 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11221 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11222 | #endif |
| 11223 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11224 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11225 | #endif |
| 11226 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11227 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11228 | #endif |
| 11229 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11230 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11231 | #endif |
| 11232 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11233 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11234 | #endif |
| 11235 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11236 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11237 | #endif |
| 11238 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11239 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11240 | #endif |
| 11241 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11242 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11243 | #endif |
| 11244 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11245 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11246 | #endif |
| 11247 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11248 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11249 | #endif |
| 11250 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11251 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11252 | #endif |
| 11253 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11254 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11255 | #endif |
| 11256 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11257 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11258 | #endif |
| 11259 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11260 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11261 | #endif |
| 11262 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11263 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11264 | #endif |
| 11265 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11266 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11267 | #endif |
| 11268 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11269 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11270 | #endif |
| 11271 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11272 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11273 | #endif |
| 11274 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11275 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11276 | #endif |
| 11277 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11278 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11279 | #endif |
| 11280 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11281 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11282 | #endif |
| 11283 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11284 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11285 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11286 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11287 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11288 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11289 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11290 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11291 | #endif |
| 11292 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11293 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11294 | #endif |
| 11295 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11296 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11297 | #endif |
| 11298 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11299 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11300 | #endif |
| 11301 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11302 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11303 | #endif |
| 11304 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11305 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11306 | #endif |
| 11307 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11308 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11309 | #endif |
| 11310 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11311 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11312 | #endif |
| 11313 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11314 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11315 | #endif |
| 11316 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11317 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11318 | #endif |
| 11319 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11320 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11321 | #endif |
| 11322 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11323 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11324 | #endif |
| 11325 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11326 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11327 | #endif |
| 11328 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11329 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11330 | #endif |
| 11331 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11332 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11333 | #endif |
| 11334 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11335 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11336 | #endif |
| 11337 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11338 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11339 | #endif |
| 11340 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11341 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11342 | #endif |
| 11343 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11344 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11345 | #endif |
| 11346 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11347 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11348 | #endif |
| 11349 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11350 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11351 | #endif |
| 11352 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11353 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11354 | #endif |
| 11355 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11356 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11357 | #endif |
| 11358 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11359 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11360 | #endif |
| 11361 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11362 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11363 | #endif |
| 11364 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11365 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11366 | #endif |
| 11367 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11368 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11369 | #endif |
| 11370 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11371 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11372 | #endif |
| 11373 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11374 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11375 | #endif |
| 11376 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11377 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11378 | #endif |
| 11379 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11380 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11381 | #endif |
| 11382 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11383 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11384 | #endif |
| 11385 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11386 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11387 | #endif |
| 11388 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11389 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11390 | #endif |
| 11391 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11392 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11393 | #endif |
| 11394 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11395 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11396 | #endif |
| 11397 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11398 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11399 | #endif |
| 11400 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11401 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11402 | #endif |
| 11403 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11404 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11405 | #endif |
| 11406 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11407 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11408 | #endif |
| 11409 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11410 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11411 | #endif |
| 11412 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11413 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11414 | #endif |
| 11415 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11416 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11417 | #endif |
| 11418 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11419 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11420 | #endif |
| 11421 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11422 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11423 | #endif |
| 11424 | }; |
| 11425 | |
| 11426 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11427 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11428 | { |
| 11429 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 11430 | sizeof(posix_constants_sysconf) |
| 11431 | / sizeof(struct constdef)); |
| 11432 | } |
| 11433 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11434 | |
| 11435 | /*[clinic input] |
| 11436 | os.sysconf -> long |
| 11437 | name: sysconf_confname |
| 11438 | / |
| 11439 | |
| 11440 | Return an integer-valued system configuration variable. |
| 11441 | [clinic start generated code]*/ |
| 11442 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11443 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11444 | os_sysconf_impl(PyObject *module, int name) |
| 11445 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11446 | { |
| 11447 | long value; |
| 11448 | |
| 11449 | errno = 0; |
| 11450 | value = sysconf(name); |
| 11451 | if (value == -1 && errno != 0) |
| 11452 | posix_error(); |
| 11453 | return value; |
| 11454 | } |
| 11455 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11456 | |
| 11457 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11458 | /* 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] | 11459 | * 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] | 11460 | * the exported dictionaries that are used to publish information about the |
| 11461 | * names available on the host platform. |
| 11462 | * |
| 11463 | * Sorting the table at runtime ensures that the table is properly ordered |
| 11464 | * when used, even for platforms we're not able to test on. It also makes |
| 11465 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11466 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11467 | |
| 11468 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11469 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11470 | { |
| 11471 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11472 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11473 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11474 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11475 | |
| 11476 | return strcmp(c1->name, c2->name); |
| 11477 | } |
| 11478 | |
| 11479 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11480 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11481 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11482 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11483 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11484 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11485 | |
| 11486 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 11487 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11488 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11489 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11490 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11491 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11492 | PyObject *o = PyLong_FromLong(table[i].value); |
| 11493 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 11494 | Py_XDECREF(o); |
| 11495 | Py_DECREF(d); |
| 11496 | return -1; |
| 11497 | } |
| 11498 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11499 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11500 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11501 | } |
| 11502 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11503 | /* Return -1 on failure, 0 on success. */ |
| 11504 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11505 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11506 | { |
| 11507 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11508 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11509 | sizeof(posix_constants_pathconf) |
| 11510 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11511 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11512 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11513 | #endif |
| 11514 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11515 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11516 | sizeof(posix_constants_confstr) |
| 11517 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11518 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11519 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11520 | #endif |
| 11521 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11522 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11523 | sizeof(posix_constants_sysconf) |
| 11524 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11525 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11526 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11527 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11528 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11529 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11530 | |
| 11531 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11532 | /*[clinic input] |
| 11533 | os.abort |
| 11534 | |
| 11535 | Abort the interpreter immediately. |
| 11536 | |
| 11537 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 11538 | on the hosting operating system. This function never returns. |
| 11539 | [clinic start generated code]*/ |
| 11540 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11541 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11542 | os_abort_impl(PyObject *module) |
| 11543 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11544 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11545 | abort(); |
| 11546 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11547 | #ifndef __clang__ |
| 11548 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 11549 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 11550 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11551 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 11552 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11553 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11554 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11555 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11556 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11557 | /* Grab ShellExecute dynamically from shell32 */ |
| 11558 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11559 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 11560 | LPCWSTR, INT); |
| 11561 | static int |
| 11562 | check_ShellExecute() |
| 11563 | { |
| 11564 | HINSTANCE hShell32; |
| 11565 | |
| 11566 | /* only recheck */ |
| 11567 | if (-1 == has_ShellExecute) { |
| 11568 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11569 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 11570 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 11571 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 11572 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11573 | hShell32 = LoadLibraryW(L"SHELL32"); |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11574 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11575 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 11576 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11577 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11578 | } else { |
| 11579 | has_ShellExecute = 0; |
| 11580 | } |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 11581 | Py_END_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11582 | } |
| 11583 | return has_ShellExecute; |
| 11584 | } |
| 11585 | |
| 11586 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11587 | /*[clinic input] |
| 11588 | os.startfile |
| 11589 | filepath: path_t |
| 11590 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11591 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11592 | Start a file with its associated application. |
| 11593 | |
| 11594 | When "operation" is not specified or "open", this acts like |
| 11595 | double-clicking the file in Explorer, or giving the file name as an |
| 11596 | argument to the DOS "start" command: the file is opened with whatever |
| 11597 | application (if any) its extension is associated. |
| 11598 | When another "operation" is given, it specifies what should be done with |
| 11599 | the file. A typical operation is "print". |
| 11600 | |
| 11601 | startfile returns as soon as the associated application is launched. |
| 11602 | There is no option to wait for the application to close, and no way |
| 11603 | to retrieve the application's exit status. |
| 11604 | |
| 11605 | The filepath is relative to the current directory. If you want to use |
| 11606 | an absolute path, make sure the first character is not a slash ("/"); |
| 11607 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 11608 | [clinic start generated code]*/ |
| 11609 | |
| 11610 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11611 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 11612 | const Py_UNICODE *operation) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 11613 | /*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11614 | { |
| 11615 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11616 | |
| 11617 | if(!check_ShellExecute()) { |
| 11618 | /* If the OS doesn't have ShellExecute, return a |
| 11619 | NotImplementedError. */ |
| 11620 | return PyErr_Format(PyExc_NotImplementedError, |
| 11621 | "startfile not available on this platform"); |
| 11622 | } |
| 11623 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11624 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11625 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11626 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11627 | Py_END_ALLOW_THREADS |
| 11628 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11629 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11630 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11631 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11632 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11633 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11634 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11635 | #endif /* MS_WINDOWS */ |
| 11636 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11637 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11638 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11639 | /*[clinic input] |
| 11640 | os.getloadavg |
| 11641 | |
| 11642 | Return average recent system load information. |
| 11643 | |
| 11644 | Return the number of processes in the system run queue averaged over |
| 11645 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 11646 | Raises OSError if the load average was unobtainable. |
| 11647 | [clinic start generated code]*/ |
| 11648 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11649 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11650 | os_getloadavg_impl(PyObject *module) |
| 11651 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11652 | { |
| 11653 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11654 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11655 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 11656 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11657 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11658 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11659 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11660 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11661 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11662 | |
| 11663 | /*[clinic input] |
| 11664 | os.device_encoding |
| 11665 | fd: int |
| 11666 | |
| 11667 | Return a string describing the encoding of a terminal's file descriptor. |
| 11668 | |
| 11669 | The file descriptor must be attached to a terminal. |
| 11670 | If the device is not a terminal, return None. |
| 11671 | [clinic start generated code]*/ |
| 11672 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11673 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11674 | os_device_encoding_impl(PyObject *module, int fd) |
| 11675 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11676 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 11677 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 11678 | } |
| 11679 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11680 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11681 | #ifdef HAVE_SETRESUID |
| 11682 | /*[clinic input] |
| 11683 | os.setresuid |
| 11684 | |
| 11685 | ruid: uid_t |
| 11686 | euid: uid_t |
| 11687 | suid: uid_t |
| 11688 | / |
| 11689 | |
| 11690 | Set the current process's real, effective, and saved user ids. |
| 11691 | [clinic start generated code]*/ |
| 11692 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11693 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11694 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 11695 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11696 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11697 | if (setresuid(ruid, euid, suid) < 0) |
| 11698 | return posix_error(); |
| 11699 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11700 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11701 | #endif /* HAVE_SETRESUID */ |
| 11702 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11703 | |
| 11704 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11705 | /*[clinic input] |
| 11706 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11707 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11708 | rgid: gid_t |
| 11709 | egid: gid_t |
| 11710 | sgid: gid_t |
| 11711 | / |
| 11712 | |
| 11713 | Set the current process's real, effective, and saved group ids. |
| 11714 | [clinic start generated code]*/ |
| 11715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11716 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11717 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 11718 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11719 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11720 | if (setresgid(rgid, egid, sgid) < 0) |
| 11721 | return posix_error(); |
| 11722 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11723 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11724 | #endif /* HAVE_SETRESGID */ |
| 11725 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11726 | |
| 11727 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11728 | /*[clinic input] |
| 11729 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11730 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11731 | Return a tuple of the current process's real, effective, and saved user ids. |
| 11732 | [clinic start generated code]*/ |
| 11733 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11734 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11735 | os_getresuid_impl(PyObject *module) |
| 11736 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11737 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11738 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11739 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 11740 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11741 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 11742 | _PyLong_FromUid(euid), |
| 11743 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11744 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11745 | #endif /* HAVE_GETRESUID */ |
| 11746 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11747 | |
| 11748 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11749 | /*[clinic input] |
| 11750 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11751 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11752 | Return a tuple of the current process's real, effective, and saved group ids. |
| 11753 | [clinic start generated code]*/ |
| 11754 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11755 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11756 | os_getresgid_impl(PyObject *module) |
| 11757 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11758 | { |
| 11759 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11760 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 11761 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11762 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 11763 | _PyLong_FromGid(egid), |
| 11764 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11765 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11766 | #endif /* HAVE_GETRESGID */ |
| 11767 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11768 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11769 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11770 | /*[clinic input] |
| 11771 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11772 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11773 | path: path_t(allow_fd=True) |
| 11774 | attribute: path_t |
| 11775 | * |
| 11776 | follow_symlinks: bool = True |
| 11777 | |
| 11778 | Return the value of extended attribute attribute on path. |
| 11779 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11780 | 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] | 11781 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11782 | link, getxattr will examine the symbolic link itself instead of the file |
| 11783 | the link points to. |
| 11784 | |
| 11785 | [clinic start generated code]*/ |
| 11786 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11787 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11788 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11789 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11790 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11791 | { |
| 11792 | Py_ssize_t i; |
| 11793 | PyObject *buffer = NULL; |
| 11794 | |
| 11795 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 11796 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11797 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11798 | for (i = 0; ; i++) { |
| 11799 | void *ptr; |
| 11800 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11801 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11802 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11803 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11804 | path_error(path); |
| 11805 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11806 | } |
| 11807 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 11808 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11809 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11810 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11811 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11812 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11813 | if (path->fd >= 0) |
| 11814 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11815 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11816 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11817 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11818 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11819 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11820 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11821 | if (result < 0) { |
| 11822 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11823 | if (errno == ERANGE) |
| 11824 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11825 | path_error(path); |
| 11826 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11827 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11828 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11829 | if (result != buffer_size) { |
| 11830 | /* Can only shrink. */ |
| 11831 | _PyBytes_Resize(&buffer, result); |
| 11832 | } |
| 11833 | break; |
| 11834 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11835 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11836 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11837 | } |
| 11838 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11839 | |
| 11840 | /*[clinic input] |
| 11841 | os.setxattr |
| 11842 | |
| 11843 | path: path_t(allow_fd=True) |
| 11844 | attribute: path_t |
| 11845 | value: Py_buffer |
| 11846 | flags: int = 0 |
| 11847 | * |
| 11848 | follow_symlinks: bool = True |
| 11849 | |
| 11850 | Set extended attribute attribute on path to value. |
| 11851 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11852 | 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] | 11853 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11854 | link, setxattr will modify the symbolic link itself instead of the file |
| 11855 | the link points to. |
| 11856 | |
| 11857 | [clinic start generated code]*/ |
| 11858 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11859 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11860 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11861 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11862 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11863 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11864 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11865 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11866 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11867 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11868 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11869 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11870 | if (path->fd > -1) |
| 11871 | result = fsetxattr(path->fd, attribute->narrow, |
| 11872 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11873 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11874 | result = setxattr(path->narrow, attribute->narrow, |
| 11875 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11876 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11877 | result = lsetxattr(path->narrow, attribute->narrow, |
| 11878 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11879 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11880 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11881 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11882 | path_error(path); |
| 11883 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11884 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11885 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11886 | Py_RETURN_NONE; |
| 11887 | } |
| 11888 | |
| 11889 | |
| 11890 | /*[clinic input] |
| 11891 | os.removexattr |
| 11892 | |
| 11893 | path: path_t(allow_fd=True) |
| 11894 | attribute: path_t |
| 11895 | * |
| 11896 | follow_symlinks: bool = True |
| 11897 | |
| 11898 | Remove extended attribute attribute on path. |
| 11899 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11900 | 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] | 11901 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11902 | link, removexattr will modify the symbolic link itself instead of the file |
| 11903 | the link points to. |
| 11904 | |
| 11905 | [clinic start generated code]*/ |
| 11906 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11907 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11908 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11909 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11910 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11911 | { |
| 11912 | ssize_t result; |
| 11913 | |
| 11914 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 11915 | return NULL; |
| 11916 | |
| 11917 | Py_BEGIN_ALLOW_THREADS; |
| 11918 | if (path->fd > -1) |
| 11919 | result = fremovexattr(path->fd, attribute->narrow); |
| 11920 | else if (follow_symlinks) |
| 11921 | result = removexattr(path->narrow, attribute->narrow); |
| 11922 | else |
| 11923 | result = lremovexattr(path->narrow, attribute->narrow); |
| 11924 | Py_END_ALLOW_THREADS; |
| 11925 | |
| 11926 | if (result) { |
| 11927 | return path_error(path); |
| 11928 | } |
| 11929 | |
| 11930 | Py_RETURN_NONE; |
| 11931 | } |
| 11932 | |
| 11933 | |
| 11934 | /*[clinic input] |
| 11935 | os.listxattr |
| 11936 | |
| 11937 | path: path_t(allow_fd=True, nullable=True) = None |
| 11938 | * |
| 11939 | follow_symlinks: bool = True |
| 11940 | |
| 11941 | Return a list of extended attributes on path. |
| 11942 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11943 | 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] | 11944 | if path is None, listxattr will examine the current directory. |
| 11945 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11946 | link, listxattr will examine the symbolic link itself instead of the file |
| 11947 | the link points to. |
| 11948 | [clinic start generated code]*/ |
| 11949 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11950 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11951 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11952 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11953 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11954 | Py_ssize_t i; |
| 11955 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11956 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11957 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11958 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11959 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11960 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11961 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11962 | name = path->narrow ? path->narrow : "."; |
| 11963 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11964 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11965 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11966 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11967 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11968 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11969 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 11970 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11971 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11972 | break; |
| 11973 | } |
| 11974 | buffer = PyMem_MALLOC(buffer_size); |
| 11975 | if (!buffer) { |
| 11976 | PyErr_NoMemory(); |
| 11977 | break; |
| 11978 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11979 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11980 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11981 | if (path->fd > -1) |
| 11982 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11983 | else if (follow_symlinks) |
| 11984 | length = listxattr(name, buffer, buffer_size); |
| 11985 | else |
| 11986 | length = llistxattr(name, buffer, buffer_size); |
| 11987 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11988 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11989 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11990 | if (errno == ERANGE) { |
| 11991 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 11992 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11993 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11994 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11995 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11996 | break; |
| 11997 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11998 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11999 | result = PyList_New(0); |
| 12000 | if (!result) { |
| 12001 | goto exit; |
| 12002 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12003 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12004 | end = buffer + length; |
| 12005 | for (trace = start = buffer; trace != end; trace++) { |
| 12006 | if (!*trace) { |
| 12007 | int error; |
| 12008 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 12009 | trace - start); |
| 12010 | if (!attribute) { |
| 12011 | Py_DECREF(result); |
| 12012 | result = NULL; |
| 12013 | goto exit; |
| 12014 | } |
| 12015 | error = PyList_Append(result, attribute); |
| 12016 | Py_DECREF(attribute); |
| 12017 | if (error) { |
| 12018 | Py_DECREF(result); |
| 12019 | result = NULL; |
| 12020 | goto exit; |
| 12021 | } |
| 12022 | start = trace + 1; |
| 12023 | } |
| 12024 | } |
| 12025 | break; |
| 12026 | } |
| 12027 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12028 | if (buffer) |
| 12029 | PyMem_FREE(buffer); |
| 12030 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12031 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12032 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12033 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12034 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12035 | /*[clinic input] |
| 12036 | os.urandom |
| 12037 | |
| 12038 | size: Py_ssize_t |
| 12039 | / |
| 12040 | |
| 12041 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 12042 | [clinic start generated code]*/ |
| 12043 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12044 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12045 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 12046 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12047 | { |
| 12048 | PyObject *bytes; |
| 12049 | int result; |
| 12050 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12051 | if (size < 0) |
| 12052 | return PyErr_Format(PyExc_ValueError, |
| 12053 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12054 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 12055 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12056 | return NULL; |
| 12057 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 12058 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12059 | if (result == -1) { |
| 12060 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12061 | return NULL; |
| 12062 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12063 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12064 | } |
| 12065 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 12066 | #ifdef HAVE_MEMFD_CREATE |
| 12067 | /*[clinic input] |
| 12068 | os.memfd_create |
| 12069 | |
| 12070 | name: FSConverter |
| 12071 | flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC |
| 12072 | |
| 12073 | [clinic start generated code]*/ |
| 12074 | |
| 12075 | static PyObject * |
| 12076 | os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags) |
| 12077 | /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/ |
| 12078 | { |
| 12079 | int fd; |
| 12080 | const char *bytes = PyBytes_AS_STRING(name); |
| 12081 | Py_BEGIN_ALLOW_THREADS |
| 12082 | fd = memfd_create(bytes, flags); |
| 12083 | Py_END_ALLOW_THREADS |
| 12084 | if (fd == -1) { |
| 12085 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12086 | } |
| 12087 | return PyLong_FromLong(fd); |
| 12088 | } |
| 12089 | #endif |
| 12090 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12091 | /* Terminal size querying */ |
| 12092 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 12093 | static PyTypeObject* TerminalSizeType; |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12094 | |
| 12095 | PyDoc_STRVAR(TerminalSize_docstring, |
| 12096 | "A tuple of (columns, lines) for holding terminal window size"); |
| 12097 | |
| 12098 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 12099 | {"columns", "width of the terminal window in characters"}, |
| 12100 | {"lines", "height of the terminal window in characters"}, |
| 12101 | {NULL, NULL} |
| 12102 | }; |
| 12103 | |
| 12104 | static PyStructSequence_Desc TerminalSize_desc = { |
| 12105 | "os.terminal_size", |
| 12106 | TerminalSize_docstring, |
| 12107 | TerminalSize_fields, |
| 12108 | 2, |
| 12109 | }; |
| 12110 | |
| 12111 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12112 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12113 | PyDoc_STRVAR(termsize__doc__, |
| 12114 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 12115 | "\n" \ |
| 12116 | "The optional argument fd (default standard output) specifies\n" \ |
| 12117 | "which file descriptor should be queried.\n" \ |
| 12118 | "\n" \ |
| 12119 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 12120 | "is thrown.\n" \ |
| 12121 | "\n" \ |
| 12122 | "This function will only be defined if an implementation is\n" \ |
| 12123 | "available for this system.\n" \ |
| 12124 | "\n" \ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 12125 | "shutil.get_terminal_size is the high-level function which should\n" \ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12126 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 12127 | |
| 12128 | static PyObject* |
| 12129 | get_terminal_size(PyObject *self, PyObject *args) |
| 12130 | { |
| 12131 | int columns, lines; |
| 12132 | PyObject *termsize; |
| 12133 | |
| 12134 | int fd = fileno(stdout); |
| 12135 | /* Under some conditions stdout may not be connected and |
| 12136 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 12137 | * GUI apps don't have valid standard streams by default. |
| 12138 | * |
| 12139 | * If this happens, and the optional fd argument is not present, |
| 12140 | * the ioctl below will fail returning EBADF. This is what we want. |
| 12141 | */ |
| 12142 | |
| 12143 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 12144 | return NULL; |
| 12145 | |
| 12146 | #ifdef TERMSIZE_USE_IOCTL |
| 12147 | { |
| 12148 | struct winsize w; |
| 12149 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 12150 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12151 | columns = w.ws_col; |
| 12152 | lines = w.ws_row; |
| 12153 | } |
| 12154 | #endif /* TERMSIZE_USE_IOCTL */ |
| 12155 | |
| 12156 | #ifdef TERMSIZE_USE_CONIO |
| 12157 | { |
| 12158 | DWORD nhandle; |
| 12159 | HANDLE handle; |
| 12160 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 12161 | switch (fd) { |
| 12162 | case 0: nhandle = STD_INPUT_HANDLE; |
| 12163 | break; |
| 12164 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 12165 | break; |
| 12166 | case 2: nhandle = STD_ERROR_HANDLE; |
| 12167 | break; |
| 12168 | default: |
| 12169 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 12170 | } |
| 12171 | handle = GetStdHandle(nhandle); |
| 12172 | if (handle == NULL) |
| 12173 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 12174 | if (handle == INVALID_HANDLE_VALUE) |
| 12175 | return PyErr_SetFromWindowsErr(0); |
| 12176 | |
| 12177 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 12178 | return PyErr_SetFromWindowsErr(0); |
| 12179 | |
| 12180 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 12181 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 12182 | } |
| 12183 | #endif /* TERMSIZE_USE_CONIO */ |
| 12184 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 12185 | termsize = PyStructSequence_New(TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12186 | if (termsize == NULL) |
| 12187 | return NULL; |
| 12188 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 12189 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 12190 | if (PyErr_Occurred()) { |
| 12191 | Py_DECREF(termsize); |
| 12192 | return NULL; |
| 12193 | } |
| 12194 | return termsize; |
| 12195 | } |
| 12196 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 12197 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12198 | |
| 12199 | /*[clinic input] |
| 12200 | os.cpu_count |
| 12201 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 12202 | Return the number of CPUs in the system; return None if indeterminable. |
| 12203 | |
| 12204 | This number is not equivalent to the number of CPUs the current process can |
| 12205 | use. The number of usable CPUs can be obtained with |
| 12206 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12207 | [clinic start generated code]*/ |
| 12208 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12209 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12210 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12211 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12212 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12213 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12214 | #ifdef MS_WINDOWS |
Steve Dower | aa92927 | 2019-09-11 16:15:39 +0100 | [diff] [blame] | 12215 | ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12216 | #elif defined(__hpux) |
| 12217 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 12218 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 12219 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12220 | #elif defined(__DragonFly__) || \ |
| 12221 | defined(__OpenBSD__) || \ |
| 12222 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12223 | defined(__NetBSD__) || \ |
| 12224 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 12225 | int mib[2]; |
| 12226 | size_t len = sizeof(ncpu); |
| 12227 | mib[0] = CTL_HW; |
| 12228 | mib[1] = HW_NCPU; |
| 12229 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 12230 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12231 | #endif |
| 12232 | if (ncpu >= 1) |
| 12233 | return PyLong_FromLong(ncpu); |
| 12234 | else |
| 12235 | Py_RETURN_NONE; |
| 12236 | } |
| 12237 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12238 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12239 | /*[clinic input] |
| 12240 | os.get_inheritable -> bool |
| 12241 | |
| 12242 | fd: int |
| 12243 | / |
| 12244 | |
| 12245 | Get the close-on-exe flag of the specified file descriptor. |
| 12246 | [clinic start generated code]*/ |
| 12247 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12248 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12249 | os_get_inheritable_impl(PyObject *module, int fd) |
| 12250 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12251 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12252 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12253 | _Py_BEGIN_SUPPRESS_IPH |
| 12254 | return_value = _Py_get_inheritable(fd); |
| 12255 | _Py_END_SUPPRESS_IPH |
| 12256 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12257 | } |
| 12258 | |
| 12259 | |
| 12260 | /*[clinic input] |
| 12261 | os.set_inheritable |
| 12262 | fd: int |
| 12263 | inheritable: int |
| 12264 | / |
| 12265 | |
| 12266 | Set the inheritable flag of the specified file descriptor. |
| 12267 | [clinic start generated code]*/ |
| 12268 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12269 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12270 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 12271 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12272 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12273 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12274 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12275 | _Py_BEGIN_SUPPRESS_IPH |
| 12276 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 12277 | _Py_END_SUPPRESS_IPH |
| 12278 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12279 | return NULL; |
| 12280 | Py_RETURN_NONE; |
| 12281 | } |
| 12282 | |
| 12283 | |
| 12284 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12285 | /*[clinic input] |
| 12286 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12287 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12288 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12290 | Get the close-on-exe flag of the specified file descriptor. |
| 12291 | [clinic start generated code]*/ |
| 12292 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12293 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12294 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12295 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12296 | { |
| 12297 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12298 | |
| 12299 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 12300 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12301 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12302 | } |
| 12303 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12304 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12305 | } |
| 12306 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12307 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12308 | /*[clinic input] |
| 12309 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12310 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12311 | inheritable: bool |
| 12312 | / |
| 12313 | |
| 12314 | Set the inheritable flag of the specified handle. |
| 12315 | [clinic start generated code]*/ |
| 12316 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12317 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12318 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12319 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12320 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12321 | { |
| 12322 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12323 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 12324 | PyErr_SetFromWindowsErr(0); |
| 12325 | return NULL; |
| 12326 | } |
| 12327 | Py_RETURN_NONE; |
| 12328 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12329 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12330 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12331 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12332 | /*[clinic input] |
| 12333 | os.get_blocking -> bool |
| 12334 | fd: int |
| 12335 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12336 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12337 | Get the blocking mode of the file descriptor. |
| 12338 | |
| 12339 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 12340 | [clinic start generated code]*/ |
| 12341 | |
| 12342 | static int |
| 12343 | os_get_blocking_impl(PyObject *module, int fd) |
| 12344 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12345 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12346 | int blocking; |
| 12347 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12348 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12349 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12350 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12351 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12352 | } |
| 12353 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12354 | /*[clinic input] |
| 12355 | os.set_blocking |
| 12356 | fd: int |
| 12357 | blocking: bool(accept={int}) |
| 12358 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12359 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12360 | Set the blocking mode of the specified file descriptor. |
| 12361 | |
| 12362 | Set the O_NONBLOCK flag if blocking is False, |
| 12363 | clear the O_NONBLOCK flag otherwise. |
| 12364 | [clinic start generated code]*/ |
| 12365 | |
| 12366 | static PyObject * |
| 12367 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 12368 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12369 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12370 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12371 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12372 | _Py_BEGIN_SUPPRESS_IPH |
| 12373 | result = _Py_set_blocking(fd, blocking); |
| 12374 | _Py_END_SUPPRESS_IPH |
| 12375 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12376 | return NULL; |
| 12377 | Py_RETURN_NONE; |
| 12378 | } |
| 12379 | #endif /* !MS_WINDOWS */ |
| 12380 | |
| 12381 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12382 | /*[clinic input] |
| 12383 | class os.DirEntry "DirEntry *" "&DirEntryType" |
| 12384 | [clinic start generated code]*/ |
| 12385 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12386 | |
| 12387 | typedef struct { |
| 12388 | PyObject_HEAD |
| 12389 | PyObject *name; |
| 12390 | PyObject *path; |
| 12391 | PyObject *stat; |
| 12392 | PyObject *lstat; |
| 12393 | #ifdef MS_WINDOWS |
| 12394 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12395 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12396 | int got_file_index; |
| 12397 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12398 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12399 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12400 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12401 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12402 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12403 | #endif |
| 12404 | } DirEntry; |
| 12405 | |
| 12406 | static void |
| 12407 | DirEntry_dealloc(DirEntry *entry) |
| 12408 | { |
| 12409 | Py_XDECREF(entry->name); |
| 12410 | Py_XDECREF(entry->path); |
| 12411 | Py_XDECREF(entry->stat); |
| 12412 | Py_XDECREF(entry->lstat); |
| 12413 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 12414 | } |
| 12415 | |
| 12416 | /* Forward reference */ |
| 12417 | static int |
| 12418 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 12419 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12420 | /*[clinic input] |
| 12421 | os.DirEntry.is_symlink -> bool |
| 12422 | |
| 12423 | Return True if the entry is a symbolic link; cached per entry. |
| 12424 | [clinic start generated code]*/ |
| 12425 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12426 | static int |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12427 | os_DirEntry_is_symlink_impl(DirEntry *self) |
| 12428 | /*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12429 | { |
| 12430 | #ifdef MS_WINDOWS |
| 12431 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12432 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 12433 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12434 | if (self->d_type != DT_UNKNOWN) |
| 12435 | return self->d_type == DT_LNK; |
| 12436 | else |
| 12437 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12438 | #else |
| 12439 | /* POSIX without d_type */ |
| 12440 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12441 | #endif |
| 12442 | } |
| 12443 | |
| 12444 | static PyObject * |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12445 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 12446 | { |
| 12447 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12448 | STRUCT_STAT st; |
| 12449 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12450 | |
| 12451 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12452 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 12453 | return NULL; |
| 12454 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12455 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12456 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 12457 | return NULL; |
| 12458 | const char *path = PyBytes_AS_STRING(ub); |
| 12459 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 12460 | #ifdef HAVE_FSTATAT |
| 12461 | result = fstatat(self->dir_fd, path, &st, |
| 12462 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 12463 | #else |
| 12464 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 12465 | return NULL; |
| 12466 | #endif /* HAVE_FSTATAT */ |
| 12467 | } |
| 12468 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12469 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12470 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12471 | if (follow_symlinks) |
| 12472 | result = STAT(path, &st); |
| 12473 | else |
| 12474 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12475 | } |
| 12476 | Py_DECREF(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12477 | |
| 12478 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12479 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12480 | |
| 12481 | return _pystat_fromstructstat(&st); |
| 12482 | } |
| 12483 | |
| 12484 | static PyObject * |
| 12485 | DirEntry_get_lstat(DirEntry *self) |
| 12486 | { |
| 12487 | if (!self->lstat) { |
| 12488 | #ifdef MS_WINDOWS |
| 12489 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 12490 | #else /* POSIX */ |
| 12491 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 12492 | #endif |
| 12493 | } |
| 12494 | Py_XINCREF(self->lstat); |
| 12495 | return self->lstat; |
| 12496 | } |
| 12497 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12498 | /*[clinic input] |
| 12499 | os.DirEntry.stat |
| 12500 | * |
| 12501 | follow_symlinks: bool = True |
| 12502 | |
| 12503 | Return stat_result object for the entry; cached per entry. |
| 12504 | [clinic start generated code]*/ |
| 12505 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12506 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12507 | os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks) |
| 12508 | /*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12509 | { |
| 12510 | if (!follow_symlinks) |
| 12511 | return DirEntry_get_lstat(self); |
| 12512 | |
| 12513 | if (!self->stat) { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12514 | int result = os_DirEntry_is_symlink_impl(self); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12515 | if (result == -1) |
| 12516 | return NULL; |
| 12517 | else if (result) |
| 12518 | self->stat = DirEntry_fetch_stat(self, 1); |
| 12519 | else |
| 12520 | self->stat = DirEntry_get_lstat(self); |
| 12521 | } |
| 12522 | |
| 12523 | Py_XINCREF(self->stat); |
| 12524 | return self->stat; |
| 12525 | } |
| 12526 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12527 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 12528 | static int |
| 12529 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 12530 | { |
| 12531 | PyObject *stat = NULL; |
| 12532 | PyObject *st_mode = NULL; |
| 12533 | long mode; |
| 12534 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12535 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12536 | int is_symlink; |
| 12537 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12538 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12539 | #ifdef MS_WINDOWS |
| 12540 | unsigned long dir_bits; |
| 12541 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12542 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12543 | |
| 12544 | #ifdef MS_WINDOWS |
| 12545 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 12546 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12547 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12548 | is_symlink = self->d_type == DT_LNK; |
| 12549 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 12550 | #endif |
| 12551 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12552 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12553 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12554 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12555 | stat = os_DirEntry_stat_impl(self, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12556 | if (!stat) { |
| 12557 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 12558 | /* If file doesn't exist (anymore), then return False |
| 12559 | (i.e., say it's not a file/directory) */ |
| 12560 | PyErr_Clear(); |
| 12561 | return 0; |
| 12562 | } |
| 12563 | goto error; |
| 12564 | } |
| 12565 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 12566 | if (!st_mode) |
| 12567 | goto error; |
| 12568 | |
| 12569 | mode = PyLong_AsLong(st_mode); |
| 12570 | if (mode == -1 && PyErr_Occurred()) |
| 12571 | goto error; |
| 12572 | Py_CLEAR(st_mode); |
| 12573 | Py_CLEAR(stat); |
| 12574 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12575 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12576 | } |
| 12577 | else if (is_symlink) { |
| 12578 | assert(mode_bits != S_IFLNK); |
| 12579 | result = 0; |
| 12580 | } |
| 12581 | else { |
| 12582 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 12583 | #ifdef MS_WINDOWS |
| 12584 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 12585 | if (mode_bits == S_IFDIR) |
| 12586 | result = dir_bits != 0; |
| 12587 | else |
| 12588 | result = dir_bits == 0; |
| 12589 | #else /* POSIX */ |
| 12590 | if (mode_bits == S_IFDIR) |
| 12591 | result = self->d_type == DT_DIR; |
| 12592 | else |
| 12593 | result = self->d_type == DT_REG; |
| 12594 | #endif |
| 12595 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12596 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12597 | |
| 12598 | return result; |
| 12599 | |
| 12600 | error: |
| 12601 | Py_XDECREF(st_mode); |
| 12602 | Py_XDECREF(stat); |
| 12603 | return -1; |
| 12604 | } |
| 12605 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12606 | /*[clinic input] |
| 12607 | os.DirEntry.is_dir -> bool |
| 12608 | * |
| 12609 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12610 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12611 | Return True if the entry is a directory; cached per entry. |
| 12612 | [clinic start generated code]*/ |
| 12613 | |
| 12614 | static int |
| 12615 | os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks) |
| 12616 | /*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/ |
| 12617 | { |
| 12618 | return DirEntry_test_mode(self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12619 | } |
| 12620 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12621 | /*[clinic input] |
| 12622 | os.DirEntry.is_file -> bool |
| 12623 | * |
| 12624 | follow_symlinks: bool = True |
| 12625 | |
| 12626 | Return True if the entry is a file; cached per entry. |
| 12627 | [clinic start generated code]*/ |
| 12628 | |
| 12629 | static int |
| 12630 | os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks) |
| 12631 | /*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12632 | { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12633 | return DirEntry_test_mode(self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12634 | } |
| 12635 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12636 | /*[clinic input] |
| 12637 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12638 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12639 | Return inode of the entry; cached per entry. |
| 12640 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12641 | |
| 12642 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12643 | os_DirEntry_inode_impl(DirEntry *self) |
| 12644 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12645 | { |
| 12646 | #ifdef MS_WINDOWS |
| 12647 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12648 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12649 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12650 | STRUCT_STAT stat; |
| 12651 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12652 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12653 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12654 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12655 | path = PyUnicode_AsUnicode(unicode); |
| 12656 | result = LSTAT(path, &stat); |
| 12657 | Py_DECREF(unicode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12658 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12659 | if (result != 0) |
| 12660 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12661 | |
| 12662 | self->win32_file_index = stat.st_ino; |
| 12663 | self->got_file_index = 1; |
| 12664 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12665 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 12666 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12667 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 12668 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 12669 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12670 | #endif |
| 12671 | } |
| 12672 | |
| 12673 | static PyObject * |
| 12674 | DirEntry_repr(DirEntry *self) |
| 12675 | { |
| 12676 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 12677 | } |
| 12678 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12679 | /*[clinic input] |
| 12680 | os.DirEntry.__fspath__ |
| 12681 | |
| 12682 | Returns the path for the entry. |
| 12683 | [clinic start generated code]*/ |
| 12684 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12685 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12686 | os_DirEntry___fspath___impl(DirEntry *self) |
| 12687 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12688 | { |
| 12689 | Py_INCREF(self->path); |
| 12690 | return self->path; |
| 12691 | } |
| 12692 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12693 | static PyMemberDef DirEntry_members[] = { |
| 12694 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 12695 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 12696 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 12697 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 12698 | {NULL} |
| 12699 | }; |
| 12700 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12701 | #include "clinic/posixmodule.c.h" |
| 12702 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12703 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12704 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 12705 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 12706 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 12707 | OS_DIRENTRY_STAT_METHODDEF |
| 12708 | OS_DIRENTRY_INODE_METHODDEF |
| 12709 | OS_DIRENTRY___FSPATH___METHODDEF |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12710 | {NULL} |
| 12711 | }; |
| 12712 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12713 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12714 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12715 | MODNAME ".DirEntry", /* tp_name */ |
| 12716 | sizeof(DirEntry), /* tp_basicsize */ |
| 12717 | 0, /* tp_itemsize */ |
| 12718 | /* methods */ |
| 12719 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 12720 | 0, /* tp_vectorcall_offset */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12721 | 0, /* tp_getattr */ |
| 12722 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 12723 | 0, /* tp_as_async */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12724 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 12725 | 0, /* tp_as_number */ |
| 12726 | 0, /* tp_as_sequence */ |
| 12727 | 0, /* tp_as_mapping */ |
| 12728 | 0, /* tp_hash */ |
| 12729 | 0, /* tp_call */ |
| 12730 | 0, /* tp_str */ |
| 12731 | 0, /* tp_getattro */ |
| 12732 | 0, /* tp_setattro */ |
| 12733 | 0, /* tp_as_buffer */ |
| 12734 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 12735 | 0, /* tp_doc */ |
| 12736 | 0, /* tp_traverse */ |
| 12737 | 0, /* tp_clear */ |
| 12738 | 0, /* tp_richcompare */ |
| 12739 | 0, /* tp_weaklistoffset */ |
| 12740 | 0, /* tp_iter */ |
| 12741 | 0, /* tp_iternext */ |
| 12742 | DirEntry_methods, /* tp_methods */ |
| 12743 | DirEntry_members, /* tp_members */ |
| 12744 | }; |
| 12745 | |
| 12746 | #ifdef MS_WINDOWS |
| 12747 | |
| 12748 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12749 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12750 | { |
| 12751 | Py_ssize_t path_len; |
| 12752 | Py_ssize_t size; |
| 12753 | wchar_t *result; |
| 12754 | wchar_t ch; |
| 12755 | |
| 12756 | if (!path_wide) { /* Default arg: "." */ |
| 12757 | path_wide = L"."; |
| 12758 | path_len = 1; |
| 12759 | } |
| 12760 | else { |
| 12761 | path_len = wcslen(path_wide); |
| 12762 | } |
| 12763 | |
| 12764 | /* The +1's are for the path separator and the NUL */ |
| 12765 | size = path_len + 1 + wcslen(filename) + 1; |
| 12766 | result = PyMem_New(wchar_t, size); |
| 12767 | if (!result) { |
| 12768 | PyErr_NoMemory(); |
| 12769 | return NULL; |
| 12770 | } |
| 12771 | wcscpy(result, path_wide); |
| 12772 | if (path_len > 0) { |
| 12773 | ch = result[path_len - 1]; |
| 12774 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 12775 | result[path_len++] = SEP; |
| 12776 | wcscpy(result + path_len, filename); |
| 12777 | } |
| 12778 | return result; |
| 12779 | } |
| 12780 | |
| 12781 | static PyObject * |
| 12782 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 12783 | { |
| 12784 | DirEntry *entry; |
| 12785 | BY_HANDLE_FILE_INFORMATION file_info; |
| 12786 | ULONG reparse_tag; |
| 12787 | wchar_t *joined_path; |
| 12788 | |
| 12789 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 12790 | if (!entry) |
| 12791 | return NULL; |
| 12792 | entry->name = NULL; |
| 12793 | entry->path = NULL; |
| 12794 | entry->stat = NULL; |
| 12795 | entry->lstat = NULL; |
| 12796 | entry->got_file_index = 0; |
| 12797 | |
| 12798 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 12799 | if (!entry->name) |
| 12800 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12801 | if (path->narrow) { |
| 12802 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 12803 | if (!entry->name) |
| 12804 | goto error; |
| 12805 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12806 | |
| 12807 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 12808 | if (!joined_path) |
| 12809 | goto error; |
| 12810 | |
| 12811 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 12812 | PyMem_Free(joined_path); |
| 12813 | if (!entry->path) |
| 12814 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12815 | if (path->narrow) { |
| 12816 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 12817 | if (!entry->path) |
| 12818 | goto error; |
| 12819 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12820 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12821 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12822 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 12823 | |
| 12824 | return (PyObject *)entry; |
| 12825 | |
| 12826 | error: |
| 12827 | Py_DECREF(entry); |
| 12828 | return NULL; |
| 12829 | } |
| 12830 | |
| 12831 | #else /* POSIX */ |
| 12832 | |
| 12833 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12834 | 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] | 12835 | { |
| 12836 | Py_ssize_t path_len; |
| 12837 | Py_ssize_t size; |
| 12838 | char *result; |
| 12839 | |
| 12840 | if (!path_narrow) { /* Default arg: "." */ |
| 12841 | path_narrow = "."; |
| 12842 | path_len = 1; |
| 12843 | } |
| 12844 | else { |
| 12845 | path_len = strlen(path_narrow); |
| 12846 | } |
| 12847 | |
| 12848 | if (filename_len == -1) |
| 12849 | filename_len = strlen(filename); |
| 12850 | |
| 12851 | /* The +1's are for the path separator and the NUL */ |
| 12852 | size = path_len + 1 + filename_len + 1; |
| 12853 | result = PyMem_New(char, size); |
| 12854 | if (!result) { |
| 12855 | PyErr_NoMemory(); |
| 12856 | return NULL; |
| 12857 | } |
| 12858 | strcpy(result, path_narrow); |
| 12859 | if (path_len > 0 && result[path_len - 1] != '/') |
| 12860 | result[path_len++] = '/'; |
| 12861 | strcpy(result + path_len, filename); |
| 12862 | return result; |
| 12863 | } |
| 12864 | |
| 12865 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12866 | 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] | 12867 | ino_t d_ino |
| 12868 | #ifdef HAVE_DIRENT_D_TYPE |
| 12869 | , unsigned char d_type |
| 12870 | #endif |
| 12871 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12872 | { |
| 12873 | DirEntry *entry; |
| 12874 | char *joined_path; |
| 12875 | |
| 12876 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 12877 | if (!entry) |
| 12878 | return NULL; |
| 12879 | entry->name = NULL; |
| 12880 | entry->path = NULL; |
| 12881 | entry->stat = NULL; |
| 12882 | entry->lstat = NULL; |
| 12883 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12884 | if (path->fd != -1) { |
| 12885 | entry->dir_fd = path->fd; |
| 12886 | joined_path = NULL; |
| 12887 | } |
| 12888 | else { |
| 12889 | entry->dir_fd = DEFAULT_DIR_FD; |
| 12890 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 12891 | if (!joined_path) |
| 12892 | goto error; |
| 12893 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12894 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 12895 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12896 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12897 | if (joined_path) |
| 12898 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12899 | } |
| 12900 | else { |
| 12901 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12902 | if (joined_path) |
| 12903 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12904 | } |
| 12905 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12906 | if (!entry->name) |
| 12907 | goto error; |
| 12908 | |
| 12909 | if (path->fd != -1) { |
| 12910 | entry->path = entry->name; |
| 12911 | Py_INCREF(entry->path); |
| 12912 | } |
| 12913 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12914 | goto error; |
| 12915 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12916 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12917 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12918 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12919 | entry->d_ino = d_ino; |
| 12920 | |
| 12921 | return (PyObject *)entry; |
| 12922 | |
| 12923 | error: |
| 12924 | Py_XDECREF(entry); |
| 12925 | return NULL; |
| 12926 | } |
| 12927 | |
| 12928 | #endif |
| 12929 | |
| 12930 | |
| 12931 | typedef struct { |
| 12932 | PyObject_HEAD |
| 12933 | path_t path; |
| 12934 | #ifdef MS_WINDOWS |
| 12935 | HANDLE handle; |
| 12936 | WIN32_FIND_DATAW file_data; |
| 12937 | int first_time; |
| 12938 | #else /* POSIX */ |
| 12939 | DIR *dirp; |
| 12940 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12941 | #ifdef HAVE_FDOPENDIR |
| 12942 | int fd; |
| 12943 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12944 | } ScandirIterator; |
| 12945 | |
| 12946 | #ifdef MS_WINDOWS |
| 12947 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12948 | static int |
| 12949 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 12950 | { |
| 12951 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 12952 | } |
| 12953 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12954 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12955 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12956 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12957 | HANDLE handle = iterator->handle; |
| 12958 | |
| 12959 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12960 | return; |
| 12961 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12962 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12963 | Py_BEGIN_ALLOW_THREADS |
| 12964 | FindClose(handle); |
| 12965 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12966 | } |
| 12967 | |
| 12968 | static PyObject * |
| 12969 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12970 | { |
| 12971 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 12972 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12973 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12974 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12975 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12976 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12977 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12978 | |
| 12979 | while (1) { |
| 12980 | if (!iterator->first_time) { |
| 12981 | Py_BEGIN_ALLOW_THREADS |
| 12982 | success = FindNextFileW(iterator->handle, file_data); |
| 12983 | Py_END_ALLOW_THREADS |
| 12984 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12985 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12986 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12987 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12988 | break; |
| 12989 | } |
| 12990 | } |
| 12991 | iterator->first_time = 0; |
| 12992 | |
| 12993 | /* Skip over . and .. */ |
| 12994 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12995 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 12996 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 12997 | if (!entry) |
| 12998 | break; |
| 12999 | return entry; |
| 13000 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13001 | |
| 13002 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13003 | } |
| 13004 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13005 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13006 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13007 | return NULL; |
| 13008 | } |
| 13009 | |
| 13010 | #else /* POSIX */ |
| 13011 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13012 | static int |
| 13013 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13014 | { |
| 13015 | return !iterator->dirp; |
| 13016 | } |
| 13017 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13018 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13019 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13020 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13021 | DIR *dirp = iterator->dirp; |
| 13022 | |
| 13023 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13024 | return; |
| 13025 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13026 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13027 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13028 | #ifdef HAVE_FDOPENDIR |
| 13029 | if (iterator->path.fd != -1) |
| 13030 | rewinddir(dirp); |
| 13031 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13032 | closedir(dirp); |
| 13033 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13034 | return; |
| 13035 | } |
| 13036 | |
| 13037 | static PyObject * |
| 13038 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13039 | { |
| 13040 | struct dirent *direntp; |
| 13041 | Py_ssize_t name_len; |
| 13042 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13043 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13044 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13045 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13046 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13047 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13048 | |
| 13049 | while (1) { |
| 13050 | errno = 0; |
| 13051 | Py_BEGIN_ALLOW_THREADS |
| 13052 | direntp = readdir(iterator->dirp); |
| 13053 | Py_END_ALLOW_THREADS |
| 13054 | |
| 13055 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13056 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13057 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13058 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13059 | break; |
| 13060 | } |
| 13061 | |
| 13062 | /* Skip over . and .. */ |
| 13063 | name_len = NAMLEN(direntp); |
| 13064 | is_dot = direntp->d_name[0] == '.' && |
| 13065 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 13066 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13067 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13068 | name_len, direntp->d_ino |
| 13069 | #ifdef HAVE_DIRENT_D_TYPE |
| 13070 | , direntp->d_type |
| 13071 | #endif |
| 13072 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13073 | if (!entry) |
| 13074 | break; |
| 13075 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13076 | } |
| 13077 | |
| 13078 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13079 | } |
| 13080 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13081 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13082 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13083 | return NULL; |
| 13084 | } |
| 13085 | |
| 13086 | #endif |
| 13087 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13088 | static PyObject * |
| 13089 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 13090 | { |
| 13091 | ScandirIterator_closedir(self); |
| 13092 | Py_RETURN_NONE; |
| 13093 | } |
| 13094 | |
| 13095 | static PyObject * |
| 13096 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 13097 | { |
| 13098 | Py_INCREF(self); |
| 13099 | return self; |
| 13100 | } |
| 13101 | |
| 13102 | static PyObject * |
| 13103 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 13104 | { |
| 13105 | ScandirIterator_closedir(self); |
| 13106 | Py_RETURN_NONE; |
| 13107 | } |
| 13108 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13109 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13110 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 13111 | { |
| 13112 | PyObject *error_type, *error_value, *error_traceback; |
| 13113 | |
| 13114 | /* Save the current exception, if any. */ |
| 13115 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 13116 | |
| 13117 | if (!ScandirIterator_is_closed(iterator)) { |
| 13118 | ScandirIterator_closedir(iterator); |
| 13119 | |
| 13120 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 13121 | "unclosed scandir iterator %R", iterator)) { |
| 13122 | /* Spurious errors can appear at shutdown */ |
| 13123 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 13124 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 13125 | } |
| 13126 | } |
| 13127 | } |
| 13128 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13129 | path_cleanup(&iterator->path); |
| 13130 | |
| 13131 | /* Restore the saved exception. */ |
| 13132 | PyErr_Restore(error_type, error_value, error_traceback); |
| 13133 | } |
| 13134 | |
| 13135 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13136 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 13137 | { |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13138 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 13139 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13140 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13141 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 13142 | } |
| 13143 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13144 | static PyMethodDef ScandirIterator_methods[] = { |
| 13145 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 13146 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 13147 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 13148 | {NULL} |
| 13149 | }; |
| 13150 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 13151 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13152 | PyVarObject_HEAD_INIT(NULL, 0) |
| 13153 | MODNAME ".ScandirIterator", /* tp_name */ |
| 13154 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 13155 | 0, /* tp_itemsize */ |
| 13156 | /* methods */ |
| 13157 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 13158 | 0, /* tp_vectorcall_offset */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13159 | 0, /* tp_getattr */ |
| 13160 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 13161 | 0, /* tp_as_async */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13162 | 0, /* tp_repr */ |
| 13163 | 0, /* tp_as_number */ |
| 13164 | 0, /* tp_as_sequence */ |
| 13165 | 0, /* tp_as_mapping */ |
| 13166 | 0, /* tp_hash */ |
| 13167 | 0, /* tp_call */ |
| 13168 | 0, /* tp_str */ |
| 13169 | 0, /* tp_getattro */ |
| 13170 | 0, /* tp_setattro */ |
| 13171 | 0, /* tp_as_buffer */ |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 13172 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13173 | 0, /* tp_doc */ |
| 13174 | 0, /* tp_traverse */ |
| 13175 | 0, /* tp_clear */ |
| 13176 | 0, /* tp_richcompare */ |
| 13177 | 0, /* tp_weaklistoffset */ |
| 13178 | PyObject_SelfIter, /* tp_iter */ |
| 13179 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13180 | ScandirIterator_methods, /* tp_methods */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13181 | 0, /* tp_members */ |
| 13182 | 0, /* tp_getset */ |
| 13183 | 0, /* tp_base */ |
| 13184 | 0, /* tp_dict */ |
| 13185 | 0, /* tp_descr_get */ |
| 13186 | 0, /* tp_descr_set */ |
| 13187 | 0, /* tp_dictoffset */ |
| 13188 | 0, /* tp_init */ |
| 13189 | 0, /* tp_alloc */ |
| 13190 | 0, /* tp_new */ |
| 13191 | 0, /* tp_free */ |
| 13192 | 0, /* tp_is_gc */ |
| 13193 | 0, /* tp_bases */ |
| 13194 | 0, /* tp_mro */ |
| 13195 | 0, /* tp_cache */ |
| 13196 | 0, /* tp_subclasses */ |
| 13197 | 0, /* tp_weaklist */ |
| 13198 | 0, /* tp_del */ |
| 13199 | 0, /* tp_version_tag */ |
| 13200 | (destructor)ScandirIterator_finalize, /* tp_finalize */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13201 | }; |
| 13202 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13203 | /*[clinic input] |
| 13204 | os.scandir |
| 13205 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13206 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13207 | |
| 13208 | Return an iterator of DirEntry objects for given path. |
| 13209 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13210 | 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] | 13211 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 13212 | all other circumstances they will be str. |
| 13213 | |
| 13214 | If path is None, uses the path='.'. |
| 13215 | [clinic start generated code]*/ |
| 13216 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13217 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13218 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13219 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13220 | { |
| 13221 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13222 | #ifdef MS_WINDOWS |
| 13223 | wchar_t *path_strW; |
| 13224 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13225 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13226 | #ifdef HAVE_FDOPENDIR |
| 13227 | int fd = -1; |
| 13228 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13229 | #endif |
| 13230 | |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 13231 | if (PySys_Audit("os.scandir", "O", |
| 13232 | path->object ? path->object : Py_None) < 0) { |
| 13233 | return NULL; |
| 13234 | } |
| 13235 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13236 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 13237 | if (!iterator) |
| 13238 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13239 | |
| 13240 | #ifdef MS_WINDOWS |
| 13241 | iterator->handle = INVALID_HANDLE_VALUE; |
| 13242 | #else |
| 13243 | iterator->dirp = NULL; |
| 13244 | #endif |
| 13245 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13246 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 13247 | /* Move the ownership to iterator->path */ |
| 13248 | path->object = NULL; |
| 13249 | path->cleanup = NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13250 | |
| 13251 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13252 | iterator->first_time = 1; |
| 13253 | |
| 13254 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 13255 | if (!path_strW) |
| 13256 | goto error; |
| 13257 | |
| 13258 | Py_BEGIN_ALLOW_THREADS |
| 13259 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 13260 | Py_END_ALLOW_THREADS |
| 13261 | |
| 13262 | PyMem_Free(path_strW); |
| 13263 | |
| 13264 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 13265 | path_error(&iterator->path); |
| 13266 | goto error; |
| 13267 | } |
| 13268 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13269 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13270 | #ifdef HAVE_FDOPENDIR |
| 13271 | if (path->fd != -1) { |
| 13272 | /* closedir() closes the FD, so we duplicate it */ |
| 13273 | fd = _Py_dup(path->fd); |
| 13274 | if (fd == -1) |
| 13275 | goto error; |
| 13276 | |
| 13277 | Py_BEGIN_ALLOW_THREADS |
| 13278 | iterator->dirp = fdopendir(fd); |
| 13279 | Py_END_ALLOW_THREADS |
| 13280 | } |
| 13281 | else |
| 13282 | #endif |
| 13283 | { |
| 13284 | if (iterator->path.narrow) |
| 13285 | path_str = iterator->path.narrow; |
| 13286 | else |
| 13287 | path_str = "."; |
| 13288 | |
| 13289 | Py_BEGIN_ALLOW_THREADS |
| 13290 | iterator->dirp = opendir(path_str); |
| 13291 | Py_END_ALLOW_THREADS |
| 13292 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13293 | |
| 13294 | if (!iterator->dirp) { |
| 13295 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13296 | #ifdef HAVE_FDOPENDIR |
| 13297 | if (fd != -1) { |
| 13298 | Py_BEGIN_ALLOW_THREADS |
| 13299 | close(fd); |
| 13300 | Py_END_ALLOW_THREADS |
| 13301 | } |
| 13302 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13303 | goto error; |
| 13304 | } |
| 13305 | #endif |
| 13306 | |
| 13307 | return (PyObject *)iterator; |
| 13308 | |
| 13309 | error: |
| 13310 | Py_DECREF(iterator); |
| 13311 | return NULL; |
| 13312 | } |
| 13313 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13314 | /* |
| 13315 | Return the file system path representation of the object. |
| 13316 | |
| 13317 | If the object is str or bytes, then allow it to pass through with |
| 13318 | an incremented refcount. If the object defines __fspath__(), then |
| 13319 | return the result of that method. All other types raise a TypeError. |
| 13320 | */ |
| 13321 | PyObject * |
| 13322 | PyOS_FSPath(PyObject *path) |
| 13323 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 13324 | /* For error message reasons, this function is manually inlined in |
| 13325 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13326 | _Py_IDENTIFIER(__fspath__); |
| 13327 | PyObject *func = NULL; |
| 13328 | PyObject *path_repr = NULL; |
| 13329 | |
| 13330 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 13331 | Py_INCREF(path); |
| 13332 | return path; |
| 13333 | } |
| 13334 | |
| 13335 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 13336 | if (NULL == func) { |
| 13337 | return PyErr_Format(PyExc_TypeError, |
| 13338 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13339 | "not %.200s", |
| 13340 | Py_TYPE(path)->tp_name); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13341 | } |
| 13342 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 13343 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13344 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 13345 | if (NULL == path_repr) { |
| 13346 | return NULL; |
| 13347 | } |
| 13348 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13349 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 13350 | PyErr_Format(PyExc_TypeError, |
| 13351 | "expected %.200s.__fspath__() to return str or bytes, " |
| 13352 | "not %.200s", Py_TYPE(path)->tp_name, |
| 13353 | Py_TYPE(path_repr)->tp_name); |
| 13354 | Py_DECREF(path_repr); |
| 13355 | return NULL; |
| 13356 | } |
| 13357 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13358 | return path_repr; |
| 13359 | } |
| 13360 | |
| 13361 | /*[clinic input] |
| 13362 | os.fspath |
| 13363 | |
| 13364 | path: object |
| 13365 | |
| 13366 | Return the file system path representation of the object. |
| 13367 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 13368 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 13369 | object defines __fspath__(), then return the result of that method. All other |
| 13370 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13371 | [clinic start generated code]*/ |
| 13372 | |
| 13373 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 13374 | os_fspath_impl(PyObject *module, PyObject *path) |
| 13375 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13376 | { |
| 13377 | return PyOS_FSPath(path); |
| 13378 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13379 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13380 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13381 | /*[clinic input] |
| 13382 | os.getrandom |
| 13383 | |
| 13384 | size: Py_ssize_t |
| 13385 | flags: int=0 |
| 13386 | |
| 13387 | Obtain a series of random bytes. |
| 13388 | [clinic start generated code]*/ |
| 13389 | |
| 13390 | static PyObject * |
| 13391 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 13392 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 13393 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13394 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13395 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13396 | |
| 13397 | if (size < 0) { |
| 13398 | errno = EINVAL; |
| 13399 | return posix_error(); |
| 13400 | } |
| 13401 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13402 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 13403 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13404 | PyErr_NoMemory(); |
| 13405 | return NULL; |
| 13406 | } |
| 13407 | |
| 13408 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13409 | n = syscall(SYS_getrandom, |
| 13410 | PyBytes_AS_STRING(bytes), |
| 13411 | PyBytes_GET_SIZE(bytes), |
| 13412 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13413 | if (n < 0 && errno == EINTR) { |
| 13414 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13415 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13416 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13417 | |
| 13418 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13419 | continue; |
| 13420 | } |
| 13421 | break; |
| 13422 | } |
| 13423 | |
| 13424 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13425 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13426 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13427 | } |
| 13428 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13429 | if (n != size) { |
| 13430 | _PyBytes_Resize(&bytes, n); |
| 13431 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13432 | |
| 13433 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13434 | |
| 13435 | error: |
| 13436 | Py_DECREF(bytes); |
| 13437 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13438 | } |
| 13439 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 13440 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13441 | #ifdef MS_WINDOWS |
| 13442 | /* bpo-36085: Helper functions for managing DLL search directories |
| 13443 | * on win32 |
| 13444 | */ |
| 13445 | |
| 13446 | typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory); |
| 13447 | typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie); |
| 13448 | |
| 13449 | /*[clinic input] |
| 13450 | os._add_dll_directory |
| 13451 | |
| 13452 | path: path_t |
| 13453 | |
| 13454 | Add a path to the DLL search path. |
| 13455 | |
| 13456 | This search path is used when resolving dependencies for imported |
| 13457 | extension modules (the module itself is resolved through sys.path), |
| 13458 | and also by ctypes. |
| 13459 | |
| 13460 | Returns an opaque value that may be passed to os.remove_dll_directory |
| 13461 | to remove this directory from the search path. |
| 13462 | [clinic start generated code]*/ |
| 13463 | |
| 13464 | static PyObject * |
| 13465 | os__add_dll_directory_impl(PyObject *module, path_t *path) |
| 13466 | /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ |
| 13467 | { |
| 13468 | HMODULE hKernel32; |
| 13469 | PAddDllDirectory AddDllDirectory; |
| 13470 | DLL_DIRECTORY_COOKIE cookie = 0; |
| 13471 | DWORD err = 0; |
| 13472 | |
| 13473 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13474 | infrequent operation, just do it each time. Kernel32 is always |
| 13475 | loaded. */ |
| 13476 | Py_BEGIN_ALLOW_THREADS |
| 13477 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13478 | !(AddDllDirectory = (PAddDllDirectory)GetProcAddress( |
| 13479 | hKernel32, "AddDllDirectory")) || |
| 13480 | !(cookie = (*AddDllDirectory)(path->wide))) { |
| 13481 | err = GetLastError(); |
| 13482 | } |
| 13483 | Py_END_ALLOW_THREADS |
| 13484 | |
| 13485 | if (err) { |
| 13486 | return win32_error_object_err("add_dll_directory", |
| 13487 | path->object, err); |
| 13488 | } |
| 13489 | |
| 13490 | return PyCapsule_New(cookie, "DLL directory cookie", NULL); |
| 13491 | } |
| 13492 | |
| 13493 | /*[clinic input] |
| 13494 | os._remove_dll_directory |
| 13495 | |
| 13496 | cookie: object |
| 13497 | |
| 13498 | Removes a path from the DLL search path. |
| 13499 | |
| 13500 | The parameter is an opaque value that was returned from |
| 13501 | os.add_dll_directory. You can only remove directories that you added |
| 13502 | yourself. |
| 13503 | [clinic start generated code]*/ |
| 13504 | |
| 13505 | static PyObject * |
| 13506 | os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) |
| 13507 | /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ |
| 13508 | { |
| 13509 | HMODULE hKernel32; |
| 13510 | PRemoveDllDirectory RemoveDllDirectory; |
| 13511 | DLL_DIRECTORY_COOKIE cookieValue; |
| 13512 | DWORD err = 0; |
| 13513 | |
| 13514 | if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) { |
| 13515 | PyErr_SetString(PyExc_TypeError, |
| 13516 | "Provided cookie was not returned from os.add_dll_directory"); |
| 13517 | return NULL; |
| 13518 | } |
| 13519 | |
| 13520 | cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( |
| 13521 | cookie, "DLL directory cookie"); |
| 13522 | |
| 13523 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13524 | infrequent operation, just do it each time. Kernel32 is always |
| 13525 | loaded. */ |
| 13526 | Py_BEGIN_ALLOW_THREADS |
| 13527 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13528 | !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress( |
| 13529 | hKernel32, "RemoveDllDirectory")) || |
| 13530 | !(*RemoveDllDirectory)(cookieValue)) { |
| 13531 | err = GetLastError(); |
| 13532 | } |
| 13533 | Py_END_ALLOW_THREADS |
| 13534 | |
| 13535 | if (err) { |
| 13536 | return win32_error_object_err("remove_dll_directory", |
| 13537 | NULL, err); |
| 13538 | } |
| 13539 | |
| 13540 | if (PyCapsule_SetName(cookie, NULL)) { |
| 13541 | return NULL; |
| 13542 | } |
| 13543 | |
| 13544 | Py_RETURN_NONE; |
| 13545 | } |
| 13546 | |
| 13547 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13548 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13549 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13550 | |
| 13551 | OS_STAT_METHODDEF |
| 13552 | OS_ACCESS_METHODDEF |
| 13553 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13554 | OS_CHDIR_METHODDEF |
| 13555 | OS_CHFLAGS_METHODDEF |
| 13556 | OS_CHMOD_METHODDEF |
| 13557 | OS_FCHMOD_METHODDEF |
| 13558 | OS_LCHMOD_METHODDEF |
| 13559 | OS_CHOWN_METHODDEF |
| 13560 | OS_FCHOWN_METHODDEF |
| 13561 | OS_LCHOWN_METHODDEF |
| 13562 | OS_LCHFLAGS_METHODDEF |
| 13563 | OS_CHROOT_METHODDEF |
| 13564 | OS_CTERMID_METHODDEF |
| 13565 | OS_GETCWD_METHODDEF |
| 13566 | OS_GETCWDB_METHODDEF |
| 13567 | OS_LINK_METHODDEF |
| 13568 | OS_LISTDIR_METHODDEF |
| 13569 | OS_LSTAT_METHODDEF |
| 13570 | OS_MKDIR_METHODDEF |
| 13571 | OS_NICE_METHODDEF |
| 13572 | OS_GETPRIORITY_METHODDEF |
| 13573 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13574 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 13575 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13576 | OS_READLINK_METHODDEF |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 13577 | OS_COPY_FILE_RANGE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13578 | OS_RENAME_METHODDEF |
| 13579 | OS_REPLACE_METHODDEF |
| 13580 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13581 | OS_SYMLINK_METHODDEF |
| 13582 | OS_SYSTEM_METHODDEF |
| 13583 | OS_UMASK_METHODDEF |
| 13584 | OS_UNAME_METHODDEF |
| 13585 | OS_UNLINK_METHODDEF |
| 13586 | OS_REMOVE_METHODDEF |
| 13587 | OS_UTIME_METHODDEF |
| 13588 | OS_TIMES_METHODDEF |
| 13589 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 13590 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13591 | OS_EXECV_METHODDEF |
| 13592 | OS_EXECVE_METHODDEF |
| 13593 | OS_SPAWNV_METHODDEF |
| 13594 | OS_SPAWNVE_METHODDEF |
| 13595 | OS_FORK1_METHODDEF |
| 13596 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 13597 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13598 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 13599 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 13600 | OS_SCHED_GETPARAM_METHODDEF |
| 13601 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 13602 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 13603 | OS_SCHED_SETPARAM_METHODDEF |
| 13604 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 13605 | OS_SCHED_YIELD_METHODDEF |
| 13606 | OS_SCHED_SETAFFINITY_METHODDEF |
| 13607 | OS_SCHED_GETAFFINITY_METHODDEF |
| 13608 | OS_OPENPTY_METHODDEF |
| 13609 | OS_FORKPTY_METHODDEF |
| 13610 | OS_GETEGID_METHODDEF |
| 13611 | OS_GETEUID_METHODDEF |
| 13612 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 13613 | #ifdef HAVE_GETGROUPLIST |
| 13614 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 13615 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13616 | OS_GETGROUPS_METHODDEF |
| 13617 | OS_GETPID_METHODDEF |
| 13618 | OS_GETPGRP_METHODDEF |
| 13619 | OS_GETPPID_METHODDEF |
| 13620 | OS_GETUID_METHODDEF |
| 13621 | OS_GETLOGIN_METHODDEF |
| 13622 | OS_KILL_METHODDEF |
| 13623 | OS_KILLPG_METHODDEF |
| 13624 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13625 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13626 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13627 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13628 | OS_SETUID_METHODDEF |
| 13629 | OS_SETEUID_METHODDEF |
| 13630 | OS_SETREUID_METHODDEF |
| 13631 | OS_SETGID_METHODDEF |
| 13632 | OS_SETEGID_METHODDEF |
| 13633 | OS_SETREGID_METHODDEF |
| 13634 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13635 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13636 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13637 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13638 | OS_GETPGID_METHODDEF |
| 13639 | OS_SETPGRP_METHODDEF |
| 13640 | OS_WAIT_METHODDEF |
| 13641 | OS_WAIT3_METHODDEF |
| 13642 | OS_WAIT4_METHODDEF |
| 13643 | OS_WAITID_METHODDEF |
| 13644 | OS_WAITPID_METHODDEF |
| 13645 | OS_GETSID_METHODDEF |
| 13646 | OS_SETSID_METHODDEF |
| 13647 | OS_SETPGID_METHODDEF |
| 13648 | OS_TCGETPGRP_METHODDEF |
| 13649 | OS_TCSETPGRP_METHODDEF |
| 13650 | OS_OPEN_METHODDEF |
| 13651 | OS_CLOSE_METHODDEF |
| 13652 | OS_CLOSERANGE_METHODDEF |
| 13653 | OS_DEVICE_ENCODING_METHODDEF |
| 13654 | OS_DUP_METHODDEF |
| 13655 | OS_DUP2_METHODDEF |
| 13656 | OS_LOCKF_METHODDEF |
| 13657 | OS_LSEEK_METHODDEF |
| 13658 | OS_READ_METHODDEF |
| 13659 | OS_READV_METHODDEF |
| 13660 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13661 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13662 | OS_WRITE_METHODDEF |
| 13663 | OS_WRITEV_METHODDEF |
| 13664 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13665 | OS_PWRITEV_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13666 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 13667 | {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13668 | posix_sendfile__doc__}, |
| 13669 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13670 | OS_FSTAT_METHODDEF |
| 13671 | OS_ISATTY_METHODDEF |
| 13672 | OS_PIPE_METHODDEF |
| 13673 | OS_PIPE2_METHODDEF |
| 13674 | OS_MKFIFO_METHODDEF |
| 13675 | OS_MKNOD_METHODDEF |
| 13676 | OS_MAJOR_METHODDEF |
| 13677 | OS_MINOR_METHODDEF |
| 13678 | OS_MAKEDEV_METHODDEF |
| 13679 | OS_FTRUNCATE_METHODDEF |
| 13680 | OS_TRUNCATE_METHODDEF |
| 13681 | OS_POSIX_FALLOCATE_METHODDEF |
| 13682 | OS_POSIX_FADVISE_METHODDEF |
| 13683 | OS_PUTENV_METHODDEF |
| 13684 | OS_UNSETENV_METHODDEF |
| 13685 | OS_STRERROR_METHODDEF |
| 13686 | OS_FCHDIR_METHODDEF |
| 13687 | OS_FSYNC_METHODDEF |
| 13688 | OS_SYNC_METHODDEF |
| 13689 | OS_FDATASYNC_METHODDEF |
| 13690 | OS_WCOREDUMP_METHODDEF |
| 13691 | OS_WIFCONTINUED_METHODDEF |
| 13692 | OS_WIFSTOPPED_METHODDEF |
| 13693 | OS_WIFSIGNALED_METHODDEF |
| 13694 | OS_WIFEXITED_METHODDEF |
| 13695 | OS_WEXITSTATUS_METHODDEF |
| 13696 | OS_WTERMSIG_METHODDEF |
| 13697 | OS_WSTOPSIG_METHODDEF |
| 13698 | OS_FSTATVFS_METHODDEF |
| 13699 | OS_STATVFS_METHODDEF |
| 13700 | OS_CONFSTR_METHODDEF |
| 13701 | OS_SYSCONF_METHODDEF |
| 13702 | OS_FPATHCONF_METHODDEF |
| 13703 | OS_PATHCONF_METHODDEF |
| 13704 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 13705 | OS__GETFULLPATHNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13706 | OS__GETDISKUSAGE_METHODDEF |
| 13707 | OS__GETFINALPATHNAME_METHODDEF |
| 13708 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 13709 | OS_GETLOADAVG_METHODDEF |
| 13710 | OS_URANDOM_METHODDEF |
| 13711 | OS_SETRESUID_METHODDEF |
| 13712 | OS_SETRESGID_METHODDEF |
| 13713 | OS_GETRESUID_METHODDEF |
| 13714 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 13715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13716 | OS_GETXATTR_METHODDEF |
| 13717 | OS_SETXATTR_METHODDEF |
| 13718 | OS_REMOVEXATTR_METHODDEF |
| 13719 | OS_LISTXATTR_METHODDEF |
| 13720 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13721 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 13722 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 13723 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13724 | OS_CPU_COUNT_METHODDEF |
| 13725 | OS_GET_INHERITABLE_METHODDEF |
| 13726 | OS_SET_INHERITABLE_METHODDEF |
| 13727 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 13728 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13729 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13730 | OS_GET_BLOCKING_METHODDEF |
| 13731 | OS_SET_BLOCKING_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13732 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13733 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13734 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13735 | OS_GETRANDOM_METHODDEF |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 13736 | OS_MEMFD_CREATE_METHODDEF |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13737 | #ifdef MS_WINDOWS |
| 13738 | OS__ADD_DLL_DIRECTORY_METHODDEF |
| 13739 | OS__REMOVE_DLL_DIRECTORY_METHODDEF |
| 13740 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13741 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13742 | }; |
| 13743 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13744 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13745 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13746 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13747 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13748 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13749 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13750 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13751 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13752 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13753 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13754 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13755 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13756 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13757 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13758 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13759 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13760 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13761 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13762 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13763 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13764 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13765 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13766 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13767 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13768 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13769 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13770 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13771 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13772 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13773 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13774 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13775 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13776 | #endif |
| 13777 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13778 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13779 | #endif |
| 13780 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13781 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13782 | #endif |
| 13783 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13784 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13785 | #endif |
| 13786 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13787 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13788 | #endif |
| 13789 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13790 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13791 | #endif |
| 13792 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13793 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13794 | #endif |
| 13795 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13796 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13797 | #endif |
| 13798 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13799 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13800 | #endif |
| 13801 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13802 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13803 | #endif |
| 13804 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13805 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13806 | #endif |
| 13807 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13808 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13809 | #endif |
| 13810 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13811 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13812 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13813 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13814 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13815 | #endif |
| 13816 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13817 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13818 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13819 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13820 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13821 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13822 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13823 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13824 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13825 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13826 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13827 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13828 | #endif |
| 13829 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13830 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13831 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13832 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13833 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13834 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13835 | #endif |
| 13836 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13837 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13838 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13839 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13840 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13841 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13842 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13843 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13844 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 13845 | #ifdef O_TMPFILE |
| 13846 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 13847 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13848 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13849 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13850 | #endif |
| 13851 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13852 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13853 | #endif |
| 13854 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13855 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13856 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13857 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13858 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13859 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13860 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13861 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13862 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13863 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13864 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13865 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13866 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13867 | #endif |
| 13868 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13869 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13870 | #endif |
| 13871 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13872 | /* MS Windows */ |
| 13873 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13874 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13875 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13876 | #endif |
| 13877 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13878 | /* Optimize for short life (keep in memory). */ |
| 13879 | /* 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] | 13880 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13881 | #endif |
| 13882 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13883 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13884 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13885 | #endif |
| 13886 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13887 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13888 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13889 | #endif |
| 13890 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13891 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13892 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13893 | #endif |
| 13894 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13895 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13896 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13897 | /* Send a SIGIO signal whenever input or output |
| 13898 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13899 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13900 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13901 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13902 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13903 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13904 | #endif |
| 13905 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13906 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13907 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13908 | #endif |
| 13909 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13910 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13911 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13912 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13913 | #ifdef O_NOLINKS |
| 13914 | /* 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] | 13915 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13916 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13917 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13918 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13919 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13920 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 13921 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13922 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13923 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13924 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13925 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13926 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13927 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13928 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13929 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13930 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13931 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13932 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13933 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13934 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13935 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13936 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13937 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13938 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13939 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13940 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13941 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13942 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13943 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13944 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13945 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13946 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13947 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13948 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13949 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13950 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13951 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13952 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13953 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13954 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13955 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13956 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13957 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13958 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13959 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13960 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13961 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13962 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13963 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13964 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13965 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13966 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13967 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13968 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13969 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13970 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13971 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13972 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13973 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13974 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 13975 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13976 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13977 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13978 | #endif /* ST_RDONLY */ |
| 13979 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13980 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13981 | #endif /* ST_NOSUID */ |
| 13982 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 13983 | /* GNU extensions */ |
| 13984 | #ifdef ST_NODEV |
| 13985 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 13986 | #endif /* ST_NODEV */ |
| 13987 | #ifdef ST_NOEXEC |
| 13988 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 13989 | #endif /* ST_NOEXEC */ |
| 13990 | #ifdef ST_SYNCHRONOUS |
| 13991 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 13992 | #endif /* ST_SYNCHRONOUS */ |
| 13993 | #ifdef ST_MANDLOCK |
| 13994 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 13995 | #endif /* ST_MANDLOCK */ |
| 13996 | #ifdef ST_WRITE |
| 13997 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 13998 | #endif /* ST_WRITE */ |
| 13999 | #ifdef ST_APPEND |
| 14000 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 14001 | #endif /* ST_APPEND */ |
| 14002 | #ifdef ST_NOATIME |
| 14003 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 14004 | #endif /* ST_NOATIME */ |
| 14005 | #ifdef ST_NODIRATIME |
| 14006 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 14007 | #endif /* ST_NODIRATIME */ |
| 14008 | #ifdef ST_RELATIME |
| 14009 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 14010 | #endif /* ST_RELATIME */ |
| 14011 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14012 | /* FreeBSD sendfile() constants */ |
| 14013 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14014 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14015 | #endif |
| 14016 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14017 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14018 | #endif |
| 14019 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14020 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14021 | #endif |
| 14022 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14023 | /* constants for posix_fadvise */ |
| 14024 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14025 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14026 | #endif |
| 14027 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14028 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14029 | #endif |
| 14030 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14031 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14032 | #endif |
| 14033 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14034 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14035 | #endif |
| 14036 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14037 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14038 | #endif |
| 14039 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14040 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14041 | #endif |
| 14042 | |
| 14043 | /* constants for waitid */ |
| 14044 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14045 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 14046 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 14047 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14048 | #endif |
| 14049 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14050 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14051 | #endif |
| 14052 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14053 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14054 | #endif |
| 14055 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14056 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14057 | #endif |
| 14058 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14059 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14060 | #endif |
| 14061 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14062 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14063 | #endif |
| 14064 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14065 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14066 | #endif |
| 14067 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14068 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14069 | #endif |
| 14070 | |
| 14071 | /* constants for lockf */ |
| 14072 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14073 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14074 | #endif |
| 14075 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14076 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14077 | #endif |
| 14078 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14079 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14080 | #endif |
| 14081 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14082 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14083 | #endif |
| 14084 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14085 | #ifdef RWF_DSYNC |
| 14086 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 14087 | #endif |
| 14088 | #ifdef RWF_HIPRI |
| 14089 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 14090 | #endif |
| 14091 | #ifdef RWF_SYNC |
| 14092 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 14093 | #endif |
| 14094 | #ifdef RWF_NOWAIT |
| 14095 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 14096 | #endif |
| 14097 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14098 | /* constants for posix_spawn */ |
| 14099 | #ifdef HAVE_POSIX_SPAWN |
| 14100 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 14101 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 14102 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 14103 | #endif |
| 14104 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14105 | #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14106 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 14107 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14108 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14109 | #endif |
| 14110 | #ifdef HAVE_SPAWNV |
| 14111 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14112 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 14113 | #endif |
| 14114 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14115 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14116 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14117 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14118 | #endif |
| 14119 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14120 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14121 | #endif |
| 14122 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14123 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14124 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14125 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 14126 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14127 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14128 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14129 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14130 | #endif |
| 14131 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14132 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14133 | #endif |
| 14134 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14135 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14136 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14137 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14138 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14139 | #endif |
| 14140 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14141 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14142 | #endif |
| 14143 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14144 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14145 | #endif |
| 14146 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14147 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14148 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14149 | #endif |
| 14150 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 14151 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14152 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 14153 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 14154 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 14155 | #endif |
| 14156 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14157 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14158 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14159 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14160 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14161 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14162 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14163 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14164 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14165 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14166 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14167 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14168 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14169 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14170 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14171 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14172 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14173 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14174 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14175 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14176 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14177 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 14178 | #if HAVE_DECL_RTLD_MEMBER |
| 14179 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 14180 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14181 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14182 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 14183 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 14184 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 14185 | #endif |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14186 | #ifdef HAVE_MEMFD_CREATE |
| 14187 | if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1; |
| 14188 | if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1; |
| 14189 | #ifdef MFD_HUGETLB |
| 14190 | if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14191 | #endif |
| 14192 | #ifdef MFD_HUGE_SHIFT |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14193 | if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14194 | #endif |
| 14195 | #ifdef MFD_HUGE_MASK |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14196 | if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14197 | #endif |
| 14198 | #ifdef MFD_HUGE_64KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14199 | if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14200 | #endif |
| 14201 | #ifdef MFD_HUGE_512KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14202 | if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14203 | #endif |
| 14204 | #ifdef MFD_HUGE_1MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14205 | if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14206 | #endif |
| 14207 | #ifdef MFD_HUGE_2MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14208 | if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14209 | #endif |
| 14210 | #ifdef MFD_HUGE_8MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14211 | if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14212 | #endif |
| 14213 | #ifdef MFD_HUGE_16MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14214 | if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14215 | #endif |
| 14216 | #ifdef MFD_HUGE_32MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14217 | if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14218 | #endif |
| 14219 | #ifdef MFD_HUGE_256MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14220 | if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14221 | #endif |
| 14222 | #ifdef MFD_HUGE_512MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14223 | if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14224 | #endif |
| 14225 | #ifdef MFD_HUGE_1GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14226 | if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14227 | #endif |
| 14228 | #ifdef MFD_HUGE_2GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14229 | if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14230 | #endif |
| 14231 | #ifdef MFD_HUGE_16GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14232 | if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1; |
| 14233 | #endif |
| 14234 | #endif |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14235 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14236 | #if defined(__APPLE__) |
| 14237 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 14238 | #endif |
| 14239 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14240 | #ifdef MS_WINDOWS |
| 14241 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1; |
| 14242 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1; |
| 14243 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1; |
| 14244 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1; |
| 14245 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1; |
| 14246 | #endif |
| 14247 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14248 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14249 | } |
| 14250 | |
| 14251 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14252 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14253 | PyModuleDef_HEAD_INIT, |
| 14254 | MODNAME, |
| 14255 | posix__doc__, |
| 14256 | -1, |
| 14257 | posix_methods, |
| 14258 | NULL, |
| 14259 | NULL, |
| 14260 | NULL, |
| 14261 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14262 | }; |
| 14263 | |
| 14264 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14265 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14266 | |
| 14267 | #ifdef HAVE_FACCESSAT |
| 14268 | "HAVE_FACCESSAT", |
| 14269 | #endif |
| 14270 | |
| 14271 | #ifdef HAVE_FCHDIR |
| 14272 | "HAVE_FCHDIR", |
| 14273 | #endif |
| 14274 | |
| 14275 | #ifdef HAVE_FCHMOD |
| 14276 | "HAVE_FCHMOD", |
| 14277 | #endif |
| 14278 | |
| 14279 | #ifdef HAVE_FCHMODAT |
| 14280 | "HAVE_FCHMODAT", |
| 14281 | #endif |
| 14282 | |
| 14283 | #ifdef HAVE_FCHOWN |
| 14284 | "HAVE_FCHOWN", |
| 14285 | #endif |
| 14286 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 14287 | #ifdef HAVE_FCHOWNAT |
| 14288 | "HAVE_FCHOWNAT", |
| 14289 | #endif |
| 14290 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14291 | #ifdef HAVE_FEXECVE |
| 14292 | "HAVE_FEXECVE", |
| 14293 | #endif |
| 14294 | |
| 14295 | #ifdef HAVE_FDOPENDIR |
| 14296 | "HAVE_FDOPENDIR", |
| 14297 | #endif |
| 14298 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14299 | #ifdef HAVE_FPATHCONF |
| 14300 | "HAVE_FPATHCONF", |
| 14301 | #endif |
| 14302 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14303 | #ifdef HAVE_FSTATAT |
| 14304 | "HAVE_FSTATAT", |
| 14305 | #endif |
| 14306 | |
| 14307 | #ifdef HAVE_FSTATVFS |
| 14308 | "HAVE_FSTATVFS", |
| 14309 | #endif |
| 14310 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 14311 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14312 | "HAVE_FTRUNCATE", |
| 14313 | #endif |
| 14314 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14315 | #ifdef HAVE_FUTIMENS |
| 14316 | "HAVE_FUTIMENS", |
| 14317 | #endif |
| 14318 | |
| 14319 | #ifdef HAVE_FUTIMES |
| 14320 | "HAVE_FUTIMES", |
| 14321 | #endif |
| 14322 | |
| 14323 | #ifdef HAVE_FUTIMESAT |
| 14324 | "HAVE_FUTIMESAT", |
| 14325 | #endif |
| 14326 | |
| 14327 | #ifdef HAVE_LINKAT |
| 14328 | "HAVE_LINKAT", |
| 14329 | #endif |
| 14330 | |
| 14331 | #ifdef HAVE_LCHFLAGS |
| 14332 | "HAVE_LCHFLAGS", |
| 14333 | #endif |
| 14334 | |
| 14335 | #ifdef HAVE_LCHMOD |
| 14336 | "HAVE_LCHMOD", |
| 14337 | #endif |
| 14338 | |
| 14339 | #ifdef HAVE_LCHOWN |
| 14340 | "HAVE_LCHOWN", |
| 14341 | #endif |
| 14342 | |
| 14343 | #ifdef HAVE_LSTAT |
| 14344 | "HAVE_LSTAT", |
| 14345 | #endif |
| 14346 | |
| 14347 | #ifdef HAVE_LUTIMES |
| 14348 | "HAVE_LUTIMES", |
| 14349 | #endif |
| 14350 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14351 | #ifdef HAVE_MEMFD_CREATE |
| 14352 | "HAVE_MEMFD_CREATE", |
| 14353 | #endif |
| 14354 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14355 | #ifdef HAVE_MKDIRAT |
| 14356 | "HAVE_MKDIRAT", |
| 14357 | #endif |
| 14358 | |
| 14359 | #ifdef HAVE_MKFIFOAT |
| 14360 | "HAVE_MKFIFOAT", |
| 14361 | #endif |
| 14362 | |
| 14363 | #ifdef HAVE_MKNODAT |
| 14364 | "HAVE_MKNODAT", |
| 14365 | #endif |
| 14366 | |
| 14367 | #ifdef HAVE_OPENAT |
| 14368 | "HAVE_OPENAT", |
| 14369 | #endif |
| 14370 | |
| 14371 | #ifdef HAVE_READLINKAT |
| 14372 | "HAVE_READLINKAT", |
| 14373 | #endif |
| 14374 | |
| 14375 | #ifdef HAVE_RENAMEAT |
| 14376 | "HAVE_RENAMEAT", |
| 14377 | #endif |
| 14378 | |
| 14379 | #ifdef HAVE_SYMLINKAT |
| 14380 | "HAVE_SYMLINKAT", |
| 14381 | #endif |
| 14382 | |
| 14383 | #ifdef HAVE_UNLINKAT |
| 14384 | "HAVE_UNLINKAT", |
| 14385 | #endif |
| 14386 | |
| 14387 | #ifdef HAVE_UTIMENSAT |
| 14388 | "HAVE_UTIMENSAT", |
| 14389 | #endif |
| 14390 | |
| 14391 | #ifdef MS_WINDOWS |
| 14392 | "MS_WINDOWS", |
| 14393 | #endif |
| 14394 | |
| 14395 | NULL |
| 14396 | }; |
| 14397 | |
| 14398 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 14399 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 14400 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14401 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14402 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14403 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14404 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14405 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14406 | m = PyModule_Create(&posixmodule); |
| 14407 | if (m == NULL) |
| 14408 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14409 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14410 | /* Initialize environ dictionary */ |
| 14411 | v = convertenviron(); |
| 14412 | Py_XINCREF(v); |
| 14413 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 14414 | return NULL; |
| 14415 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14416 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14417 | if (all_ins(m)) |
| 14418 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14419 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14420 | if (setup_confname_tables(m)) |
| 14421 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14422 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14423 | Py_INCREF(PyExc_OSError); |
| 14424 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 14425 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 14426 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14427 | if (posix_putenv_garbage == NULL) |
| 14428 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 14429 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 14430 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14431 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14432 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 14433 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14434 | WaitidResultType = PyStructSequence_NewType(&waitid_result_desc); |
| 14435 | if (WaitidResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14436 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14437 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14438 | #endif |
| 14439 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 14440 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14441 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 14442 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 14443 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14444 | StatResultType = PyStructSequence_NewType(&stat_result_desc); |
| 14445 | if (StatResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14446 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14447 | } |
| 14448 | structseq_new = StatResultType->tp_new; |
| 14449 | StatResultType->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14450 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 14451 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14452 | StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc); |
| 14453 | if (StatVFSResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14454 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14455 | } |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14456 | #ifdef NEED_TICKS_PER_SECOND |
| 14457 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14458 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14459 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14460 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14461 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14462 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14463 | # endif |
| 14464 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14465 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 14466 | #if 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] | 14467 | sched_param_desc.name = MODNAME ".sched_param"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14468 | SchedParamType = PyStructSequence_NewType(&sched_param_desc); |
| 14469 | if (SchedParamType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14470 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14471 | } |
| 14472 | SchedParamType->tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14473 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14474 | |
| 14475 | /* initialize TerminalSize_info */ |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14476 | TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc); |
| 14477 | if (TerminalSizeType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14478 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14479 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14480 | |
| 14481 | /* initialize scandir types */ |
| 14482 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 14483 | return NULL; |
| 14484 | if (PyType_Ready(&DirEntryType) < 0) |
| 14485 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14486 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14487 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14488 | Py_INCREF((PyObject*) WaitidResultType); |
| 14489 | PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14490 | #endif |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14491 | Py_INCREF((PyObject*) StatResultType); |
| 14492 | PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType); |
| 14493 | Py_INCREF((PyObject*) StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14494 | PyModule_AddObject(m, "statvfs_result", |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14495 | (PyObject*) StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14496 | |
| 14497 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14498 | Py_INCREF(SchedParamType); |
| 14499 | PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14500 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14501 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14502 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14503 | TimesResultType = PyStructSequence_NewType(×_result_desc); |
| 14504 | if (TimesResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14505 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14506 | } |
| 14507 | PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14508 | |
| 14509 | uname_result_desc.name = MODNAME ".uname_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14510 | UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
| 14511 | if (UnameResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14512 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14513 | } |
| 14514 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14515 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14516 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14517 | /* |
| 14518 | * Step 2 of weak-linking support on Mac OS X. |
| 14519 | * |
| 14520 | * The code below removes functions that are not available on the |
| 14521 | * currently active platform. |
| 14522 | * |
| 14523 | * 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] | 14524 | * 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] | 14525 | * OSX 10.4. |
| 14526 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14527 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14528 | if (fstatvfs == NULL) { |
| 14529 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 14530 | return NULL; |
| 14531 | } |
| 14532 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14533 | #endif /* HAVE_FSTATVFS */ |
| 14534 | |
| 14535 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14536 | if (statvfs == NULL) { |
| 14537 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 14538 | return NULL; |
| 14539 | } |
| 14540 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14541 | #endif /* HAVE_STATVFS */ |
| 14542 | |
| 14543 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14544 | if (lchown == NULL) { |
| 14545 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 14546 | return NULL; |
| 14547 | } |
| 14548 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14549 | #endif /* HAVE_LCHOWN */ |
| 14550 | |
| 14551 | |
| 14552 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14553 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14554 | Py_INCREF(TerminalSizeType); |
| 14555 | PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14556 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 14557 | billion = PyLong_FromLong(1000000000); |
| 14558 | if (!billion) |
| 14559 | return NULL; |
| 14560 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14561 | /* suppress "function not used" warnings */ |
| 14562 | { |
| 14563 | int ignored; |
| 14564 | fd_specified("", -1); |
| 14565 | follow_symlinks_specified("", 1); |
| 14566 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 14567 | dir_fd_converter(Py_None, &ignored); |
| 14568 | dir_fd_unavailable(Py_None, &ignored); |
| 14569 | } |
| 14570 | |
| 14571 | /* |
| 14572 | * provide list of locally available functions |
| 14573 | * so os.py can populate support_* lists |
| 14574 | */ |
| 14575 | list = PyList_New(0); |
| 14576 | if (!list) |
| 14577 | return NULL; |
| 14578 | for (trace = have_functions; *trace; trace++) { |
| 14579 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 14580 | if (!unicode) |
| 14581 | return NULL; |
| 14582 | if (PyList_Append(list, unicode)) |
| 14583 | return NULL; |
| 14584 | Py_DECREF(unicode); |
| 14585 | } |
| 14586 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 14587 | |
| 14588 | Py_INCREF((PyObject *) &DirEntryType); |
Brett Cannon | a32c4d0 | 2016-06-24 14:14:44 -0700 | [diff] [blame] | 14589 | PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14590 | |
| 14591 | initialized = 1; |
| 14592 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14593 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14594 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14595 | |
| 14596 | #ifdef __cplusplus |
| 14597 | } |
| 14598 | #endif |