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); |
| 436 | res = PyObject_CallObject(func, NULL); |
| 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 | |
| 509 | #ifdef MS_WINDOWS |
| 510 | static int |
| 511 | win32_warn_bytes_api() |
| 512 | { |
| 513 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 514 | "The Windows bytes API has been deprecated, " |
| 515 | "use Unicode filenames instead", |
| 516 | 1); |
| 517 | } |
| 518 | #endif |
| 519 | |
| 520 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 521 | #ifndef MS_WINDOWS |
| 522 | PyObject * |
| 523 | _PyLong_FromUid(uid_t uid) |
| 524 | { |
| 525 | if (uid == (uid_t)-1) |
| 526 | return PyLong_FromLong(-1); |
| 527 | return PyLong_FromUnsignedLong(uid); |
| 528 | } |
| 529 | |
| 530 | PyObject * |
| 531 | _PyLong_FromGid(gid_t gid) |
| 532 | { |
| 533 | if (gid == (gid_t)-1) |
| 534 | return PyLong_FromLong(-1); |
| 535 | return PyLong_FromUnsignedLong(gid); |
| 536 | } |
| 537 | |
| 538 | int |
| 539 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 540 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 541 | uid_t uid; |
| 542 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 543 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 544 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 545 | unsigned long uresult; |
| 546 | |
| 547 | index = PyNumber_Index(obj); |
| 548 | if (index == NULL) { |
| 549 | PyErr_Format(PyExc_TypeError, |
| 550 | "uid should be integer, not %.200s", |
| 551 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 552 | return 0; |
| 553 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 554 | |
| 555 | /* |
| 556 | * Handling uid_t is complicated for two reasons: |
| 557 | * * Although uid_t is (always?) unsigned, it still |
| 558 | * accepts -1. |
| 559 | * * We don't know its size in advance--it may be |
| 560 | * bigger than an int, or it may be smaller than |
| 561 | * a long. |
| 562 | * |
| 563 | * So a bit of defensive programming is in order. |
| 564 | * Start with interpreting the value passed |
| 565 | * in as a signed long and see if it works. |
| 566 | */ |
| 567 | |
| 568 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 569 | |
| 570 | if (!overflow) { |
| 571 | uid = (uid_t)result; |
| 572 | |
| 573 | if (result == -1) { |
| 574 | if (PyErr_Occurred()) |
| 575 | goto fail; |
| 576 | /* It's a legitimate -1, we're done. */ |
| 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 | /* Any other negative number is disallowed. */ |
| 581 | if (result < 0) |
| 582 | goto underflow; |
| 583 | |
| 584 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 585 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 586 | (long)uid != result) |
| 587 | goto underflow; |
| 588 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 589 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 590 | |
| 591 | if (overflow < 0) |
| 592 | goto underflow; |
| 593 | |
| 594 | /* |
| 595 | * Okay, the value overflowed a signed long. If it |
| 596 | * fits in an *unsigned* long, it may still be okay, |
| 597 | * as uid_t may be unsigned long on this platform. |
| 598 | */ |
| 599 | uresult = PyLong_AsUnsignedLong(index); |
| 600 | if (PyErr_Occurred()) { |
| 601 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 602 | goto overflow; |
| 603 | goto fail; |
| 604 | } |
| 605 | |
| 606 | uid = (uid_t)uresult; |
| 607 | |
| 608 | /* |
| 609 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 610 | * but this value would get interpreted as (uid_t)-1 by chown |
| 611 | * and its siblings. That's not what the user meant! So we |
| 612 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 613 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 614 | */ |
| 615 | if (uid == (uid_t)-1) |
| 616 | goto overflow; |
| 617 | |
| 618 | /* Ensure the value wasn't truncated. */ |
| 619 | if (sizeof(uid_t) < sizeof(long) && |
| 620 | (unsigned long)uid != uresult) |
| 621 | goto overflow; |
| 622 | /* fallthrough */ |
| 623 | |
| 624 | success: |
| 625 | Py_DECREF(index); |
| 626 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 627 | return 1; |
| 628 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 629 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 630 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 631 | "uid is less than minimum"); |
| 632 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 633 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 634 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 635 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 636 | "uid is greater than maximum"); |
| 637 | /* fallthrough */ |
| 638 | |
| 639 | fail: |
| 640 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | int |
| 645 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 646 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 647 | gid_t gid; |
| 648 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 649 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 650 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 651 | unsigned long uresult; |
| 652 | |
| 653 | index = PyNumber_Index(obj); |
| 654 | if (index == NULL) { |
| 655 | PyErr_Format(PyExc_TypeError, |
| 656 | "gid should be integer, not %.200s", |
| 657 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 658 | return 0; |
| 659 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 660 | |
| 661 | /* |
| 662 | * Handling gid_t is complicated for two reasons: |
| 663 | * * Although gid_t is (always?) unsigned, it still |
| 664 | * accepts -1. |
| 665 | * * We don't know its size in advance--it may be |
| 666 | * bigger than an int, or it may be smaller than |
| 667 | * a long. |
| 668 | * |
| 669 | * So a bit of defensive programming is in order. |
| 670 | * Start with interpreting the value passed |
| 671 | * in as a signed long and see if it works. |
| 672 | */ |
| 673 | |
| 674 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 675 | |
| 676 | if (!overflow) { |
| 677 | gid = (gid_t)result; |
| 678 | |
| 679 | if (result == -1) { |
| 680 | if (PyErr_Occurred()) |
| 681 | goto fail; |
| 682 | /* It's a legitimate -1, we're done. */ |
| 683 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 684 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 685 | |
| 686 | /* Any other negative number is disallowed. */ |
| 687 | if (result < 0) { |
| 688 | goto underflow; |
| 689 | } |
| 690 | |
| 691 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 692 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 693 | (long)gid != result) |
| 694 | goto underflow; |
| 695 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 696 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 697 | |
| 698 | if (overflow < 0) |
| 699 | goto underflow; |
| 700 | |
| 701 | /* |
| 702 | * Okay, the value overflowed a signed long. If it |
| 703 | * fits in an *unsigned* long, it may still be okay, |
| 704 | * as gid_t may be unsigned long on this platform. |
| 705 | */ |
| 706 | uresult = PyLong_AsUnsignedLong(index); |
| 707 | if (PyErr_Occurred()) { |
| 708 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 709 | goto overflow; |
| 710 | goto fail; |
| 711 | } |
| 712 | |
| 713 | gid = (gid_t)uresult; |
| 714 | |
| 715 | /* |
| 716 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 717 | * but this value would get interpreted as (gid_t)-1 by chown |
| 718 | * and its siblings. That's not what the user meant! So we |
| 719 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 720 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 721 | */ |
| 722 | if (gid == (gid_t)-1) |
| 723 | goto overflow; |
| 724 | |
| 725 | /* Ensure the value wasn't truncated. */ |
| 726 | if (sizeof(gid_t) < sizeof(long) && |
| 727 | (unsigned long)gid != uresult) |
| 728 | goto overflow; |
| 729 | /* fallthrough */ |
| 730 | |
| 731 | success: |
| 732 | Py_DECREF(index); |
| 733 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 734 | return 1; |
| 735 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 736 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 737 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 738 | "gid is less than minimum"); |
| 739 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 740 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 741 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 742 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 743 | "gid is greater than maximum"); |
| 744 | /* fallthrough */ |
| 745 | |
| 746 | fail: |
| 747 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | #endif /* MS_WINDOWS */ |
| 751 | |
| 752 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 753 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 754 | |
| 755 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 756 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 757 | static int |
| 758 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 759 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 760 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 761 | if (PyErr_Occurred()) |
| 762 | return 0; |
| 763 | return 1; |
| 764 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 765 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 766 | |
| 767 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 768 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 769 | /* |
| 770 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 771 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 772 | * which doesn't play nicely with all the initializer lines in this file that |
| 773 | * look like this: |
| 774 | * int dir_fd = DEFAULT_DIR_FD; |
| 775 | */ |
| 776 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 777 | #else |
| 778 | #define DEFAULT_DIR_FD (-100) |
| 779 | #endif |
| 780 | |
| 781 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 782 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 783 | { |
| 784 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 785 | long long_value; |
| 786 | |
| 787 | PyObject *index = PyNumber_Index(o); |
| 788 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 789 | return 0; |
| 790 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 791 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 792 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 793 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 794 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 795 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 796 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 797 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 798 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 799 | return 0; |
| 800 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 801 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 802 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 803 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 804 | return 0; |
| 805 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 806 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 807 | *p = (int)long_value; |
| 808 | return 1; |
| 809 | } |
| 810 | |
| 811 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 812 | dir_fd_converter(PyObject *o, void *p) |
| 813 | { |
| 814 | if (o == Py_None) { |
| 815 | *(int *)p = DEFAULT_DIR_FD; |
| 816 | return 1; |
| 817 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 818 | else if (PyIndex_Check(o)) { |
| 819 | return _fd_converter(o, (int *)p); |
| 820 | } |
| 821 | else { |
| 822 | PyErr_Format(PyExc_TypeError, |
| 823 | "argument should be integer or None, not %.200s", |
| 824 | Py_TYPE(o)->tp_name); |
| 825 | return 0; |
| 826 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 830 | /* |
| 831 | * A PyArg_ParseTuple "converter" function |
| 832 | * that handles filesystem paths in the manner |
| 833 | * preferred by the os module. |
| 834 | * |
| 835 | * path_converter accepts (Unicode) strings and their |
| 836 | * subclasses, and bytes and their subclasses. What |
| 837 | * it does with the argument depends on the platform: |
| 838 | * |
| 839 | * * On Windows, if we get a (Unicode) string we |
| 840 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 841 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 842 | * |
| 843 | * * On all other platforms, strings are encoded |
| 844 | * to bytes using PyUnicode_FSConverter, then we |
| 845 | * extract the char * from the bytes object and |
| 846 | * return that. |
| 847 | * |
| 848 | * path_converter also optionally accepts signed |
| 849 | * integers (representing open file descriptors) instead |
| 850 | * of path strings. |
| 851 | * |
| 852 | * Input fields: |
| 853 | * path.nullable |
| 854 | * If nonzero, the path is permitted to be None. |
| 855 | * path.allow_fd |
| 856 | * If nonzero, the path is permitted to be a file handle |
| 857 | * (a signed int) instead of a string. |
| 858 | * path.function_name |
| 859 | * If non-NULL, path_converter will use that as the name |
| 860 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 861 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 862 | * path.argument_name |
| 863 | * If non-NULL, path_converter will use that as the name |
| 864 | * of the parameter in error messages. |
| 865 | * (If path.argument_name is NULL it uses "path".) |
| 866 | * |
| 867 | * Output fields: |
| 868 | * path.wide |
| 869 | * Points to the path if it was expressed as Unicode |
| 870 | * and was not encoded. (Only used on Windows.) |
| 871 | * path.narrow |
| 872 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 873 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 874 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 875 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 876 | * path.fd |
| 877 | * Contains a file descriptor if path.accept_fd was true |
| 878 | * and the caller provided a signed integer instead of any |
| 879 | * sort of string. |
| 880 | * |
| 881 | * WARNING: if your "path" parameter is optional, and is |
| 882 | * unspecified, path_converter will never get called. |
| 883 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 884 | * yourself! |
| 885 | * path.length |
| 886 | * The length of the path in characters, if specified as |
| 887 | * a string. |
| 888 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 889 | * The original object passed in (if get a PathLike object, |
| 890 | * the result of PyOS_FSPath() is treated as the original object). |
| 891 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 892 | * path.cleanup |
| 893 | * For internal use only. May point to a temporary object. |
| 894 | * (Pay no attention to the man behind the curtain.) |
| 895 | * |
| 896 | * At most one of path.wide or path.narrow will be non-NULL. |
| 897 | * If path was None and path.nullable was set, |
| 898 | * or if path was an integer and path.allow_fd was set, |
| 899 | * both path.wide and path.narrow will be NULL |
| 900 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 901 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 902 | * path_converter takes care to not write to the path_t |
| 903 | * unless it's successful. However it must reset the |
| 904 | * "cleanup" field each time it's called. |
| 905 | * |
| 906 | * Use as follows: |
| 907 | * path_t path; |
| 908 | * memset(&path, 0, sizeof(path)); |
| 909 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 910 | * // ... use values from path ... |
| 911 | * path_cleanup(&path); |
| 912 | * |
| 913 | * (Note that if PyArg_Parse fails you don't need to call |
| 914 | * path_cleanup(). However it is safe to do so.) |
| 915 | */ |
| 916 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 917 | const char *function_name; |
| 918 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 919 | int nullable; |
| 920 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 921 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 922 | #ifdef MS_WINDOWS |
| 923 | BOOL narrow; |
| 924 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 925 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 926 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 927 | int fd; |
| 928 | Py_ssize_t length; |
| 929 | PyObject *object; |
| 930 | PyObject *cleanup; |
| 931 | } path_t; |
| 932 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 933 | #ifdef MS_WINDOWS |
| 934 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 935 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 936 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 937 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 938 | {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] | 939 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 940 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 941 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 942 | path_cleanup(path_t *path) |
| 943 | { |
| 944 | Py_CLEAR(path->object); |
| 945 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 949 | path_converter(PyObject *o, void *p) |
| 950 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 951 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 952 | PyObject *bytes = NULL; |
| 953 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 954 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 955 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 956 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 957 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 958 | const wchar_t *wide; |
| 959 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 960 | |
| 961 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 962 | PyErr_Format(exc, "%s%s" fmt, \ |
| 963 | path->function_name ? path->function_name : "", \ |
| 964 | path->function_name ? ": " : "", \ |
| 965 | path->argument_name ? path->argument_name : "path") |
| 966 | |
| 967 | /* Py_CLEANUP_SUPPORTED support */ |
| 968 | if (o == NULL) { |
| 969 | path_cleanup(path); |
| 970 | return 1; |
| 971 | } |
| 972 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 973 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 974 | path->object = path->cleanup = NULL; |
| 975 | /* path->object owns a reference to the original object */ |
| 976 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 977 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 978 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 979 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 980 | #ifdef MS_WINDOWS |
| 981 | path->narrow = FALSE; |
| 982 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 983 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 984 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 985 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 986 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 989 | /* Only call this here so that we don't treat the return value of |
| 990 | os.fspath() as an fd or buffer. */ |
| 991 | is_index = path->allow_fd && PyIndex_Check(o); |
| 992 | is_buffer = PyObject_CheckBuffer(o); |
| 993 | is_bytes = PyBytes_Check(o); |
| 994 | is_unicode = PyUnicode_Check(o); |
| 995 | |
| 996 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 997 | /* Inline PyOS_FSPath() for better error messages. */ |
| 998 | _Py_IDENTIFIER(__fspath__); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 999 | PyObject *func, *res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1000 | |
| 1001 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 1002 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1003 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1004 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1005 | res = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1006 | Py_DECREF(func); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1007 | if (NULL == res) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1008 | goto error_exit; |
| 1009 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1010 | else if (PyUnicode_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1011 | is_unicode = 1; |
| 1012 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1013 | else if (PyBytes_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1014 | is_bytes = 1; |
| 1015 | } |
| 1016 | else { |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1017 | PyErr_Format(PyExc_TypeError, |
| 1018 | "expected %.200s.__fspath__() to return str or bytes, " |
| 1019 | "not %.200s", Py_TYPE(o)->tp_name, |
| 1020 | Py_TYPE(res)->tp_name); |
| 1021 | Py_DECREF(res); |
| 1022 | goto error_exit; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1023 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1024 | |
| 1025 | /* still owns a reference to the original object */ |
| 1026 | Py_DECREF(o); |
| 1027 | o = res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1031 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1032 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1033 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1034 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1035 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1036 | if (length > 32767) { |
| 1037 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1038 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1039 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1040 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1041 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1042 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1043 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1044 | |
| 1045 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1046 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1047 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1048 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1049 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1050 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1051 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1052 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1053 | #endif |
| 1054 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1055 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1056 | bytes = o; |
| 1057 | Py_INCREF(bytes); |
| 1058 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1059 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1060 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1061 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1062 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1063 | "%s%s%s should be %s, not %.200s", |
| 1064 | path->function_name ? path->function_name : "", |
| 1065 | path->function_name ? ": " : "", |
| 1066 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1067 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1068 | "integer or None" : |
| 1069 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1070 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1071 | "string, bytes or os.PathLike", |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1072 | Py_TYPE(o)->tp_name)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1073 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1074 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1075 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1076 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1077 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1078 | } |
| 1079 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1080 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1081 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1082 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1083 | } |
| 1084 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1085 | #ifdef MS_WINDOWS |
| 1086 | path->narrow = FALSE; |
| 1087 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1088 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1089 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1090 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1091 | } |
| 1092 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1093 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1094 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1095 | path->function_name ? path->function_name : "", |
| 1096 | path->function_name ? ": " : "", |
| 1097 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1098 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1099 | "integer or None" : |
| 1100 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1101 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1102 | "string, bytes or os.PathLike", |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1103 | Py_TYPE(o)->tp_name); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1104 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1107 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1108 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1109 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1110 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1111 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1114 | #ifdef MS_WINDOWS |
| 1115 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1116 | narrow, |
| 1117 | length |
| 1118 | ); |
| 1119 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1120 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1123 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1124 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1125 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1126 | } |
| 1127 | if (length > 32767) { |
| 1128 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1129 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1130 | } |
| 1131 | if (wcslen(wide) != length) { |
| 1132 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1133 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1134 | } |
| 1135 | path->wide = wide; |
| 1136 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1137 | path->cleanup = wo; |
| 1138 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1139 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1140 | path->wide = NULL; |
| 1141 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1142 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1143 | /* Still a reference owned by path->object, don't have to |
| 1144 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1145 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1146 | } |
| 1147 | else { |
| 1148 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1149 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1150 | #endif |
| 1151 | path->fd = -1; |
| 1152 | |
| 1153 | success_exit: |
| 1154 | path->length = length; |
| 1155 | path->object = o; |
| 1156 | return Py_CLEANUP_SUPPORTED; |
| 1157 | |
| 1158 | error_exit: |
| 1159 | Py_XDECREF(o); |
| 1160 | Py_XDECREF(bytes); |
| 1161 | #ifdef MS_WINDOWS |
| 1162 | Py_XDECREF(wo); |
| 1163 | #endif |
| 1164 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1168 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1169 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1170 | PyErr_Format(PyExc_NotImplementedError, |
| 1171 | "%s%s%s unavailable on this platform", |
| 1172 | (function_name != NULL) ? function_name : "", |
| 1173 | (function_name != NULL) ? ": ": "", |
| 1174 | argument_name); |
| 1175 | } |
| 1176 | |
| 1177 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1178 | dir_fd_unavailable(PyObject *o, void *p) |
| 1179 | { |
| 1180 | int dir_fd; |
| 1181 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1182 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1183 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1184 | argument_unavailable_error(NULL, "dir_fd"); |
| 1185 | return 0; |
| 1186 | } |
| 1187 | *(int *)p = dir_fd; |
| 1188 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1192 | fd_specified(const char *function_name, int fd) |
| 1193 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1194 | if (fd == -1) |
| 1195 | return 0; |
| 1196 | |
| 1197 | argument_unavailable_error(function_name, "fd"); |
| 1198 | return 1; |
| 1199 | } |
| 1200 | |
| 1201 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1202 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1203 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1204 | if (follow_symlinks) |
| 1205 | return 0; |
| 1206 | |
| 1207 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1208 | return 1; |
| 1209 | } |
| 1210 | |
| 1211 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1212 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1213 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1214 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1215 | #ifndef MS_WINDOWS |
| 1216 | && !path->narrow |
| 1217 | #endif |
| 1218 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1219 | PyErr_Format(PyExc_ValueError, |
| 1220 | "%s: can't specify dir_fd without matching path", |
| 1221 | function_name); |
| 1222 | return 1; |
| 1223 | } |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1228 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1229 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1230 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1231 | PyErr_Format(PyExc_ValueError, |
| 1232 | "%s: can't specify both dir_fd and fd", |
| 1233 | function_name); |
| 1234 | return 1; |
| 1235 | } |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1240 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1241 | int follow_symlinks) |
| 1242 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1243 | if ((fd > 0) && (!follow_symlinks)) { |
| 1244 | PyErr_Format(PyExc_ValueError, |
| 1245 | "%s: cannot use fd and follow_symlinks together", |
| 1246 | function_name); |
| 1247 | return 1; |
| 1248 | } |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1253 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1254 | int follow_symlinks) |
| 1255 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1256 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1257 | PyErr_Format(PyExc_ValueError, |
| 1258 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1259 | function_name); |
| 1260 | return 1; |
| 1261 | } |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1265 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1266 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1267 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1268 | typedef off_t Py_off_t; |
| 1269 | #endif |
| 1270 | |
| 1271 | static int |
| 1272 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1273 | { |
| 1274 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1275 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1276 | #else |
| 1277 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1278 | #endif |
| 1279 | if (PyErr_Occurred()) |
| 1280 | return 0; |
| 1281 | return 1; |
| 1282 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1283 | |
| 1284 | static PyObject * |
| 1285 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1286 | { |
| 1287 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1288 | return PyLong_FromLongLong(offset); |
| 1289 | #else |
| 1290 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1291 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1292 | } |
| 1293 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1294 | #ifdef HAVE_SIGSET_T |
| 1295 | /* Convert an iterable of integers to a sigset. |
| 1296 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1297 | int |
| 1298 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1299 | { |
| 1300 | sigset_t *mask = (sigset_t *)addr; |
| 1301 | PyObject *iterator, *item; |
| 1302 | long signum; |
| 1303 | int overflow; |
| 1304 | |
Rémi Lapeyre | f090019 | 2019-05-04 01:30:53 +0200 | [diff] [blame] | 1305 | // The extra parens suppress the unreachable-code warning with clang on MacOS |
| 1306 | if (sigemptyset(mask) < (0)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1307 | /* Probably only if mask == NULL. */ |
| 1308 | PyErr_SetFromErrno(PyExc_OSError); |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | iterator = PyObject_GetIter(obj); |
| 1313 | if (iterator == NULL) { |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
| 1317 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1318 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1319 | Py_DECREF(item); |
| 1320 | if (signum <= 0 || signum >= NSIG) { |
| 1321 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1322 | PyErr_Format(PyExc_ValueError, |
| 1323 | "signal number %ld out of range", signum); |
| 1324 | } |
| 1325 | goto error; |
| 1326 | } |
| 1327 | if (sigaddset(mask, (int)signum)) { |
| 1328 | if (errno != EINVAL) { |
| 1329 | /* Probably impossible */ |
| 1330 | PyErr_SetFromErrno(PyExc_OSError); |
| 1331 | goto error; |
| 1332 | } |
| 1333 | /* For backwards compatibility, allow idioms such as |
| 1334 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1335 | */ |
| 1336 | const char msg[] = |
| 1337 | "invalid signal number %ld, please use valid_signals()"; |
| 1338 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1339 | goto error; |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | if (!PyErr_Occurred()) { |
| 1344 | Py_DECREF(iterator); |
| 1345 | return 1; |
| 1346 | } |
| 1347 | |
| 1348 | error: |
| 1349 | Py_DECREF(iterator); |
| 1350 | return 0; |
| 1351 | } |
| 1352 | #endif /* HAVE_SIGSET_T */ |
| 1353 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1354 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1355 | |
| 1356 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1357 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1358 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1359 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1360 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1361 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1362 | |
| 1363 | if (0 == DeviceIoControl( |
| 1364 | reparse_point_handle, |
| 1365 | FSCTL_GET_REPARSE_POINT, |
| 1366 | NULL, 0, /* in buffer */ |
| 1367 | target_buffer, sizeof(target_buffer), |
| 1368 | &n_bytes_returned, |
| 1369 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1370 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1371 | |
| 1372 | if (reparse_tag) |
| 1373 | *reparse_tag = rdb->ReparseTag; |
| 1374 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1375 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1376 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1377 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1378 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1379 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1380 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1381 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1382 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1383 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1384 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1385 | */ |
| 1386 | #include <crt_externs.h> |
| 1387 | static char **environ; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 1388 | #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1389 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1390 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1391 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1392 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1393 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1394 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1395 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1396 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1397 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1398 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1399 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1400 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1401 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1402 | d = PyDict_New(); |
| 1403 | if (d == NULL) |
| 1404 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1405 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1406 | if (environ == NULL) |
| 1407 | environ = *_NSGetEnviron(); |
| 1408 | #endif |
| 1409 | #ifdef MS_WINDOWS |
| 1410 | /* _wenviron must be initialized in this way if the program is started |
| 1411 | through main() instead of wmain(). */ |
| 1412 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1413 | e = _wenviron; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1414 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1415 | e = environ; |
| 1416 | #endif |
| 1417 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1418 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1419 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1420 | PyObject *k; |
| 1421 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1422 | #ifdef MS_WINDOWS |
| 1423 | const wchar_t *p = wcschr(*e, L'='); |
| 1424 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1425 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1426 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1427 | if (p == NULL) |
| 1428 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1429 | #ifdef MS_WINDOWS |
| 1430 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1431 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1432 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1433 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1434 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1435 | Py_DECREF(d); |
| 1436 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1437 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1438 | #ifdef MS_WINDOWS |
| 1439 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1440 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1441 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1442 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1443 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1444 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1445 | Py_DECREF(d); |
| 1446 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1447 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1448 | if (PyDict_GetItemWithError(d, k) == NULL) { |
| 1449 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { |
| 1450 | Py_DECREF(v); |
| 1451 | Py_DECREF(k); |
| 1452 | Py_DECREF(d); |
| 1453 | return NULL; |
| 1454 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1455 | } |
| 1456 | Py_DECREF(k); |
| 1457 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1458 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1459 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1462 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1463 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1464 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1465 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1466 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1467 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1468 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1469 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1470 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1471 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1472 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1473 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1474 | /* XXX We should pass the function name along in the future. |
| 1475 | (winreg.c also wants to pass the function name.) |
| 1476 | This would however require an additional param to the |
| 1477 | Windows error object, which is non-trivial. |
| 1478 | */ |
| 1479 | errno = GetLastError(); |
| 1480 | if (filename) |
| 1481 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1482 | else |
| 1483 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1484 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1485 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1486 | static PyObject * |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1487 | win32_error_object_err(const char* function, PyObject* filename, DWORD err) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1488 | { |
| 1489 | /* XXX - see win32_error for comments on 'function' */ |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1490 | if (filename) |
| 1491 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1492 | PyExc_OSError, |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1493 | err, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1494 | filename); |
| 1495 | else |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1496 | return PyErr_SetFromWindowsErr(err); |
| 1497 | } |
| 1498 | |
| 1499 | static PyObject * |
| 1500 | win32_error_object(const char* function, PyObject* filename) |
| 1501 | { |
| 1502 | errno = GetLastError(); |
| 1503 | return win32_error_object_err(function, filename, errno); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1504 | } |
| 1505 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1506 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1507 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1508 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1509 | posix_path_object_error(PyObject *path) |
| 1510 | { |
| 1511 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1512 | } |
| 1513 | |
| 1514 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1515 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1516 | { |
| 1517 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1518 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1519 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1520 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1521 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1522 | #endif |
| 1523 | } |
| 1524 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1525 | static PyObject * |
| 1526 | path_object_error2(PyObject *path, PyObject *path2) |
| 1527 | { |
| 1528 | #ifdef MS_WINDOWS |
| 1529 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1530 | PyExc_OSError, 0, path, path2); |
| 1531 | #else |
| 1532 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1533 | #endif |
| 1534 | } |
| 1535 | |
| 1536 | static PyObject * |
| 1537 | path_error(path_t *path) |
| 1538 | { |
| 1539 | return path_object_error(path->object); |
| 1540 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1541 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1542 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1543 | posix_path_error(path_t *path) |
| 1544 | { |
| 1545 | return posix_path_object_error(path->object); |
| 1546 | } |
| 1547 | |
| 1548 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1549 | path_error2(path_t *path, path_t *path2) |
| 1550 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1551 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1555 | /* POSIX generic methods */ |
| 1556 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1557 | static int |
| 1558 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1559 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1560 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1561 | int *pointer = (int *)p; |
| 1562 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1563 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1564 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1565 | *pointer = fd; |
| 1566 | return 1; |
| 1567 | } |
| 1568 | |
| 1569 | static PyObject * |
| 1570 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1571 | { |
| 1572 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1573 | int async_err = 0; |
| 1574 | |
| 1575 | do { |
| 1576 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1577 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1578 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1579 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1580 | Py_END_ALLOW_THREADS |
| 1581 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1582 | if (res != 0) |
| 1583 | return (!async_err) ? posix_error() : NULL; |
| 1584 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1585 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1586 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1587 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1588 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1589 | /* This is a reimplementation of the C library's chdir function, |
| 1590 | but one that produces Win32 errors instead of DOS error codes. |
| 1591 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1592 | it also needs to set "magic" environment variables indicating |
| 1593 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1594 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1595 | win32_wchdir(LPCWSTR path) |
| 1596 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1597 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1598 | int result; |
| 1599 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1600 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1601 | if(!SetCurrentDirectoryW(path)) |
| 1602 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1603 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1604 | if (!result) |
| 1605 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1606 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1607 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1608 | if (!new_path) { |
| 1609 | SetLastError(ERROR_OUTOFMEMORY); |
| 1610 | return FALSE; |
| 1611 | } |
| 1612 | result = GetCurrentDirectoryW(result, new_path); |
| 1613 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1614 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1615 | return FALSE; |
| 1616 | } |
| 1617 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1618 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1619 | wcsncmp(new_path, L"//", 2) == 0); |
| 1620 | if (!is_unc_like_path) { |
| 1621 | env[1] = new_path[0]; |
| 1622 | result = SetEnvironmentVariableW(env, new_path); |
| 1623 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1624 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1625 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1626 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1627 | } |
| 1628 | #endif |
| 1629 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1630 | #ifdef MS_WINDOWS |
| 1631 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1632 | - time stamps are restricted to second resolution |
| 1633 | - file modification times suffer from forth-and-back conversions between |
| 1634 | UTC and local time |
| 1635 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1636 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1637 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1638 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1639 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1640 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1641 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1642 | BY_HANDLE_FILE_INFORMATION *info, |
| 1643 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1644 | { |
| 1645 | memset(info, 0, sizeof(*info)); |
| 1646 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1647 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1648 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1649 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1650 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1651 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1652 | /* info->nNumberOfLinks = 1; */ |
| 1653 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1654 | *reparse_tag = pFileData->dwReserved0; |
| 1655 | else |
| 1656 | *reparse_tag = 0; |
| 1657 | } |
| 1658 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1659 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1660 | 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] | 1661 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1662 | HANDLE hFindFile; |
| 1663 | WIN32_FIND_DATAW FileData; |
| 1664 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1665 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1666 | return FALSE; |
| 1667 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1668 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1669 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1672 | static BOOL |
| 1673 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1674 | { |
| 1675 | int buf_size, result_length; |
| 1676 | wchar_t *buf; |
| 1677 | |
| 1678 | /* We have a good handle to the target, use it to determine |
| 1679 | the target path name (then we'll call lstat on it). */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1680 | buf_size = GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1681 | VOLUME_NAME_DOS); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1682 | if(!buf_size) |
| 1683 | return FALSE; |
| 1684 | |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1685 | buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1686 | if (!buf) { |
| 1687 | SetLastError(ERROR_OUTOFMEMORY); |
| 1688 | return FALSE; |
| 1689 | } |
| 1690 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1691 | result_length = GetFinalPathNameByHandleW(hdl, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1692 | buf, buf_size, VOLUME_NAME_DOS); |
| 1693 | |
| 1694 | if(!result_length) { |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1695 | PyMem_RawFree(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1696 | return FALSE; |
| 1697 | } |
| 1698 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1699 | buf[result_length] = 0; |
| 1700 | |
| 1701 | *target_path = buf; |
| 1702 | return TRUE; |
| 1703 | } |
| 1704 | |
| 1705 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1706 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1707 | BOOL traverse) |
| 1708 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1709 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1710 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1711 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1712 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1713 | wchar_t *target_path; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1714 | const wchar_t *dot; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1715 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1716 | hFile = CreateFileW( |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1717 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1718 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1719 | 0, /* share mode */ |
| 1720 | NULL, /* security attributes */ |
| 1721 | OPEN_EXISTING, |
| 1722 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1723 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1724 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1725 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1726 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1727 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1728 | NULL); |
| 1729 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1730 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1731 | /* Either the target doesn't exist, or we don't have access to |
| 1732 | get a handle to it. If the former, we need to return an error. |
| 1733 | If the latter, we can use attributes_from_dir. */ |
Berker Peksag | 0b4dc48 | 2016-09-17 15:49:59 +0300 | [diff] [blame] | 1734 | DWORD lastError = GetLastError(); |
| 1735 | if (lastError != ERROR_ACCESS_DENIED && |
| 1736 | lastError != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1737 | return -1; |
| 1738 | /* Could not get attributes on open file. Fall back to |
| 1739 | reading the directory. */ |
| 1740 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1741 | /* Very strange. This should not fail now */ |
| 1742 | return -1; |
| 1743 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1744 | if (traverse) { |
| 1745 | /* Should traverse, but could not open reparse point handle */ |
Berker Peksag | 0b4dc48 | 2016-09-17 15:49:59 +0300 | [diff] [blame] | 1746 | SetLastError(lastError); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1747 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1748 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1749 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1750 | } else { |
| 1751 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1752 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1753 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1754 | } |
| 1755 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1756 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) { |
| 1757 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1758 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1759 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1760 | /* Close the outer open file handle now that we're about to |
| 1761 | reopen it with different flags. */ |
| 1762 | if (!CloseHandle(hFile)) |
| 1763 | return -1; |
| 1764 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1765 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1766 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1767 | the file without the reparse handling flag set. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1768 | hFile2 = CreateFileW( |
| 1769 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1770 | NULL, OPEN_EXISTING, |
| 1771 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1772 | NULL); |
| 1773 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1774 | return -1; |
| 1775 | |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1776 | if (!get_target_path(hFile2, &target_path)) { |
| 1777 | CloseHandle(hFile2); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1778 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | if (!CloseHandle(hFile2)) { |
| 1782 | return -1; |
| 1783 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1784 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1785 | code = win32_xstat_impl(target_path, result, FALSE); |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1786 | PyMem_RawFree(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1787 | return code; |
| 1788 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1789 | } else |
| 1790 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1791 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1792 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1793 | |
| 1794 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1795 | dot = wcsrchr(path, '.'); |
| 1796 | if (dot) { |
| 1797 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1798 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1799 | result->st_mode |= 0111; |
| 1800 | } |
| 1801 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1805 | 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] | 1806 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1807 | /* Protocol violation: we explicitly clear errno, instead of |
| 1808 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1809 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1810 | errno = 0; |
| 1811 | return code; |
| 1812 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1813 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1814 | |
| 1815 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1816 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1817 | default does not traverse symlinks and instead returns attributes for |
| 1818 | the symlink. |
| 1819 | |
| 1820 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1821 | win32_stat will first explicitly resolve the symlink target and then will |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1822 | call win32_lstat on that result. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1823 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1824 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1825 | 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] | 1826 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1827 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1830 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1831 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1832 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1833 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1836 | #endif /* MS_WINDOWS */ |
| 1837 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1838 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1839 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1840 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1841 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1842 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1843 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1844 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1845 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1846 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1847 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1848 | |
| 1849 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1850 | {"st_mode", "protection bits"}, |
| 1851 | {"st_ino", "inode"}, |
| 1852 | {"st_dev", "device"}, |
| 1853 | {"st_nlink", "number of hard links"}, |
| 1854 | {"st_uid", "user ID of owner"}, |
| 1855 | {"st_gid", "group ID of owner"}, |
| 1856 | {"st_size", "total size, in bytes"}, |
| 1857 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1858 | {NULL, "integer time of last access"}, |
| 1859 | {NULL, "integer time of last modification"}, |
| 1860 | {NULL, "integer time of last change"}, |
| 1861 | {"st_atime", "time of last access"}, |
| 1862 | {"st_mtime", "time of last modification"}, |
| 1863 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1864 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1865 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1866 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1867 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1868 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1869 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1870 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1871 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1872 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1873 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1874 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1875 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1876 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1877 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1878 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1879 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1880 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1881 | #endif |
| 1882 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1883 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1884 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1885 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1886 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1887 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1888 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1889 | {"st_fstype", "Type of filesystem"}, |
| 1890 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1891 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1892 | }; |
| 1893 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1894 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1895 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1896 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1897 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1898 | #endif |
| 1899 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1900 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1901 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1902 | #else |
| 1903 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1904 | #endif |
| 1905 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1906 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1907 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1908 | #else |
| 1909 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1910 | #endif |
| 1911 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1912 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1913 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1914 | #else |
| 1915 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1916 | #endif |
| 1917 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1918 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1919 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1920 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1921 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1922 | #endif |
| 1923 | |
| 1924 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1925 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1926 | #else |
| 1927 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1928 | #endif |
| 1929 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1930 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1931 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1932 | #else |
| 1933 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 1934 | #endif |
| 1935 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1936 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1937 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 1938 | #else |
| 1939 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 1940 | #endif |
| 1941 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1942 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1943 | "stat_result", /* name */ |
| 1944 | stat_result__doc__, /* doc */ |
| 1945 | stat_result_fields, |
| 1946 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1947 | }; |
| 1948 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1949 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1950 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1951 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1952 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1953 | 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] | 1954 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1955 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1956 | |
| 1957 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1958 | {"f_bsize", }, |
| 1959 | {"f_frsize", }, |
| 1960 | {"f_blocks", }, |
| 1961 | {"f_bfree", }, |
| 1962 | {"f_bavail", }, |
| 1963 | {"f_files", }, |
| 1964 | {"f_ffree", }, |
| 1965 | {"f_favail", }, |
| 1966 | {"f_flag", }, |
| 1967 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 1968 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1969 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1970 | }; |
| 1971 | |
| 1972 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1973 | "statvfs_result", /* name */ |
| 1974 | statvfs_result__doc__, /* doc */ |
| 1975 | statvfs_result_fields, |
| 1976 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1977 | }; |
| 1978 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1979 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1980 | PyDoc_STRVAR(waitid_result__doc__, |
| 1981 | "waitid_result: Result from waitid.\n\n\ |
| 1982 | This object may be accessed either as a tuple of\n\ |
| 1983 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1984 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1985 | \n\ |
| 1986 | See os.waitid for more information."); |
| 1987 | |
| 1988 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1989 | {"si_pid", }, |
| 1990 | {"si_uid", }, |
| 1991 | {"si_signo", }, |
| 1992 | {"si_status", }, |
| 1993 | {"si_code", }, |
| 1994 | {0} |
| 1995 | }; |
| 1996 | |
| 1997 | static PyStructSequence_Desc waitid_result_desc = { |
| 1998 | "waitid_result", /* name */ |
| 1999 | waitid_result__doc__, /* doc */ |
| 2000 | waitid_result_fields, |
| 2001 | 5 |
| 2002 | }; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2003 | static PyTypeObject* WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2004 | #endif |
| 2005 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2006 | static int initialized; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2007 | static PyTypeObject* StatResultType; |
| 2008 | static PyTypeObject* StatVFSResultType; |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 2009 | #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] | 2010 | static PyTypeObject* SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2011 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2012 | static newfunc structseq_new; |
| 2013 | |
| 2014 | static PyObject * |
| 2015 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2016 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2017 | PyStructSequence *result; |
| 2018 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2019 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2020 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2021 | if (!result) |
| 2022 | return NULL; |
| 2023 | /* If we have been initialized from a tuple, |
| 2024 | st_?time might be set to None. Initialize it |
| 2025 | from the int slots. */ |
| 2026 | for (i = 7; i <= 9; i++) { |
| 2027 | if (result->ob_item[i+3] == Py_None) { |
| 2028 | Py_DECREF(Py_None); |
| 2029 | Py_INCREF(result->ob_item[i]); |
| 2030 | result->ob_item[i+3] = result->ob_item[i]; |
| 2031 | } |
| 2032 | } |
| 2033 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2037 | static PyObject *billion = NULL; |
| 2038 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2039 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2040 | 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] | 2041 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2042 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2043 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2044 | PyObject *s_in_ns = NULL; |
| 2045 | PyObject *ns_total = NULL; |
| 2046 | PyObject *float_s = NULL; |
| 2047 | |
| 2048 | if (!(s && ns_fractional)) |
| 2049 | goto exit; |
| 2050 | |
| 2051 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2052 | if (!s_in_ns) |
| 2053 | goto exit; |
| 2054 | |
| 2055 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2056 | if (!ns_total) |
| 2057 | goto exit; |
| 2058 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2059 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2060 | if (!float_s) { |
| 2061 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | PyStructSequence_SET_ITEM(v, index, s); |
| 2065 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2066 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2067 | s = NULL; |
| 2068 | float_s = NULL; |
| 2069 | ns_total = NULL; |
| 2070 | exit: |
| 2071 | Py_XDECREF(s); |
| 2072 | Py_XDECREF(ns_fractional); |
| 2073 | Py_XDECREF(s_in_ns); |
| 2074 | Py_XDECREF(ns_total); |
| 2075 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2078 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2079 | (used by posix_stat() and posix_fstat()) */ |
| 2080 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2081 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2082 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2083 | unsigned long ansec, mnsec, cnsec; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2084 | PyObject *v = PyStructSequence_New(StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2085 | if (v == NULL) |
| 2086 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2087 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2088 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2089 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2090 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2091 | #ifdef MS_WINDOWS |
| 2092 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2093 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2094 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2095 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2096 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2097 | #if defined(MS_WINDOWS) |
| 2098 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2099 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2100 | #else |
| 2101 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2102 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2103 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2104 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2105 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2106 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2107 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2108 | ansec = st->st_atim.tv_nsec; |
| 2109 | mnsec = st->st_mtim.tv_nsec; |
| 2110 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2111 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2112 | ansec = st->st_atimespec.tv_nsec; |
| 2113 | mnsec = st->st_mtimespec.tv_nsec; |
| 2114 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2115 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2116 | ansec = st->st_atime_nsec; |
| 2117 | mnsec = st->st_mtime_nsec; |
| 2118 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2119 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2120 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2121 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2122 | fill_time(v, 7, st->st_atime, ansec); |
| 2123 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2124 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2125 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2126 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2127 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2128 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2129 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2130 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2131 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2132 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2133 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2134 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2135 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2136 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2137 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2138 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2139 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2140 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2141 | #endif |
| 2142 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2143 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2144 | PyObject *val; |
| 2145 | unsigned long bsec,bnsec; |
| 2146 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2147 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2148 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2149 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2150 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2151 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2152 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2153 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2154 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2155 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2156 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2157 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2158 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2159 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2160 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2161 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2162 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2163 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2164 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2165 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2166 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2167 | PyUnicode_FromString(st->st_fstype)); |
| 2168 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2169 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2170 | if (PyErr_Occurred()) { |
| 2171 | Py_DECREF(v); |
| 2172 | return NULL; |
| 2173 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2174 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2175 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2178 | /* POSIX methods */ |
| 2179 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2180 | |
| 2181 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2182 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2183 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2184 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2185 | STRUCT_STAT st; |
| 2186 | int result; |
| 2187 | |
| 2188 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2189 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2190 | return NULL; |
| 2191 | #endif |
| 2192 | |
| 2193 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2194 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2195 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2196 | return NULL; |
| 2197 | |
| 2198 | Py_BEGIN_ALLOW_THREADS |
| 2199 | if (path->fd != -1) |
| 2200 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2201 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2202 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2203 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2204 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2205 | result = win32_lstat(path->wide, &st); |
| 2206 | #else |
| 2207 | else |
| 2208 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2209 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2210 | result = LSTAT(path->narrow, &st); |
| 2211 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2212 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2213 | #ifdef HAVE_FSTATAT |
| 2214 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2215 | result = fstatat(dir_fd, path->narrow, &st, |
| 2216 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2217 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2218 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2219 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2220 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2221 | Py_END_ALLOW_THREADS |
| 2222 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2223 | if (result != 0) { |
| 2224 | return path_error(path); |
| 2225 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2226 | |
| 2227 | return _pystat_fromstructstat(&st); |
| 2228 | } |
| 2229 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2230 | /*[python input] |
| 2231 | |
| 2232 | for s in """ |
| 2233 | |
| 2234 | FACCESSAT |
| 2235 | FCHMODAT |
| 2236 | FCHOWNAT |
| 2237 | FSTATAT |
| 2238 | LINKAT |
| 2239 | MKDIRAT |
| 2240 | MKFIFOAT |
| 2241 | MKNODAT |
| 2242 | OPENAT |
| 2243 | READLINKAT |
| 2244 | SYMLINKAT |
| 2245 | UNLINKAT |
| 2246 | |
| 2247 | """.strip().split(): |
| 2248 | s = s.strip() |
| 2249 | print(""" |
| 2250 | #ifdef HAVE_{s} |
| 2251 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2252 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2253 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2254 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2255 | """.rstrip().format(s=s)) |
| 2256 | |
| 2257 | for s in """ |
| 2258 | |
| 2259 | FCHDIR |
| 2260 | FCHMOD |
| 2261 | FCHOWN |
| 2262 | FDOPENDIR |
| 2263 | FEXECVE |
| 2264 | FPATHCONF |
| 2265 | FSTATVFS |
| 2266 | FTRUNCATE |
| 2267 | |
| 2268 | """.strip().split(): |
| 2269 | s = s.strip() |
| 2270 | print(""" |
| 2271 | #ifdef HAVE_{s} |
| 2272 | #define PATH_HAVE_{s} 1 |
| 2273 | #else |
| 2274 | #define PATH_HAVE_{s} 0 |
| 2275 | #endif |
| 2276 | |
| 2277 | """.rstrip().format(s=s)) |
| 2278 | [python start generated code]*/ |
| 2279 | |
| 2280 | #ifdef HAVE_FACCESSAT |
| 2281 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2282 | #else |
| 2283 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2284 | #endif |
| 2285 | |
| 2286 | #ifdef HAVE_FCHMODAT |
| 2287 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2288 | #else |
| 2289 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2290 | #endif |
| 2291 | |
| 2292 | #ifdef HAVE_FCHOWNAT |
| 2293 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2294 | #else |
| 2295 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2296 | #endif |
| 2297 | |
| 2298 | #ifdef HAVE_FSTATAT |
| 2299 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2300 | #else |
| 2301 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2302 | #endif |
| 2303 | |
| 2304 | #ifdef HAVE_LINKAT |
| 2305 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2306 | #else |
| 2307 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2308 | #endif |
| 2309 | |
| 2310 | #ifdef HAVE_MKDIRAT |
| 2311 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2312 | #else |
| 2313 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2314 | #endif |
| 2315 | |
| 2316 | #ifdef HAVE_MKFIFOAT |
| 2317 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2318 | #else |
| 2319 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2320 | #endif |
| 2321 | |
| 2322 | #ifdef HAVE_MKNODAT |
| 2323 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2324 | #else |
| 2325 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2326 | #endif |
| 2327 | |
| 2328 | #ifdef HAVE_OPENAT |
| 2329 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2330 | #else |
| 2331 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2332 | #endif |
| 2333 | |
| 2334 | #ifdef HAVE_READLINKAT |
| 2335 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2336 | #else |
| 2337 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2338 | #endif |
| 2339 | |
| 2340 | #ifdef HAVE_SYMLINKAT |
| 2341 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2342 | #else |
| 2343 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2344 | #endif |
| 2345 | |
| 2346 | #ifdef HAVE_UNLINKAT |
| 2347 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2348 | #else |
| 2349 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2350 | #endif |
| 2351 | |
| 2352 | #ifdef HAVE_FCHDIR |
| 2353 | #define PATH_HAVE_FCHDIR 1 |
| 2354 | #else |
| 2355 | #define PATH_HAVE_FCHDIR 0 |
| 2356 | #endif |
| 2357 | |
| 2358 | #ifdef HAVE_FCHMOD |
| 2359 | #define PATH_HAVE_FCHMOD 1 |
| 2360 | #else |
| 2361 | #define PATH_HAVE_FCHMOD 0 |
| 2362 | #endif |
| 2363 | |
| 2364 | #ifdef HAVE_FCHOWN |
| 2365 | #define PATH_HAVE_FCHOWN 1 |
| 2366 | #else |
| 2367 | #define PATH_HAVE_FCHOWN 0 |
| 2368 | #endif |
| 2369 | |
| 2370 | #ifdef HAVE_FDOPENDIR |
| 2371 | #define PATH_HAVE_FDOPENDIR 1 |
| 2372 | #else |
| 2373 | #define PATH_HAVE_FDOPENDIR 0 |
| 2374 | #endif |
| 2375 | |
| 2376 | #ifdef HAVE_FEXECVE |
| 2377 | #define PATH_HAVE_FEXECVE 1 |
| 2378 | #else |
| 2379 | #define PATH_HAVE_FEXECVE 0 |
| 2380 | #endif |
| 2381 | |
| 2382 | #ifdef HAVE_FPATHCONF |
| 2383 | #define PATH_HAVE_FPATHCONF 1 |
| 2384 | #else |
| 2385 | #define PATH_HAVE_FPATHCONF 0 |
| 2386 | #endif |
| 2387 | |
| 2388 | #ifdef HAVE_FSTATVFS |
| 2389 | #define PATH_HAVE_FSTATVFS 1 |
| 2390 | #else |
| 2391 | #define PATH_HAVE_FSTATVFS 0 |
| 2392 | #endif |
| 2393 | |
| 2394 | #ifdef HAVE_FTRUNCATE |
| 2395 | #define PATH_HAVE_FTRUNCATE 1 |
| 2396 | #else |
| 2397 | #define PATH_HAVE_FTRUNCATE 0 |
| 2398 | #endif |
| 2399 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2400 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2401 | #ifdef MS_WINDOWS |
| 2402 | #undef PATH_HAVE_FTRUNCATE |
| 2403 | #define PATH_HAVE_FTRUNCATE 1 |
| 2404 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2405 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2406 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2407 | |
| 2408 | class path_t_converter(CConverter): |
| 2409 | |
| 2410 | type = "path_t" |
| 2411 | impl_by_reference = True |
| 2412 | parse_by_reference = True |
| 2413 | |
| 2414 | converter = 'path_converter' |
| 2415 | |
| 2416 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2417 | # right now path_t doesn't support default values. |
| 2418 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2419 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2420 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2421 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2422 | if self.c_default not in (None, 'Py_None'): |
| 2423 | 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] | 2424 | |
| 2425 | self.nullable = nullable |
| 2426 | self.allow_fd = allow_fd |
| 2427 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2428 | def pre_render(self): |
| 2429 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2430 | if isinstance(value, str): |
| 2431 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2432 | return str(int(bool(value))) |
| 2433 | |
| 2434 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2435 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2436 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2437 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2438 | strify(self.nullable), |
| 2439 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2440 | ) |
| 2441 | |
| 2442 | def cleanup(self): |
| 2443 | return "path_cleanup(&" + self.name + ");\n" |
| 2444 | |
| 2445 | |
| 2446 | class dir_fd_converter(CConverter): |
| 2447 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2448 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2449 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2450 | if self.default in (unspecified, None): |
| 2451 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2452 | if isinstance(requires, str): |
| 2453 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2454 | else: |
| 2455 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2456 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2457 | class fildes_converter(CConverter): |
| 2458 | type = 'int' |
| 2459 | converter = 'fildes_converter' |
| 2460 | |
| 2461 | class uid_t_converter(CConverter): |
| 2462 | type = "uid_t" |
| 2463 | converter = '_Py_Uid_Converter' |
| 2464 | |
| 2465 | class gid_t_converter(CConverter): |
| 2466 | type = "gid_t" |
| 2467 | converter = '_Py_Gid_Converter' |
| 2468 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2469 | class dev_t_converter(CConverter): |
| 2470 | type = 'dev_t' |
| 2471 | converter = '_Py_Dev_Converter' |
| 2472 | |
| 2473 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2474 | type = 'dev_t' |
| 2475 | conversion_fn = '_PyLong_FromDev' |
| 2476 | unsigned_cast = '(dev_t)' |
| 2477 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2478 | class FSConverter_converter(CConverter): |
| 2479 | type = 'PyObject *' |
| 2480 | converter = 'PyUnicode_FSConverter' |
| 2481 | def converter_init(self): |
| 2482 | if self.default is not unspecified: |
| 2483 | fail("FSConverter_converter does not support default values") |
| 2484 | self.c_default = 'NULL' |
| 2485 | |
| 2486 | def cleanup(self): |
| 2487 | return "Py_XDECREF(" + self.name + ");\n" |
| 2488 | |
| 2489 | class pid_t_converter(CConverter): |
| 2490 | type = 'pid_t' |
| 2491 | format_unit = '" _Py_PARSE_PID "' |
| 2492 | |
| 2493 | class idtype_t_converter(int_converter): |
| 2494 | type = 'idtype_t' |
| 2495 | |
| 2496 | class id_t_converter(CConverter): |
| 2497 | type = 'id_t' |
| 2498 | format_unit = '" _Py_PARSE_PID "' |
| 2499 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2500 | class intptr_t_converter(CConverter): |
| 2501 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2502 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2503 | |
| 2504 | class Py_off_t_converter(CConverter): |
| 2505 | type = 'Py_off_t' |
| 2506 | converter = 'Py_off_t_converter' |
| 2507 | |
| 2508 | class Py_off_t_return_converter(long_return_converter): |
| 2509 | type = 'Py_off_t' |
| 2510 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2511 | |
| 2512 | class path_confname_converter(CConverter): |
| 2513 | type="int" |
| 2514 | converter="conv_path_confname" |
| 2515 | |
| 2516 | class confstr_confname_converter(path_confname_converter): |
| 2517 | converter='conv_confstr_confname' |
| 2518 | |
| 2519 | class sysconf_confname_converter(path_confname_converter): |
| 2520 | converter="conv_sysconf_confname" |
| 2521 | |
| 2522 | class sched_param_converter(CConverter): |
| 2523 | type = 'struct sched_param' |
| 2524 | converter = 'convert_sched_param' |
| 2525 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2526 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2527 | [python start generated code]*/ |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 2528 | /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2529 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2530 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2531 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2532 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2533 | |
| 2534 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2535 | 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] | 2536 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2537 | |
| 2538 | * |
| 2539 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2540 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2541 | If not None, it should be a file descriptor open to a directory, |
| 2542 | and path should be a relative string; path will then be relative to |
| 2543 | that directory. |
| 2544 | |
| 2545 | follow_symlinks: bool = True |
| 2546 | If False, and the last element of the path is a symbolic link, |
| 2547 | stat will examine the symbolic link itself instead of the file |
| 2548 | the link points to. |
| 2549 | |
| 2550 | Perform a stat system call on the given path. |
| 2551 | |
| 2552 | dir_fd and follow_symlinks may not be implemented |
| 2553 | on your platform. If they are unavailable, using them will raise a |
| 2554 | NotImplementedError. |
| 2555 | |
| 2556 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2557 | an open file descriptor. |
| 2558 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2559 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2560 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2561 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2562 | 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] | 2563 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2564 | { |
| 2565 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2566 | } |
| 2567 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2568 | |
| 2569 | /*[clinic input] |
| 2570 | os.lstat |
| 2571 | |
| 2572 | path : path_t |
| 2573 | |
| 2574 | * |
| 2575 | |
| 2576 | dir_fd : dir_fd(requires='fstatat') = None |
| 2577 | |
| 2578 | Perform a stat system call on the given path, without following symbolic links. |
| 2579 | |
| 2580 | Like stat(), but do not follow symbolic links. |
| 2581 | Equivalent to stat(path, follow_symlinks=False). |
| 2582 | [clinic start generated code]*/ |
| 2583 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2584 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2585 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2586 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2587 | { |
| 2588 | int follow_symlinks = 0; |
| 2589 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2590 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2591 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2592 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2593 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2594 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2595 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2596 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2597 | 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] | 2598 | |
| 2599 | mode: int |
| 2600 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2601 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2602 | |
| 2603 | * |
| 2604 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2605 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2606 | If not None, it should be a file descriptor open to a directory, |
| 2607 | and path should be relative; path will then be relative to that |
| 2608 | directory. |
| 2609 | |
| 2610 | effective_ids: bool = False |
| 2611 | If True, access will use the effective uid/gid instead of |
| 2612 | the real uid/gid. |
| 2613 | |
| 2614 | follow_symlinks: bool = True |
| 2615 | If False, and the last element of the path is a symbolic link, |
| 2616 | access will examine the symbolic link itself instead of the file |
| 2617 | the link points to. |
| 2618 | |
| 2619 | Use the real uid/gid to test for access to a path. |
| 2620 | |
| 2621 | {parameters} |
| 2622 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2623 | on your platform. If they are unavailable, using them will raise a |
| 2624 | NotImplementedError. |
| 2625 | |
| 2626 | Note that most operations will use the effective uid/gid, therefore this |
| 2627 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2628 | has the specified access to the path. |
| 2629 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2630 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2631 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2632 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2633 | 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] | 2634 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2635 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2636 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2637 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2638 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2639 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2640 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2641 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2642 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2643 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2644 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2645 | #ifndef HAVE_FACCESSAT |
| 2646 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2647 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2648 | |
| 2649 | if (effective_ids) { |
| 2650 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2651 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2652 | } |
| 2653 | #endif |
| 2654 | |
| 2655 | #ifdef MS_WINDOWS |
| 2656 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2657 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2658 | Py_END_ALLOW_THREADS |
| 2659 | |
| 2660 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2661 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2662 | * * we didn't get a -1, and |
| 2663 | * * write access wasn't requested, |
| 2664 | * * or the file isn't read-only, |
| 2665 | * * or it's a directory. |
| 2666 | * (Directories cannot be read-only on Windows.) |
| 2667 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2668 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2669 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2670 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2671 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2672 | #else |
| 2673 | |
| 2674 | Py_BEGIN_ALLOW_THREADS |
| 2675 | #ifdef HAVE_FACCESSAT |
| 2676 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2677 | effective_ids || |
| 2678 | !follow_symlinks) { |
| 2679 | int flags = 0; |
| 2680 | if (!follow_symlinks) |
| 2681 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2682 | if (effective_ids) |
| 2683 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2684 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2685 | } |
| 2686 | else |
| 2687 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2688 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2689 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2690 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2691 | #endif |
| 2692 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2693 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2696 | #ifndef F_OK |
| 2697 | #define F_OK 0 |
| 2698 | #endif |
| 2699 | #ifndef R_OK |
| 2700 | #define R_OK 4 |
| 2701 | #endif |
| 2702 | #ifndef W_OK |
| 2703 | #define W_OK 2 |
| 2704 | #endif |
| 2705 | #ifndef X_OK |
| 2706 | #define X_OK 1 |
| 2707 | #endif |
| 2708 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2709 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2710 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2711 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2712 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2713 | |
| 2714 | fd: int |
| 2715 | Integer file descriptor handle. |
| 2716 | |
| 2717 | / |
| 2718 | |
| 2719 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2720 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2721 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2722 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2723 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2724 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2725 | { |
| 2726 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2727 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2728 | ret = ttyname(fd); |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2729 | if (ret == NULL) { |
| 2730 | return posix_error(); |
| 2731 | } |
| 2732 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2733 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2734 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2735 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2736 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2737 | /*[clinic input] |
| 2738 | os.ctermid |
| 2739 | |
| 2740 | Return the name of the controlling terminal for this process. |
| 2741 | [clinic start generated code]*/ |
| 2742 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2743 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2744 | os_ctermid_impl(PyObject *module) |
| 2745 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2746 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2747 | char *ret; |
| 2748 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2749 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2750 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2751 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2752 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2753 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2754 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2755 | if (ret == NULL) |
| 2756 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2757 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2758 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2759 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2760 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2761 | |
| 2762 | /*[clinic input] |
| 2763 | os.chdir |
| 2764 | |
| 2765 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2766 | |
| 2767 | Change the current working directory to the specified path. |
| 2768 | |
| 2769 | path may always be specified as a string. |
| 2770 | On some platforms, path may also be specified as an open file descriptor. |
| 2771 | If this functionality is unavailable, using it raises an exception. |
| 2772 | [clinic start generated code]*/ |
| 2773 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2774 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2775 | os_chdir_impl(PyObject *module, path_t *path) |
| 2776 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2777 | { |
| 2778 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2779 | |
| 2780 | Py_BEGIN_ALLOW_THREADS |
| 2781 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2782 | /* on unix, success = 0, on windows, success = !0 */ |
| 2783 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2784 | #else |
| 2785 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2786 | if (path->fd != -1) |
| 2787 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2788 | else |
| 2789 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2790 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2791 | #endif |
| 2792 | Py_END_ALLOW_THREADS |
| 2793 | |
| 2794 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2795 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2796 | } |
| 2797 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2798 | Py_RETURN_NONE; |
| 2799 | } |
| 2800 | |
| 2801 | |
| 2802 | #ifdef HAVE_FCHDIR |
| 2803 | /*[clinic input] |
| 2804 | os.fchdir |
| 2805 | |
| 2806 | fd: fildes |
| 2807 | |
| 2808 | Change to the directory of the given file descriptor. |
| 2809 | |
| 2810 | fd must be opened on a directory, not a file. |
| 2811 | Equivalent to os.chdir(fd). |
| 2812 | |
| 2813 | [clinic start generated code]*/ |
| 2814 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2815 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2816 | os_fchdir_impl(PyObject *module, int fd) |
| 2817 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2818 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2819 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2820 | } |
| 2821 | #endif /* HAVE_FCHDIR */ |
| 2822 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2823 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2824 | /*[clinic input] |
| 2825 | os.chmod |
| 2826 | |
| 2827 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2828 | 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] | 2829 | On some platforms, path may also be specified as an open file descriptor. |
| 2830 | If this functionality is unavailable, using it raises an exception. |
| 2831 | |
| 2832 | mode: int |
| 2833 | Operating-system mode bitfield. |
| 2834 | |
| 2835 | * |
| 2836 | |
| 2837 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2838 | If not None, it should be a file descriptor open to a directory, |
| 2839 | and path should be relative; path will then be relative to that |
| 2840 | directory. |
| 2841 | |
| 2842 | follow_symlinks: bool = True |
| 2843 | If False, and the last element of the path is a symbolic link, |
| 2844 | chmod will modify the symbolic link itself instead of the file |
| 2845 | the link points to. |
| 2846 | |
| 2847 | Change the access permissions of a file. |
| 2848 | |
| 2849 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2850 | an open file descriptor. |
| 2851 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2852 | If they are unavailable, using them will raise a NotImplementedError. |
| 2853 | |
| 2854 | [clinic start generated code]*/ |
| 2855 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2856 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2857 | 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] | 2858 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2859 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2860 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2861 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2862 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2863 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2864 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2865 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2866 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2867 | #ifdef HAVE_FCHMODAT |
| 2868 | int fchmodat_nofollow_unsupported = 0; |
| 2869 | #endif |
| 2870 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2871 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2872 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2873 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2874 | #endif |
| 2875 | |
| 2876 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2877 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2878 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2879 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2880 | result = 0; |
| 2881 | else { |
| 2882 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2883 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2884 | else |
| 2885 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2886 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2887 | } |
| 2888 | Py_END_ALLOW_THREADS |
| 2889 | |
| 2890 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2891 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2892 | } |
| 2893 | #else /* MS_WINDOWS */ |
| 2894 | Py_BEGIN_ALLOW_THREADS |
| 2895 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2896 | if (path->fd != -1) |
| 2897 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2898 | else |
| 2899 | #endif |
| 2900 | #ifdef HAVE_LCHMOD |
| 2901 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2902 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2903 | else |
| 2904 | #endif |
| 2905 | #ifdef HAVE_FCHMODAT |
| 2906 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2907 | /* |
| 2908 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2909 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2910 | * and then says it isn't implemented yet. |
| 2911 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2912 | * |
| 2913 | * Once it is supported, os.chmod will automatically |
| 2914 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2915 | * Until then, we need to be careful what exception we raise. |
| 2916 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2917 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2918 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2919 | /* |
| 2920 | * But wait! We can't throw the exception without allowing threads, |
| 2921 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2922 | */ |
| 2923 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2924 | result && |
| 2925 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2926 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2927 | } |
| 2928 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2929 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2930 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2931 | Py_END_ALLOW_THREADS |
| 2932 | |
| 2933 | if (result) { |
| 2934 | #ifdef HAVE_FCHMODAT |
| 2935 | if (fchmodat_nofollow_unsupported) { |
| 2936 | if (dir_fd != DEFAULT_DIR_FD) |
| 2937 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2938 | dir_fd, follow_symlinks); |
| 2939 | else |
| 2940 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 2941 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2942 | } |
| 2943 | else |
| 2944 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2945 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2946 | } |
| 2947 | #endif |
| 2948 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2949 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2952 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2953 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2954 | /*[clinic input] |
| 2955 | os.fchmod |
| 2956 | |
| 2957 | fd: int |
| 2958 | mode: int |
| 2959 | |
| 2960 | Change the access permissions of the file given by file descriptor fd. |
| 2961 | |
| 2962 | Equivalent to os.chmod(fd, mode). |
| 2963 | [clinic start generated code]*/ |
| 2964 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2965 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2966 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 2967 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2968 | { |
| 2969 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2970 | int async_err = 0; |
| 2971 | |
| 2972 | do { |
| 2973 | Py_BEGIN_ALLOW_THREADS |
| 2974 | res = fchmod(fd, mode); |
| 2975 | Py_END_ALLOW_THREADS |
| 2976 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 2977 | if (res != 0) |
| 2978 | return (!async_err) ? posix_error() : NULL; |
| 2979 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2980 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2981 | } |
| 2982 | #endif /* HAVE_FCHMOD */ |
| 2983 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2984 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2985 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2986 | /*[clinic input] |
| 2987 | os.lchmod |
| 2988 | |
| 2989 | path: path_t |
| 2990 | mode: int |
| 2991 | |
| 2992 | Change the access permissions of a file, without following symbolic links. |
| 2993 | |
| 2994 | If path is a symlink, this affects the link itself rather than the target. |
| 2995 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 2996 | [clinic start generated code]*/ |
| 2997 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2998 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2999 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 3000 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3001 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3002 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3003 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3004 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3005 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3006 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3007 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3008 | return NULL; |
| 3009 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3010 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3011 | } |
| 3012 | #endif /* HAVE_LCHMOD */ |
| 3013 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3014 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3015 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3016 | /*[clinic input] |
| 3017 | os.chflags |
| 3018 | |
| 3019 | path: path_t |
| 3020 | flags: unsigned_long(bitwise=True) |
| 3021 | follow_symlinks: bool=True |
| 3022 | |
| 3023 | Set file flags. |
| 3024 | |
| 3025 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3026 | link, chflags will change flags on the symbolic link itself instead of the |
| 3027 | file the link points to. |
| 3028 | follow_symlinks may not be implemented on your platform. If it is |
| 3029 | unavailable, using it will raise a NotImplementedError. |
| 3030 | |
| 3031 | [clinic start generated code]*/ |
| 3032 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3033 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3034 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3035 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3036 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3037 | { |
| 3038 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3039 | |
| 3040 | #ifndef HAVE_LCHFLAGS |
| 3041 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3042 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3043 | #endif |
| 3044 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3045 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3046 | #ifdef HAVE_LCHFLAGS |
| 3047 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3048 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3049 | else |
| 3050 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3051 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3052 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3053 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3054 | if (result) |
| 3055 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3056 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3057 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3058 | } |
| 3059 | #endif /* HAVE_CHFLAGS */ |
| 3060 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3061 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3062 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3063 | /*[clinic input] |
| 3064 | os.lchflags |
| 3065 | |
| 3066 | path: path_t |
| 3067 | flags: unsigned_long(bitwise=True) |
| 3068 | |
| 3069 | Set file flags. |
| 3070 | |
| 3071 | This function will not follow symbolic links. |
| 3072 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3073 | [clinic start generated code]*/ |
| 3074 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3075 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3076 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3077 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3078 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3079 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3080 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3081 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3082 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3083 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3084 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3085 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3086 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3087 | } |
| 3088 | #endif /* HAVE_LCHFLAGS */ |
| 3089 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3090 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3091 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3092 | /*[clinic input] |
| 3093 | os.chroot |
| 3094 | path: path_t |
| 3095 | |
| 3096 | Change root directory to path. |
| 3097 | |
| 3098 | [clinic start generated code]*/ |
| 3099 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3100 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3101 | os_chroot_impl(PyObject *module, path_t *path) |
| 3102 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3103 | { |
| 3104 | int res; |
| 3105 | Py_BEGIN_ALLOW_THREADS |
| 3106 | res = chroot(path->narrow); |
| 3107 | Py_END_ALLOW_THREADS |
| 3108 | if (res < 0) |
| 3109 | return path_error(path); |
| 3110 | Py_RETURN_NONE; |
| 3111 | } |
| 3112 | #endif /* HAVE_CHROOT */ |
| 3113 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3114 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3115 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3116 | /*[clinic input] |
| 3117 | os.fsync |
| 3118 | |
| 3119 | fd: fildes |
| 3120 | |
| 3121 | Force write of fd to disk. |
| 3122 | [clinic start generated code]*/ |
| 3123 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3124 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3125 | os_fsync_impl(PyObject *module, int fd) |
| 3126 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3127 | { |
| 3128 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3129 | } |
| 3130 | #endif /* HAVE_FSYNC */ |
| 3131 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3132 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3133 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3134 | /*[clinic input] |
| 3135 | os.sync |
| 3136 | |
| 3137 | Force write of everything to disk. |
| 3138 | [clinic start generated code]*/ |
| 3139 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3140 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3141 | os_sync_impl(PyObject *module) |
| 3142 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3143 | { |
| 3144 | Py_BEGIN_ALLOW_THREADS |
| 3145 | sync(); |
| 3146 | Py_END_ALLOW_THREADS |
| 3147 | Py_RETURN_NONE; |
| 3148 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3149 | #endif /* HAVE_SYNC */ |
| 3150 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3151 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3152 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3153 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3154 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3155 | #endif |
| 3156 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3157 | /*[clinic input] |
| 3158 | os.fdatasync |
| 3159 | |
| 3160 | fd: fildes |
| 3161 | |
| 3162 | Force write of fd to disk without forcing update of metadata. |
| 3163 | [clinic start generated code]*/ |
| 3164 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3165 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3166 | os_fdatasync_impl(PyObject *module, int fd) |
| 3167 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3168 | { |
| 3169 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3170 | } |
| 3171 | #endif /* HAVE_FDATASYNC */ |
| 3172 | |
| 3173 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3174 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3175 | /*[clinic input] |
| 3176 | os.chown |
| 3177 | |
| 3178 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3179 | 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] | 3180 | |
| 3181 | uid: uid_t |
| 3182 | |
| 3183 | gid: gid_t |
| 3184 | |
| 3185 | * |
| 3186 | |
| 3187 | dir_fd : dir_fd(requires='fchownat') = None |
| 3188 | If not None, it should be a file descriptor open to a directory, |
| 3189 | and path should be relative; path will then be relative to that |
| 3190 | directory. |
| 3191 | |
| 3192 | follow_symlinks: bool = True |
| 3193 | If False, and the last element of the path is a symbolic link, |
| 3194 | stat will examine the symbolic link itself instead of the file |
| 3195 | the link points to. |
| 3196 | |
| 3197 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3198 | |
| 3199 | path may always be specified as a string. |
| 3200 | On some platforms, path may also be specified as an open file descriptor. |
| 3201 | If this functionality is unavailable, using it raises an exception. |
| 3202 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3203 | and path should be relative; path will then be relative to that directory. |
| 3204 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3205 | link, chown will modify the symbolic link itself instead of the file the |
| 3206 | link points to. |
| 3207 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3208 | an open file descriptor. |
| 3209 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3210 | If they are unavailable, using them will raise a NotImplementedError. |
| 3211 | |
| 3212 | [clinic start generated code]*/ |
| 3213 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3214 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3215 | 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] | 3216 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3217 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3218 | { |
| 3219 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3220 | |
| 3221 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3222 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3223 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3224 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3225 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3226 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3227 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3228 | |
| 3229 | #ifdef __APPLE__ |
| 3230 | /* |
| 3231 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3232 | * (But we still have an lchown symbol because of weak-linking.) |
| 3233 | * It doesn't have fchownat either. So there's no possibility |
| 3234 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3235 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3236 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3237 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3238 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3239 | } |
| 3240 | #endif |
| 3241 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3242 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3243 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3244 | if (path->fd != -1) |
| 3245 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3246 | else |
| 3247 | #endif |
| 3248 | #ifdef HAVE_LCHOWN |
| 3249 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3250 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3251 | else |
| 3252 | #endif |
| 3253 | #ifdef HAVE_FCHOWNAT |
| 3254 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3255 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3256 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3257 | else |
| 3258 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3259 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3260 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3261 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3262 | if (result) |
| 3263 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3264 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3265 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3266 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3267 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3268 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3269 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3270 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3271 | /*[clinic input] |
| 3272 | os.fchown |
| 3273 | |
| 3274 | fd: int |
| 3275 | uid: uid_t |
| 3276 | gid: gid_t |
| 3277 | |
| 3278 | Change the owner and group id of the file specified by file descriptor. |
| 3279 | |
| 3280 | Equivalent to os.chown(fd, uid, gid). |
| 3281 | |
| 3282 | [clinic start generated code]*/ |
| 3283 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3284 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3285 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3286 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3287 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3288 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3289 | int async_err = 0; |
| 3290 | |
| 3291 | do { |
| 3292 | Py_BEGIN_ALLOW_THREADS |
| 3293 | res = fchown(fd, uid, gid); |
| 3294 | Py_END_ALLOW_THREADS |
| 3295 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3296 | if (res != 0) |
| 3297 | return (!async_err) ? posix_error() : NULL; |
| 3298 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3299 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3300 | } |
| 3301 | #endif /* HAVE_FCHOWN */ |
| 3302 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3303 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3304 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3305 | /*[clinic input] |
| 3306 | os.lchown |
| 3307 | |
| 3308 | path : path_t |
| 3309 | uid: uid_t |
| 3310 | gid: gid_t |
| 3311 | |
| 3312 | Change the owner and group id of path to the numeric uid and gid. |
| 3313 | |
| 3314 | This function will not follow symbolic links. |
| 3315 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3316 | [clinic start generated code]*/ |
| 3317 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3318 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3319 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3320 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3321 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3322 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3323 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3324 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3325 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3326 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3327 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3328 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3329 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3330 | } |
| 3331 | #endif /* HAVE_LCHOWN */ |
| 3332 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3333 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3334 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3335 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3336 | { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3337 | char *buf, *tmpbuf; |
| 3338 | char *cwd; |
| 3339 | const size_t chunk = 1024; |
| 3340 | size_t buflen = 0; |
| 3341 | PyObject *obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3342 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3343 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3344 | if (!use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3345 | wchar_t wbuf[MAXPATHLEN]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3346 | wchar_t *wbuf2 = wbuf; |
| 3347 | PyObject *resobj; |
| 3348 | DWORD len; |
| 3349 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3350 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3351 | /* If the buffer is large enough, len does not include the |
| 3352 | terminating \0. If the buffer is too small, len includes |
| 3353 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3354 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3355 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3356 | if (wbuf2) |
| 3357 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3358 | } |
| 3359 | Py_END_ALLOW_THREADS |
| 3360 | if (!wbuf2) { |
| 3361 | PyErr_NoMemory(); |
| 3362 | return NULL; |
| 3363 | } |
| 3364 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3365 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3366 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3367 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3368 | } |
| 3369 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3370 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3371 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3372 | return resobj; |
| 3373 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3374 | |
| 3375 | if (win32_warn_bytes_api()) |
| 3376 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3377 | #endif |
| 3378 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3379 | buf = cwd = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3380 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3381 | do { |
| 3382 | buflen += chunk; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3383 | #ifdef MS_WINDOWS |
| 3384 | if (buflen > INT_MAX) { |
| 3385 | PyErr_NoMemory(); |
| 3386 | break; |
| 3387 | } |
| 3388 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3389 | tmpbuf = PyMem_RawRealloc(buf, buflen); |
| 3390 | if (tmpbuf == NULL) |
| 3391 | break; |
| 3392 | |
| 3393 | buf = tmpbuf; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3394 | #ifdef MS_WINDOWS |
| 3395 | cwd = getcwd(buf, (int)buflen); |
| 3396 | #else |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3397 | cwd = getcwd(buf, buflen); |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3398 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3399 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3400 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3401 | |
| 3402 | if (cwd == NULL) { |
| 3403 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3404 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3405 | } |
| 3406 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3407 | if (use_bytes) |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3408 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 3409 | else |
| 3410 | obj = PyUnicode_DecodeFSDefault(buf); |
| 3411 | PyMem_RawFree(buf); |
| 3412 | |
| 3413 | return obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3414 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3415 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3416 | |
| 3417 | /*[clinic input] |
| 3418 | os.getcwd |
| 3419 | |
| 3420 | Return a unicode string representing the current working directory. |
| 3421 | [clinic start generated code]*/ |
| 3422 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3423 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3424 | os_getcwd_impl(PyObject *module) |
| 3425 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3426 | { |
| 3427 | return posix_getcwd(0); |
| 3428 | } |
| 3429 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3430 | |
| 3431 | /*[clinic input] |
| 3432 | os.getcwdb |
| 3433 | |
| 3434 | Return a bytes string representing the current working directory. |
| 3435 | [clinic start generated code]*/ |
| 3436 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3437 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3438 | os_getcwdb_impl(PyObject *module) |
| 3439 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3440 | { |
| 3441 | return posix_getcwd(1); |
| 3442 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3443 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3444 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3445 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3446 | #define HAVE_LINK 1 |
| 3447 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3448 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3449 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3450 | /*[clinic input] |
| 3451 | |
| 3452 | os.link |
| 3453 | |
| 3454 | src : path_t |
| 3455 | dst : path_t |
| 3456 | * |
| 3457 | src_dir_fd : dir_fd = None |
| 3458 | dst_dir_fd : dir_fd = None |
| 3459 | follow_symlinks: bool = True |
| 3460 | |
| 3461 | Create a hard link to a file. |
| 3462 | |
| 3463 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3464 | descriptor open to a directory, and the respective path string (src or dst) |
| 3465 | should be relative; the path will then be relative to that directory. |
| 3466 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3467 | link, link will create a link to the symbolic link itself instead of the |
| 3468 | file the link points to. |
| 3469 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3470 | platform. If they are unavailable, using them will raise a |
| 3471 | NotImplementedError. |
| 3472 | [clinic start generated code]*/ |
| 3473 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3474 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3475 | 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] | 3476 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3477 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3478 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3479 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3480 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3481 | #else |
| 3482 | int result; |
| 3483 | #endif |
| 3484 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3485 | #ifndef HAVE_LINKAT |
| 3486 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3487 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3488 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3489 | } |
| 3490 | #endif |
| 3491 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3492 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3493 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3494 | PyErr_SetString(PyExc_NotImplementedError, |
| 3495 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3496 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3497 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3498 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3499 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3500 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3501 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3502 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3503 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3504 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3505 | if (!result) |
| 3506 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3507 | #else |
| 3508 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3509 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3510 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3511 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3512 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3513 | result = linkat(src_dir_fd, src->narrow, |
| 3514 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3515 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3516 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3517 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3518 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3519 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3520 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3521 | if (result) |
| 3522 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3523 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3524 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3525 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3526 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3527 | #endif |
| 3528 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3529 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3530 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3531 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3532 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3533 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3534 | PyObject *v; |
| 3535 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3536 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3537 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3538 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3539 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3540 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3541 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3542 | WIN32_FIND_DATAW wFileData; |
| 3543 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3544 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3545 | if (!path->wide) { /* Default arg: "." */ |
| 3546 | po_wchars = L"."; |
| 3547 | len = 1; |
| 3548 | } else { |
| 3549 | po_wchars = path->wide; |
| 3550 | len = wcslen(path->wide); |
| 3551 | } |
| 3552 | /* The +5 is so we can append "\\*.*\0" */ |
| 3553 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3554 | if (!wnamebuf) { |
| 3555 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3556 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3557 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3558 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3559 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3560 | wchar_t wch = wnamebuf[len-1]; |
| 3561 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3562 | wnamebuf[len++] = SEP; |
| 3563 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3564 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3565 | if ((list = PyList_New(0)) == NULL) { |
| 3566 | goto exit; |
| 3567 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3568 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3569 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3570 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3571 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3572 | int error = GetLastError(); |
| 3573 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3574 | goto exit; |
| 3575 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3576 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3577 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3578 | } |
| 3579 | do { |
| 3580 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3581 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3582 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3583 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3584 | wcslen(wFileData.cFileName)); |
| 3585 | if (path->narrow && v) { |
| 3586 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3587 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3588 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3589 | Py_DECREF(list); |
| 3590 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3591 | break; |
| 3592 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3593 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3594 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3595 | Py_DECREF(list); |
| 3596 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3597 | break; |
| 3598 | } |
| 3599 | Py_DECREF(v); |
| 3600 | } |
| 3601 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3602 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3603 | Py_END_ALLOW_THREADS |
| 3604 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3605 | it got to the end of the directory. */ |
| 3606 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3607 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3608 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3609 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3610 | } |
| 3611 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3612 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3613 | exit: |
| 3614 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3615 | if (FindClose(hFindFile) == FALSE) { |
| 3616 | if (list != NULL) { |
| 3617 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3618 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3619 | } |
| 3620 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3621 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3622 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3623 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3624 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3625 | } /* end of _listdir_windows_no_opendir */ |
| 3626 | |
| 3627 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3628 | |
| 3629 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3630 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3631 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3632 | PyObject *v; |
| 3633 | DIR *dirp = NULL; |
| 3634 | struct dirent *ep; |
| 3635 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3636 | #ifdef HAVE_FDOPENDIR |
| 3637 | int fd = -1; |
| 3638 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3639 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3640 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3641 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3642 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3643 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3644 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3645 | if (fd == -1) |
| 3646 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3647 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3648 | return_str = 1; |
| 3649 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3650 | Py_BEGIN_ALLOW_THREADS |
| 3651 | dirp = fdopendir(fd); |
| 3652 | Py_END_ALLOW_THREADS |
| 3653 | } |
| 3654 | else |
| 3655 | #endif |
| 3656 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3657 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3658 | if (path->narrow) { |
| 3659 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3660 | /* only return bytes if they specified a bytes-like object */ |
| 3661 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3662 | } |
| 3663 | else { |
| 3664 | name = "."; |
| 3665 | return_str = 1; |
| 3666 | } |
| 3667 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3668 | Py_BEGIN_ALLOW_THREADS |
| 3669 | dirp = opendir(name); |
| 3670 | Py_END_ALLOW_THREADS |
| 3671 | } |
| 3672 | |
| 3673 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3674 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3675 | #ifdef HAVE_FDOPENDIR |
| 3676 | if (fd != -1) { |
| 3677 | Py_BEGIN_ALLOW_THREADS |
| 3678 | close(fd); |
| 3679 | Py_END_ALLOW_THREADS |
| 3680 | } |
| 3681 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3682 | goto exit; |
| 3683 | } |
| 3684 | if ((list = PyList_New(0)) == NULL) { |
| 3685 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3686 | } |
| 3687 | for (;;) { |
| 3688 | errno = 0; |
| 3689 | Py_BEGIN_ALLOW_THREADS |
| 3690 | ep = readdir(dirp); |
| 3691 | Py_END_ALLOW_THREADS |
| 3692 | if (ep == NULL) { |
| 3693 | if (errno == 0) { |
| 3694 | break; |
| 3695 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3696 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3697 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3698 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3699 | } |
| 3700 | } |
| 3701 | if (ep->d_name[0] == '.' && |
| 3702 | (NAMLEN(ep) == 1 || |
| 3703 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3704 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3705 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3706 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3707 | else |
| 3708 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3709 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3710 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3711 | break; |
| 3712 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3713 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3714 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3715 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3716 | break; |
| 3717 | } |
| 3718 | Py_DECREF(v); |
| 3719 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3720 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3721 | exit: |
| 3722 | if (dirp != NULL) { |
| 3723 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3724 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3725 | if (fd > -1) |
| 3726 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3727 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3728 | closedir(dirp); |
| 3729 | Py_END_ALLOW_THREADS |
| 3730 | } |
| 3731 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3732 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3733 | } /* end of _posix_listdir */ |
| 3734 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3735 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3736 | |
| 3737 | /*[clinic input] |
| 3738 | os.listdir |
| 3739 | |
| 3740 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3741 | |
| 3742 | Return a list containing the names of the files in the directory. |
| 3743 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3744 | 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] | 3745 | the filenames returned will also be bytes; in all other circumstances |
| 3746 | the filenames returned will be str. |
| 3747 | If path is None, uses the path='.'. |
| 3748 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3749 | the file descriptor must refer to a directory. |
| 3750 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3751 | |
| 3752 | The list is in arbitrary order. It does not include the special |
| 3753 | entries '.' and '..' even if they are present in the directory. |
| 3754 | |
| 3755 | |
| 3756 | [clinic start generated code]*/ |
| 3757 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3758 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3759 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3760 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3761 | { |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 3762 | if (PySys_Audit("os.listdir", "O", |
| 3763 | path->object ? path->object : Py_None) < 0) { |
| 3764 | return NULL; |
| 3765 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3766 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3767 | return _listdir_windows_no_opendir(path, NULL); |
| 3768 | #else |
| 3769 | return _posix_listdir(path, NULL); |
| 3770 | #endif |
| 3771 | } |
| 3772 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3773 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3774 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3775 | /*[clinic input] |
| 3776 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3777 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3778 | path: path_t |
| 3779 | / |
| 3780 | |
| 3781 | [clinic start generated code]*/ |
| 3782 | |
| 3783 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3784 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 3785 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3786 | { |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame^] | 3787 | wchar_t *abspath; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3788 | |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame^] | 3789 | /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */ |
| 3790 | if (_Py_abspath(path->wide, &abspath) < 0) { |
| 3791 | return win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3792 | } |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame^] | 3793 | if (abspath == NULL) { |
| 3794 | return PyErr_NoMemory(); |
| 3795 | } |
| 3796 | |
| 3797 | PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath)); |
| 3798 | PyMem_RawFree(abspath); |
| 3799 | if (str == NULL) { |
| 3800 | return NULL; |
| 3801 | } |
| 3802 | if (path->narrow) { |
| 3803 | Py_SETREF(str, PyUnicode_EncodeFSDefault(str)); |
| 3804 | } |
| 3805 | return str; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3806 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3807 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3808 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3809 | /*[clinic input] |
| 3810 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3811 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3812 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3813 | / |
| 3814 | |
| 3815 | A helper function for samepath on windows. |
| 3816 | [clinic start generated code]*/ |
| 3817 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3818 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3819 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 3820 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3821 | { |
| 3822 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3823 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 3824 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3825 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3826 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3827 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3828 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3829 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3830 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3831 | 0, /* desired access */ |
| 3832 | 0, /* share mode */ |
| 3833 | NULL, /* security attributes */ |
| 3834 | OPEN_EXISTING, |
| 3835 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3836 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3837 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3838 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3839 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3840 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3841 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3842 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3843 | |
| 3844 | /* We have a good handle to the target, use it to determine the |
| 3845 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3846 | while (1) { |
| 3847 | Py_BEGIN_ALLOW_THREADS |
| 3848 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 3849 | buf_size, VOLUME_NAME_DOS); |
| 3850 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3851 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3852 | if (!result_length) { |
| 3853 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 3854 | path->object); |
| 3855 | goto cleanup; |
| 3856 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3857 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3858 | if (result_length < buf_size) { |
| 3859 | break; |
| 3860 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3861 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3862 | wchar_t *tmp; |
| 3863 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 3864 | result_length * sizeof(*tmp)); |
| 3865 | if (!tmp) { |
| 3866 | result = PyErr_NoMemory(); |
| 3867 | goto cleanup; |
| 3868 | } |
| 3869 | |
| 3870 | buf_size = result_length; |
| 3871 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3872 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3873 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3874 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3875 | if (path->narrow) |
| 3876 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3877 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3878 | cleanup: |
| 3879 | if (target_path != buf) { |
| 3880 | PyMem_Free(target_path); |
| 3881 | } |
| 3882 | CloseHandle(hFile); |
| 3883 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3884 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3885 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3886 | /*[clinic input] |
| 3887 | os._isdir |
| 3888 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3889 | path as arg: object |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3890 | / |
| 3891 | |
Serhiy Storchaka | 579f038 | 2016-11-08 20:21:22 +0200 | [diff] [blame] | 3892 | Return true if the pathname refers to an existing directory. |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3893 | [clinic start generated code]*/ |
| 3894 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3895 | static PyObject * |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3896 | os__isdir(PyObject *module, PyObject *arg) |
| 3897 | /*[clinic end generated code: output=404f334d85d4bf25 input=36cb6785874d479e]*/ |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3898 | { |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3899 | DWORD attributes; |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3900 | path_t path = PATH_T_INITIALIZE("_isdir", "path", 0, 0); |
| 3901 | |
| 3902 | if (!path_converter(arg, &path)) { |
| 3903 | if (PyErr_ExceptionMatches(PyExc_ValueError)) { |
| 3904 | PyErr_Clear(); |
| 3905 | Py_RETURN_FALSE; |
| 3906 | } |
| 3907 | return NULL; |
| 3908 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3909 | |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3910 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3911 | attributes = GetFileAttributesW(path.wide); |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3912 | Py_END_ALLOW_THREADS |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3913 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3914 | path_cleanup(&path); |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3915 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3916 | Py_RETURN_FALSE; |
| 3917 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3918 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3919 | Py_RETURN_TRUE; |
| 3920 | else |
| 3921 | Py_RETURN_FALSE; |
| 3922 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3923 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3924 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3925 | /*[clinic input] |
| 3926 | os._getvolumepathname |
| 3927 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3928 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3929 | |
| 3930 | A helper function for ismount on Win32. |
| 3931 | [clinic start generated code]*/ |
| 3932 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3933 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3934 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 3935 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3936 | { |
| 3937 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3938 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3939 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3940 | BOOL ret; |
| 3941 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3942 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3943 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3944 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 3945 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3946 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 3947 | return NULL; |
| 3948 | } |
| 3949 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3950 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3951 | if (mountpath == NULL) |
| 3952 | return PyErr_NoMemory(); |
| 3953 | |
| 3954 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3955 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3956 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3957 | Py_END_ALLOW_THREADS |
| 3958 | |
| 3959 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3960 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3961 | goto exit; |
| 3962 | } |
| 3963 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3964 | if (path->narrow) |
| 3965 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3966 | |
| 3967 | exit: |
| 3968 | PyMem_Free(mountpath); |
| 3969 | return result; |
| 3970 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3971 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3972 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3973 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3974 | |
| 3975 | /*[clinic input] |
| 3976 | os.mkdir |
| 3977 | |
| 3978 | path : path_t |
| 3979 | |
| 3980 | mode: int = 0o777 |
| 3981 | |
| 3982 | * |
| 3983 | |
| 3984 | dir_fd : dir_fd(requires='mkdirat') = None |
| 3985 | |
| 3986 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3987 | |
| 3988 | Create a directory. |
| 3989 | |
| 3990 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3991 | and path should be relative; path will then be relative to that directory. |
| 3992 | dir_fd may not be implemented on your platform. |
| 3993 | If it is unavailable, using it will raise a NotImplementedError. |
| 3994 | |
| 3995 | The mode argument is ignored on Windows. |
| 3996 | [clinic start generated code]*/ |
| 3997 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3998 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3999 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 4000 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4001 | { |
| 4002 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4003 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4004 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4005 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4006 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4007 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4008 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4009 | if (!result) |
| 4010 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4011 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4012 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4013 | #if HAVE_MKDIRAT |
| 4014 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4015 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4016 | else |
| 4017 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 4018 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4019 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4020 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4021 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4022 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4023 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4024 | if (result < 0) |
| 4025 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4026 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4027 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4030 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4031 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4032 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4033 | #include <sys/resource.h> |
| 4034 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4035 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4036 | |
| 4037 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4038 | /*[clinic input] |
| 4039 | os.nice |
| 4040 | |
| 4041 | increment: int |
| 4042 | / |
| 4043 | |
| 4044 | Add increment to the priority of process and return the new priority. |
| 4045 | [clinic start generated code]*/ |
| 4046 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4047 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4048 | os_nice_impl(PyObject *module, int increment) |
| 4049 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4050 | { |
| 4051 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4052 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4053 | /* There are two flavours of 'nice': one that returns the new |
| 4054 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 4055 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4056 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4057 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4058 | If we are of the nice family that returns the new priority, we |
| 4059 | need to clear errno before the call, and check if errno is filled |
| 4060 | before calling posix_error() on a returnvalue of -1, because the |
| 4061 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4062 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4063 | errno = 0; |
| 4064 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4065 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4066 | if (value == 0) |
| 4067 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4068 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4069 | if (value == -1 && errno != 0) |
| 4070 | /* either nice() or getpriority() returned an error */ |
| 4071 | return posix_error(); |
| 4072 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4073 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4074 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4075 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4076 | |
| 4077 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4078 | /*[clinic input] |
| 4079 | os.getpriority |
| 4080 | |
| 4081 | which: int |
| 4082 | who: int |
| 4083 | |
| 4084 | Return program scheduling priority. |
| 4085 | [clinic start generated code]*/ |
| 4086 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4087 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4088 | os_getpriority_impl(PyObject *module, int which, int who) |
| 4089 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4090 | { |
| 4091 | int retval; |
| 4092 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4093 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4094 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4095 | if (errno != 0) |
| 4096 | return posix_error(); |
| 4097 | return PyLong_FromLong((long)retval); |
| 4098 | } |
| 4099 | #endif /* HAVE_GETPRIORITY */ |
| 4100 | |
| 4101 | |
| 4102 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4103 | /*[clinic input] |
| 4104 | os.setpriority |
| 4105 | |
| 4106 | which: int |
| 4107 | who: int |
| 4108 | priority: int |
| 4109 | |
| 4110 | Set program scheduling priority. |
| 4111 | [clinic start generated code]*/ |
| 4112 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4113 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4114 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4115 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4116 | { |
| 4117 | int retval; |
| 4118 | |
| 4119 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4120 | if (retval == -1) |
| 4121 | return posix_error(); |
| 4122 | Py_RETURN_NONE; |
| 4123 | } |
| 4124 | #endif /* HAVE_SETPRIORITY */ |
| 4125 | |
| 4126 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4127 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4128 | 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] | 4129 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4130 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4131 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4132 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4133 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4134 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4135 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4136 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4137 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4138 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4139 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4140 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4141 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4142 | #ifndef HAVE_RENAMEAT |
| 4143 | if (dir_fd_specified) { |
| 4144 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4145 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4146 | } |
| 4147 | #endif |
| 4148 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4149 | #ifdef MS_WINDOWS |
| 4150 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4151 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4152 | Py_END_ALLOW_THREADS |
| 4153 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4154 | if (!result) |
| 4155 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4156 | |
| 4157 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4158 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4159 | PyErr_Format(PyExc_ValueError, |
| 4160 | "%s: src and dst must be the same type", function_name); |
| 4161 | return NULL; |
| 4162 | } |
| 4163 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4164 | Py_BEGIN_ALLOW_THREADS |
| 4165 | #ifdef HAVE_RENAMEAT |
| 4166 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4167 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4168 | else |
| 4169 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4170 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4171 | Py_END_ALLOW_THREADS |
| 4172 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4173 | if (result) |
| 4174 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4175 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4176 | Py_RETURN_NONE; |
| 4177 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4178 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4179 | |
| 4180 | /*[clinic input] |
| 4181 | os.rename |
| 4182 | |
| 4183 | src : path_t |
| 4184 | dst : path_t |
| 4185 | * |
| 4186 | src_dir_fd : dir_fd = None |
| 4187 | dst_dir_fd : dir_fd = None |
| 4188 | |
| 4189 | Rename a file or directory. |
| 4190 | |
| 4191 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4192 | descriptor open to a directory, and the respective path string (src or dst) |
| 4193 | should be relative; the path will then be relative to that directory. |
| 4194 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4195 | If they are unavailable, using them will raise a NotImplementedError. |
| 4196 | [clinic start generated code]*/ |
| 4197 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4198 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4199 | 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] | 4200 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4201 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4202 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4203 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4204 | } |
| 4205 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4206 | |
| 4207 | /*[clinic input] |
| 4208 | os.replace = os.rename |
| 4209 | |
| 4210 | Rename a file or directory, overwriting the destination. |
| 4211 | |
| 4212 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4213 | descriptor open to a directory, and the respective path string (src or dst) |
| 4214 | should be relative; the path will then be relative to that directory. |
| 4215 | 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] | 4216 | If they are unavailable, using them will raise a NotImplementedError. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4217 | [clinic start generated code]*/ |
| 4218 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4219 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4220 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4221 | int dst_dir_fd) |
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4222 | /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4223 | { |
| 4224 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4225 | } |
| 4226 | |
| 4227 | |
| 4228 | /*[clinic input] |
| 4229 | os.rmdir |
| 4230 | |
| 4231 | path: path_t |
| 4232 | * |
| 4233 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4234 | |
| 4235 | Remove a directory. |
| 4236 | |
| 4237 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4238 | and path should be relative; path will then be relative to that directory. |
| 4239 | dir_fd may not be implemented on your platform. |
| 4240 | If it is unavailable, using it will raise a NotImplementedError. |
| 4241 | [clinic start generated code]*/ |
| 4242 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4243 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4244 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4245 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4246 | { |
| 4247 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4248 | |
| 4249 | Py_BEGIN_ALLOW_THREADS |
| 4250 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4251 | /* Windows, success=1, UNIX, success=0 */ |
| 4252 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4253 | #else |
| 4254 | #ifdef HAVE_UNLINKAT |
| 4255 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4256 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4257 | else |
| 4258 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4259 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4260 | #endif |
| 4261 | Py_END_ALLOW_THREADS |
| 4262 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4263 | if (result) |
| 4264 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4265 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4266 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4269 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4270 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4271 | #ifdef MS_WINDOWS |
| 4272 | /*[clinic input] |
| 4273 | os.system -> long |
| 4274 | |
| 4275 | command: Py_UNICODE |
| 4276 | |
| 4277 | Execute the command in a subshell. |
| 4278 | [clinic start generated code]*/ |
| 4279 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4280 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4281 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4282 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4283 | { |
| 4284 | long result; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4285 | |
| 4286 | if (PySys_Audit("system", "(u)", command) < 0) { |
| 4287 | return -1; |
| 4288 | } |
| 4289 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4290 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4291 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4292 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4293 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4294 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4295 | return result; |
| 4296 | } |
| 4297 | #else /* MS_WINDOWS */ |
| 4298 | /*[clinic input] |
| 4299 | os.system -> long |
| 4300 | |
| 4301 | command: FSConverter |
| 4302 | |
| 4303 | Execute the command in a subshell. |
| 4304 | [clinic start generated code]*/ |
| 4305 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4306 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4307 | os_system_impl(PyObject *module, PyObject *command) |
| 4308 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4309 | { |
| 4310 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4311 | const char *bytes = PyBytes_AsString(command); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4312 | |
| 4313 | if (PySys_Audit("system", "(O)", command) < 0) { |
| 4314 | return -1; |
| 4315 | } |
| 4316 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4317 | Py_BEGIN_ALLOW_THREADS |
| 4318 | result = system(bytes); |
| 4319 | Py_END_ALLOW_THREADS |
| 4320 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4321 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4322 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4323 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4324 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4325 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4326 | /*[clinic input] |
| 4327 | os.umask |
| 4328 | |
| 4329 | mask: int |
| 4330 | / |
| 4331 | |
| 4332 | Set the current numeric umask and return the previous umask. |
| 4333 | [clinic start generated code]*/ |
| 4334 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4335 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4336 | os_umask_impl(PyObject *module, int mask) |
| 4337 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4338 | { |
| 4339 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4340 | if (i < 0) |
| 4341 | return posix_error(); |
| 4342 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4343 | } |
| 4344 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4345 | #ifdef MS_WINDOWS |
| 4346 | |
| 4347 | /* override the default DeleteFileW behavior so that directory |
| 4348 | symlinks can be removed with this function, the same as with |
| 4349 | Unix symlinks */ |
| 4350 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4351 | { |
| 4352 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4353 | WIN32_FIND_DATAW find_data; |
| 4354 | HANDLE find_data_handle; |
| 4355 | int is_directory = 0; |
| 4356 | int is_link = 0; |
| 4357 | |
| 4358 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4359 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4360 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4361 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4362 | it is a symlink */ |
| 4363 | if(is_directory && |
| 4364 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4365 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4366 | |
| 4367 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4368 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4369 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4370 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4371 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4372 | FindClose(find_data_handle); |
| 4373 | } |
| 4374 | } |
| 4375 | } |
| 4376 | |
| 4377 | if (is_directory && is_link) |
| 4378 | return RemoveDirectoryW(lpFileName); |
| 4379 | |
| 4380 | return DeleteFileW(lpFileName); |
| 4381 | } |
| 4382 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4383 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4384 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4385 | /*[clinic input] |
| 4386 | os.unlink |
| 4387 | |
| 4388 | path: path_t |
| 4389 | * |
| 4390 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4391 | |
| 4392 | Remove a file (same as remove()). |
| 4393 | |
| 4394 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4395 | and path should be relative; path will then be relative to that directory. |
| 4396 | dir_fd may not be implemented on your platform. |
| 4397 | If it is unavailable, using it will raise a NotImplementedError. |
| 4398 | |
| 4399 | [clinic start generated code]*/ |
| 4400 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4401 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4402 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4403 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4404 | { |
| 4405 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4406 | |
| 4407 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4408 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4409 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4410 | /* Windows, success=1, UNIX, success=0 */ |
| 4411 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4412 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4413 | #ifdef HAVE_UNLINKAT |
| 4414 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4415 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4416 | else |
| 4417 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4418 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4419 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4420 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4421 | Py_END_ALLOW_THREADS |
| 4422 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4423 | if (result) |
| 4424 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4425 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4426 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4429 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4430 | /*[clinic input] |
| 4431 | os.remove = os.unlink |
| 4432 | |
| 4433 | Remove a file (same as unlink()). |
| 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 | [clinic start generated code]*/ |
| 4440 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4441 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4442 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4443 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4444 | { |
| 4445 | return os_unlink_impl(module, path, dir_fd); |
| 4446 | } |
| 4447 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4448 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4449 | static PyStructSequence_Field uname_result_fields[] = { |
| 4450 | {"sysname", "operating system name"}, |
| 4451 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4452 | {"release", "operating system release"}, |
| 4453 | {"version", "operating system version"}, |
| 4454 | {"machine", "hardware identifier"}, |
| 4455 | {NULL} |
| 4456 | }; |
| 4457 | |
| 4458 | PyDoc_STRVAR(uname_result__doc__, |
| 4459 | "uname_result: Result from os.uname().\n\n\ |
| 4460 | This object may be accessed either as a tuple of\n\ |
| 4461 | (sysname, nodename, release, version, machine),\n\ |
| 4462 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4463 | \n\ |
| 4464 | See os.uname for more information."); |
| 4465 | |
| 4466 | static PyStructSequence_Desc uname_result_desc = { |
| 4467 | "uname_result", /* name */ |
| 4468 | uname_result__doc__, /* doc */ |
| 4469 | uname_result_fields, |
| 4470 | 5 |
| 4471 | }; |
| 4472 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 4473 | static PyTypeObject* UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4474 | |
| 4475 | |
| 4476 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4477 | /*[clinic input] |
| 4478 | os.uname |
| 4479 | |
| 4480 | Return an object identifying the current operating system. |
| 4481 | |
| 4482 | The object behaves like a named tuple with the following fields: |
| 4483 | (sysname, nodename, release, version, machine) |
| 4484 | |
| 4485 | [clinic start generated code]*/ |
| 4486 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4487 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4488 | os_uname_impl(PyObject *module) |
| 4489 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4490 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4491 | struct utsname u; |
| 4492 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4493 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4494 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4495 | Py_BEGIN_ALLOW_THREADS |
| 4496 | res = uname(&u); |
| 4497 | Py_END_ALLOW_THREADS |
| 4498 | if (res < 0) |
| 4499 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4500 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 4501 | value = PyStructSequence_New(UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4502 | if (value == NULL) |
| 4503 | return NULL; |
| 4504 | |
| 4505 | #define SET(i, field) \ |
| 4506 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4507 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4508 | if (!o) { \ |
| 4509 | Py_DECREF(value); \ |
| 4510 | return NULL; \ |
| 4511 | } \ |
| 4512 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4513 | } \ |
| 4514 | |
| 4515 | SET(0, u.sysname); |
| 4516 | SET(1, u.nodename); |
| 4517 | SET(2, u.release); |
| 4518 | SET(3, u.version); |
| 4519 | SET(4, u.machine); |
| 4520 | |
| 4521 | #undef SET |
| 4522 | |
| 4523 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4524 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4525 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4526 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4527 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4528 | |
| 4529 | typedef struct { |
| 4530 | int now; |
| 4531 | time_t atime_s; |
| 4532 | long atime_ns; |
| 4533 | time_t mtime_s; |
| 4534 | long mtime_ns; |
| 4535 | } utime_t; |
| 4536 | |
| 4537 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4538 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4539 | * they also intentionally leak the declaration of a pointer named "time" |
| 4540 | */ |
| 4541 | #define UTIME_TO_TIMESPEC \ |
| 4542 | struct timespec ts[2]; \ |
| 4543 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4544 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4545 | time = NULL; \ |
| 4546 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4547 | ts[0].tv_sec = ut->atime_s; \ |
| 4548 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4549 | ts[1].tv_sec = ut->mtime_s; \ |
| 4550 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4551 | time = ts; \ |
| 4552 | } \ |
| 4553 | |
| 4554 | #define UTIME_TO_TIMEVAL \ |
| 4555 | struct timeval tv[2]; \ |
| 4556 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4557 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4558 | time = NULL; \ |
| 4559 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4560 | tv[0].tv_sec = ut->atime_s; \ |
| 4561 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4562 | tv[1].tv_sec = ut->mtime_s; \ |
| 4563 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4564 | time = tv; \ |
| 4565 | } \ |
| 4566 | |
| 4567 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4568 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4569 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4570 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4571 | time = NULL; \ |
| 4572 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4573 | u.actime = ut->atime_s; \ |
| 4574 | u.modtime = ut->mtime_s; \ |
| 4575 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | #define UTIME_TO_TIME_T \ |
| 4579 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4580 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4581 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4582 | time = NULL; \ |
| 4583 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4584 | timet[0] = ut->atime_s; \ |
| 4585 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4586 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4587 | } \ |
| 4588 | |
| 4589 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4590 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4591 | |
| 4592 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4593 | 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] | 4594 | { |
| 4595 | #ifdef HAVE_UTIMENSAT |
| 4596 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4597 | UTIME_TO_TIMESPEC; |
| 4598 | return utimensat(dir_fd, path, time, flags); |
| 4599 | #elif defined(HAVE_FUTIMESAT) |
| 4600 | UTIME_TO_TIMEVAL; |
| 4601 | /* |
| 4602 | * follow_symlinks will never be false here; |
| 4603 | * we only allow !follow_symlinks and dir_fd together |
| 4604 | * if we have utimensat() |
| 4605 | */ |
| 4606 | assert(follow_symlinks); |
| 4607 | return futimesat(dir_fd, path, time); |
| 4608 | #endif |
| 4609 | } |
| 4610 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4611 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4612 | #else |
| 4613 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4614 | #endif |
| 4615 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4616 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4617 | |
| 4618 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4619 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4620 | { |
| 4621 | #ifdef HAVE_FUTIMENS |
| 4622 | UTIME_TO_TIMESPEC; |
| 4623 | return futimens(fd, time); |
| 4624 | #else |
| 4625 | UTIME_TO_TIMEVAL; |
| 4626 | return futimes(fd, time); |
| 4627 | #endif |
| 4628 | } |
| 4629 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4630 | #define PATH_UTIME_HAVE_FD 1 |
| 4631 | #else |
| 4632 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4633 | #endif |
| 4634 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4635 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4636 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4637 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4638 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4639 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4640 | |
| 4641 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4642 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4643 | { |
| 4644 | #ifdef HAVE_UTIMENSAT |
| 4645 | UTIME_TO_TIMESPEC; |
| 4646 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4647 | #else |
| 4648 | UTIME_TO_TIMEVAL; |
| 4649 | return lutimes(path, time); |
| 4650 | #endif |
| 4651 | } |
| 4652 | |
| 4653 | #endif |
| 4654 | |
| 4655 | #ifndef MS_WINDOWS |
| 4656 | |
| 4657 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4658 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4659 | { |
| 4660 | #ifdef HAVE_UTIMENSAT |
| 4661 | UTIME_TO_TIMESPEC; |
| 4662 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4663 | #elif defined(HAVE_UTIMES) |
| 4664 | UTIME_TO_TIMEVAL; |
| 4665 | return utimes(path, time); |
| 4666 | #elif defined(HAVE_UTIME_H) |
| 4667 | UTIME_TO_UTIMBUF; |
| 4668 | return utime(path, time); |
| 4669 | #else |
| 4670 | UTIME_TO_TIME_T; |
| 4671 | return utime(path, time); |
| 4672 | #endif |
| 4673 | } |
| 4674 | |
| 4675 | #endif |
| 4676 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4677 | static int |
| 4678 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4679 | { |
| 4680 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4681 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4682 | divmod = PyNumber_Divmod(py_long, billion); |
| 4683 | if (!divmod) |
| 4684 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4685 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 4686 | PyErr_Format(PyExc_TypeError, |
| 4687 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
| 4688 | Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name); |
| 4689 | goto exit; |
| 4690 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4691 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4692 | if ((*s == -1) && PyErr_Occurred()) |
| 4693 | goto exit; |
| 4694 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4695 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4696 | goto exit; |
| 4697 | |
| 4698 | result = 1; |
| 4699 | exit: |
| 4700 | Py_XDECREF(divmod); |
| 4701 | return result; |
| 4702 | } |
| 4703 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4704 | |
| 4705 | /*[clinic input] |
| 4706 | os.utime |
| 4707 | |
| 4708 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
| 4709 | times: object = NULL |
| 4710 | * |
| 4711 | ns: object = NULL |
| 4712 | dir_fd: dir_fd(requires='futimensat') = None |
| 4713 | follow_symlinks: bool=True |
| 4714 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4715 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4716 | |
| 4717 | Set the access and modified time of path. |
| 4718 | |
| 4719 | path may always be specified as a string. |
| 4720 | On some platforms, path may also be specified as an open file descriptor. |
| 4721 | If this functionality is unavailable, using it raises an exception. |
| 4722 | |
| 4723 | If times is not None, it must be a tuple (atime, mtime); |
| 4724 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4725 | 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] | 4726 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4727 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4728 | 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] | 4729 | Specifying tuples for both times and ns is an error. |
| 4730 | |
| 4731 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4732 | and path should be relative; path will then be relative to that directory. |
| 4733 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4734 | link, utime will modify the symbolic link itself instead of the file the |
| 4735 | link points to. |
| 4736 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4737 | as an open file descriptor. |
| 4738 | dir_fd and follow_symlinks may not be available on your platform. |
| 4739 | If they are unavailable, using them will raise a NotImplementedError. |
| 4740 | |
| 4741 | [clinic start generated code]*/ |
| 4742 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4743 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4744 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4745 | int dir_fd, int follow_symlinks) |
| 4746 | /*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4747 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4748 | #ifdef MS_WINDOWS |
| 4749 | HANDLE hFile; |
| 4750 | FILETIME atime, mtime; |
| 4751 | #else |
| 4752 | int result; |
| 4753 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4754 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4755 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4756 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4757 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4758 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4759 | if (times && (times != Py_None) && ns) { |
| 4760 | PyErr_SetString(PyExc_ValueError, |
| 4761 | "utime: you may specify either 'times'" |
| 4762 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4763 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4764 | } |
| 4765 | |
| 4766 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4767 | time_t a_sec, m_sec; |
| 4768 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4769 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4770 | PyErr_SetString(PyExc_TypeError, |
| 4771 | "utime: 'times' must be either" |
| 4772 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4773 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4774 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4775 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4776 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4777 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4778 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4779 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4780 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4781 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4782 | utime.atime_s = a_sec; |
| 4783 | utime.atime_ns = a_nsec; |
| 4784 | utime.mtime_s = m_sec; |
| 4785 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4786 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4787 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4788 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4789 | PyErr_SetString(PyExc_TypeError, |
| 4790 | "utime: 'ns' must be a tuple of two ints"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4791 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4792 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4793 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4794 | 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] | 4795 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4796 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4797 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4798 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4799 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4800 | } |
| 4801 | else { |
| 4802 | /* times and ns are both None/unspecified. use "now". */ |
| 4803 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4804 | } |
| 4805 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4806 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4807 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4808 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4809 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4810 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4811 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4812 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4813 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
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 | #if !defined(HAVE_UTIMENSAT) |
| 4817 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4818 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4819 | "utime: cannot use dir_fd and follow_symlinks " |
| 4820 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4821 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4822 | } |
| 4823 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4824 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4825 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4826 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4827 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 4828 | NULL, OPEN_EXISTING, |
| 4829 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4830 | Py_END_ALLOW_THREADS |
| 4831 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4832 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4833 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4834 | } |
| 4835 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4836 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4837 | GetSystemTimeAsFileTime(&mtime); |
| 4838 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4839 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4840 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4841 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4842 | _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] | 4843 | } |
| 4844 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4845 | /* Avoid putting the file name into the error here, |
| 4846 | as that may confuse the user into believing that |
| 4847 | something is wrong with the file, when it also |
| 4848 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4849 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4850 | CloseHandle(hFile); |
| 4851 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4852 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4853 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4854 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4855 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4856 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4857 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4858 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4859 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4860 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4861 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4862 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4863 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4864 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4865 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4866 | else |
| 4867 | #endif |
| 4868 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4869 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4870 | if (path->fd != -1) |
| 4871 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4872 | else |
| 4873 | #endif |
| 4874 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4875 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4876 | |
| 4877 | Py_END_ALLOW_THREADS |
| 4878 | |
| 4879 | if (result < 0) { |
| 4880 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4881 | posix_error(); |
| 4882 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4883 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4884 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4885 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4886 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4887 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4888 | } |
| 4889 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4890 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4891 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4892 | |
| 4893 | /*[clinic input] |
| 4894 | os._exit |
| 4895 | |
| 4896 | status: int |
| 4897 | |
| 4898 | Exit to the system with specified status, without normal exit processing. |
| 4899 | [clinic start generated code]*/ |
| 4900 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4901 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4902 | os__exit_impl(PyObject *module, int status) |
| 4903 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4904 | { |
| 4905 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4906 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4909 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4910 | #define EXECV_CHAR wchar_t |
| 4911 | #else |
| 4912 | #define EXECV_CHAR char |
| 4913 | #endif |
| 4914 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 4915 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4916 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4917 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4918 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4919 | Py_ssize_t i; |
| 4920 | for (i = 0; i < count; i++) |
| 4921 | PyMem_Free(array[i]); |
| 4922 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4923 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4924 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4925 | static int |
| 4926 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4927 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4928 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4929 | PyObject *ub; |
| 4930 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4931 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4932 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4933 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4934 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 4935 | if (*out) |
| 4936 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4937 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4938 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4939 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4940 | size = PyBytes_GET_SIZE(ub); |
| 4941 | *out = PyMem_Malloc(size + 1); |
| 4942 | if (*out) { |
| 4943 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 4944 | result = 1; |
| 4945 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4946 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4947 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4948 | Py_DECREF(ub); |
| 4949 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4950 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4951 | #endif |
| 4952 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 4953 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4954 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4955 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4956 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4957 | Py_ssize_t i, pos, envc; |
| 4958 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4959 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4960 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4961 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4962 | i = PyMapping_Size(env); |
| 4963 | if (i < 0) |
| 4964 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4965 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4966 | if (envlist == NULL) { |
| 4967 | PyErr_NoMemory(); |
| 4968 | return NULL; |
| 4969 | } |
| 4970 | envc = 0; |
| 4971 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4972 | if (!keys) |
| 4973 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4974 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4975 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4976 | goto error; |
| 4977 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4978 | PyErr_Format(PyExc_TypeError, |
| 4979 | "env.keys() or env.values() is not a list"); |
| 4980 | goto error; |
| 4981 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4982 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4983 | for (pos = 0; pos < i; pos++) { |
| 4984 | key = PyList_GetItem(keys, pos); |
| 4985 | val = PyList_GetItem(vals, pos); |
| 4986 | if (!key || !val) |
| 4987 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4988 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4989 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4990 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 4991 | goto error; |
| 4992 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 4993 | Py_DECREF(key2); |
| 4994 | goto error; |
| 4995 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4996 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 4997 | defining hidden environment variables. */ |
| 4998 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 4999 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 5000 | { |
| 5001 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5002 | Py_DECREF(key2); |
| 5003 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5004 | goto error; |
| 5005 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5006 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 5007 | #else |
| 5008 | if (!PyUnicode_FSConverter(key, &key2)) |
| 5009 | goto error; |
| 5010 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 5011 | Py_DECREF(key2); |
| 5012 | goto error; |
| 5013 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5014 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 5015 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 5016 | { |
| 5017 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5018 | Py_DECREF(key2); |
| 5019 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5020 | goto error; |
| 5021 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5022 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 5023 | PyBytes_AS_STRING(val2)); |
| 5024 | #endif |
| 5025 | Py_DECREF(key2); |
| 5026 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5027 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5028 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5029 | |
| 5030 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 5031 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5032 | goto error; |
| 5033 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5034 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5035 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5036 | } |
| 5037 | Py_DECREF(vals); |
| 5038 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5039 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5040 | envlist[envc] = 0; |
| 5041 | *envc_ptr = envc; |
| 5042 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5043 | |
| 5044 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5045 | Py_XDECREF(keys); |
| 5046 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5047 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5048 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5049 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5050 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5051 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5052 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5053 | { |
| 5054 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5055 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5056 | if (argvlist == NULL) { |
| 5057 | PyErr_NoMemory(); |
| 5058 | return NULL; |
| 5059 | } |
| 5060 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5061 | PyObject* item = PySequence_ITEM(argv, i); |
| 5062 | if (item == NULL) |
| 5063 | goto fail; |
| 5064 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5065 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5066 | goto fail; |
| 5067 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5068 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5069 | } |
| 5070 | argvlist[*argc] = NULL; |
| 5071 | return argvlist; |
| 5072 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5073 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5074 | free_string_array(argvlist, *argc); |
| 5075 | return NULL; |
| 5076 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5077 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5078 | #endif |
| 5079 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5080 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5081 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5082 | /*[clinic input] |
| 5083 | os.execv |
| 5084 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5085 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5086 | Path of executable file. |
| 5087 | argv: object |
| 5088 | Tuple or list of strings. |
| 5089 | / |
| 5090 | |
| 5091 | Execute an executable path with arguments, replacing current process. |
| 5092 | [clinic start generated code]*/ |
| 5093 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5094 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5095 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5096 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5097 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5098 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5099 | Py_ssize_t argc; |
| 5100 | |
| 5101 | /* execv has two arguments: (path, argv), where |
| 5102 | argv is a list or tuple of strings. */ |
| 5103 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5104 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5105 | PyErr_SetString(PyExc_TypeError, |
| 5106 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5107 | return NULL; |
| 5108 | } |
| 5109 | argc = PySequence_Size(argv); |
| 5110 | if (argc < 1) { |
| 5111 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5112 | return NULL; |
| 5113 | } |
| 5114 | |
| 5115 | argvlist = parse_arglist(argv, &argc); |
| 5116 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5117 | return NULL; |
| 5118 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5119 | if (!argvlist[0][0]) { |
| 5120 | PyErr_SetString(PyExc_ValueError, |
| 5121 | "execv() arg 2 first element cannot be empty"); |
| 5122 | free_string_array(argvlist, argc); |
| 5123 | return NULL; |
| 5124 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5125 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5126 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5127 | #ifdef HAVE_WEXECV |
| 5128 | _wexecv(path->wide, argvlist); |
| 5129 | #else |
| 5130 | execv(path->narrow, argvlist); |
| 5131 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5132 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5133 | |
| 5134 | /* If we get here it's definitely an error */ |
| 5135 | |
| 5136 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5137 | return posix_error(); |
| 5138 | } |
| 5139 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5140 | |
| 5141 | /*[clinic input] |
| 5142 | os.execve |
| 5143 | |
| 5144 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5145 | Path of executable file. |
| 5146 | argv: object |
| 5147 | Tuple or list of strings. |
| 5148 | env: object |
| 5149 | Dictionary of strings mapping to strings. |
| 5150 | |
| 5151 | Execute an executable path with arguments, replacing current process. |
| 5152 | [clinic start generated code]*/ |
| 5153 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5154 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5155 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5156 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5157 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5158 | EXECV_CHAR **argvlist = NULL; |
| 5159 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5160 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5161 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5162 | /* execve has three arguments: (path, argv, env), where |
| 5163 | argv is a list or tuple of strings and env is a dictionary |
| 5164 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5165 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5166 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5167 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5168 | "execve: argv must be a tuple or list"); |
| 5169 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5170 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5171 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5172 | if (argc < 1) { |
| 5173 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5174 | return NULL; |
| 5175 | } |
| 5176 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5177 | if (!PyMapping_Check(env)) { |
| 5178 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5179 | "execve: environment must be a mapping object"); |
| 5180 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5181 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5182 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5183 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5184 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5185 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5186 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5187 | if (!argvlist[0][0]) { |
| 5188 | PyErr_SetString(PyExc_ValueError, |
| 5189 | "execve: argv first element cannot be empty"); |
| 5190 | goto fail; |
| 5191 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5192 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5193 | envlist = parse_envlist(env, &envc); |
| 5194 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5195 | goto fail; |
| 5196 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5197 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5198 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5199 | if (path->fd > -1) |
| 5200 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5201 | else |
| 5202 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5203 | #ifdef HAVE_WEXECV |
| 5204 | _wexecve(path->wide, argvlist, envlist); |
| 5205 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5206 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5207 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5208 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5209 | |
| 5210 | /* If we get here it's definitely an error */ |
| 5211 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5212 | posix_path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5213 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5214 | free_string_array(envlist, envc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5215 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5216 | if (argvlist) |
| 5217 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5218 | return NULL; |
| 5219 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5220 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5221 | #endif /* HAVE_EXECV */ |
| 5222 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5223 | #ifdef HAVE_POSIX_SPAWN |
| 5224 | |
| 5225 | enum posix_spawn_file_actions_identifier { |
| 5226 | POSIX_SPAWN_OPEN, |
| 5227 | POSIX_SPAWN_CLOSE, |
| 5228 | POSIX_SPAWN_DUP2 |
| 5229 | }; |
| 5230 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5231 | #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] | 5232 | static int |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5233 | convert_sched_param(PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5234 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5235 | |
| 5236 | static int |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5237 | parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup, |
| 5238 | int resetids, int setsid, PyObject *setsigmask, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5239 | PyObject *setsigdef, PyObject *scheduler, |
| 5240 | posix_spawnattr_t *attrp) |
| 5241 | { |
| 5242 | long all_flags = 0; |
| 5243 | |
| 5244 | errno = posix_spawnattr_init(attrp); |
| 5245 | if (errno) { |
| 5246 | posix_error(); |
| 5247 | return -1; |
| 5248 | } |
| 5249 | |
| 5250 | if (setpgroup) { |
| 5251 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5252 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5253 | goto fail; |
| 5254 | } |
| 5255 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5256 | if (errno) { |
| 5257 | posix_error(); |
| 5258 | goto fail; |
| 5259 | } |
| 5260 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5261 | } |
| 5262 | |
| 5263 | if (resetids) { |
| 5264 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5265 | } |
| 5266 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5267 | if (setsid) { |
| 5268 | #ifdef POSIX_SPAWN_SETSID |
| 5269 | all_flags |= POSIX_SPAWN_SETSID; |
| 5270 | #elif defined(POSIX_SPAWN_SETSID_NP) |
| 5271 | all_flags |= POSIX_SPAWN_SETSID_NP; |
| 5272 | #else |
| 5273 | argument_unavailable_error(func_name, "setsid"); |
| 5274 | return -1; |
| 5275 | #endif |
| 5276 | } |
| 5277 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5278 | if (setsigmask) { |
| 5279 | sigset_t set; |
| 5280 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5281 | goto fail; |
| 5282 | } |
| 5283 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5284 | if (errno) { |
| 5285 | posix_error(); |
| 5286 | goto fail; |
| 5287 | } |
| 5288 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5289 | } |
| 5290 | |
| 5291 | if (setsigdef) { |
| 5292 | sigset_t set; |
| 5293 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5294 | goto fail; |
| 5295 | } |
| 5296 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5297 | if (errno) { |
| 5298 | posix_error(); |
| 5299 | goto fail; |
| 5300 | } |
| 5301 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5302 | } |
| 5303 | |
| 5304 | if (scheduler) { |
| 5305 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5306 | PyObject *py_schedpolicy; |
| 5307 | struct sched_param schedparam; |
| 5308 | |
| 5309 | if (!PyArg_ParseTuple(scheduler, "OO&" |
| 5310 | ";A scheduler tuple must have two elements", |
| 5311 | &py_schedpolicy, convert_sched_param, &schedparam)) { |
| 5312 | goto fail; |
| 5313 | } |
| 5314 | if (py_schedpolicy != Py_None) { |
| 5315 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5316 | |
| 5317 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5318 | goto fail; |
| 5319 | } |
| 5320 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5321 | if (errno) { |
| 5322 | posix_error(); |
| 5323 | goto fail; |
| 5324 | } |
| 5325 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5326 | } |
| 5327 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5328 | if (errno) { |
| 5329 | posix_error(); |
| 5330 | goto fail; |
| 5331 | } |
| 5332 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5333 | #else |
| 5334 | PyErr_SetString(PyExc_NotImplementedError, |
| 5335 | "The scheduler option is not supported in this system."); |
| 5336 | goto fail; |
| 5337 | #endif |
| 5338 | } |
| 5339 | |
| 5340 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5341 | if (errno) { |
| 5342 | posix_error(); |
| 5343 | goto fail; |
| 5344 | } |
| 5345 | |
| 5346 | return 0; |
| 5347 | |
| 5348 | fail: |
| 5349 | (void)posix_spawnattr_destroy(attrp); |
| 5350 | return -1; |
| 5351 | } |
| 5352 | |
| 5353 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5354 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5355 | posix_spawn_file_actions_t *file_actionsp, |
| 5356 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5357 | { |
| 5358 | PyObject *seq; |
| 5359 | PyObject *file_action = NULL; |
| 5360 | PyObject *tag_obj; |
| 5361 | |
| 5362 | seq = PySequence_Fast(file_actions, |
| 5363 | "file_actions must be a sequence or None"); |
| 5364 | if (seq == NULL) { |
| 5365 | return -1; |
| 5366 | } |
| 5367 | |
| 5368 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5369 | if (errno) { |
| 5370 | posix_error(); |
| 5371 | Py_DECREF(seq); |
| 5372 | return -1; |
| 5373 | } |
| 5374 | |
| 5375 | for (int i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) { |
| 5376 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5377 | Py_INCREF(file_action); |
| 5378 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5379 | PyErr_SetString(PyExc_TypeError, |
| 5380 | "Each file_actions element must be a non-empty tuple"); |
| 5381 | goto fail; |
| 5382 | } |
| 5383 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5384 | if (tag == -1 && PyErr_Occurred()) { |
| 5385 | goto fail; |
| 5386 | } |
| 5387 | |
| 5388 | /* Populate the file_actions object */ |
| 5389 | switch (tag) { |
| 5390 | case POSIX_SPAWN_OPEN: { |
| 5391 | int fd, oflag; |
| 5392 | PyObject *path; |
| 5393 | unsigned long mode; |
| 5394 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5395 | ";A open file_action tuple must have 5 elements", |
| 5396 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5397 | &oflag, &mode)) |
| 5398 | { |
| 5399 | goto fail; |
| 5400 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5401 | if (PyList_Append(temp_buffer, path)) { |
| 5402 | Py_DECREF(path); |
| 5403 | goto fail; |
| 5404 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5405 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5406 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5407 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5408 | if (errno) { |
| 5409 | posix_error(); |
| 5410 | goto fail; |
| 5411 | } |
| 5412 | break; |
| 5413 | } |
| 5414 | case POSIX_SPAWN_CLOSE: { |
| 5415 | int fd; |
| 5416 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5417 | ";A close file_action tuple must have 2 elements", |
| 5418 | &tag_obj, &fd)) |
| 5419 | { |
| 5420 | goto fail; |
| 5421 | } |
| 5422 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 5423 | if (errno) { |
| 5424 | posix_error(); |
| 5425 | goto fail; |
| 5426 | } |
| 5427 | break; |
| 5428 | } |
| 5429 | case POSIX_SPAWN_DUP2: { |
| 5430 | int fd1, fd2; |
| 5431 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 5432 | ";A dup2 file_action tuple must have 3 elements", |
| 5433 | &tag_obj, &fd1, &fd2)) |
| 5434 | { |
| 5435 | goto fail; |
| 5436 | } |
| 5437 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 5438 | fd1, fd2); |
| 5439 | if (errno) { |
| 5440 | posix_error(); |
| 5441 | goto fail; |
| 5442 | } |
| 5443 | break; |
| 5444 | } |
| 5445 | default: { |
| 5446 | PyErr_SetString(PyExc_TypeError, |
| 5447 | "Unknown file_actions identifier"); |
| 5448 | goto fail; |
| 5449 | } |
| 5450 | } |
| 5451 | Py_DECREF(file_action); |
| 5452 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5453 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5454 | Py_DECREF(seq); |
| 5455 | return 0; |
| 5456 | |
| 5457 | fail: |
| 5458 | Py_DECREF(seq); |
| 5459 | Py_DECREF(file_action); |
| 5460 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 5461 | return -1; |
| 5462 | } |
| 5463 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5464 | |
| 5465 | static PyObject * |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5466 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
| 5467 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5468 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5469 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5470 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5471 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5472 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5473 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5474 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5475 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5476 | posix_spawnattr_t attr; |
| 5477 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5478 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5479 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5480 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5481 | pid_t pid; |
| 5482 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5483 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5484 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5485 | 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] | 5486 | like posix.environ. */ |
| 5487 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5488 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5489 | PyErr_Format(PyExc_TypeError, |
| 5490 | "%s: argv must be a tuple or list", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5491 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5492 | } |
| 5493 | argc = PySequence_Size(argv); |
| 5494 | if (argc < 1) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5495 | PyErr_Format(PyExc_ValueError, |
| 5496 | "%s: argv must not be empty", func_name); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5497 | return NULL; |
| 5498 | } |
| 5499 | |
| 5500 | if (!PyMapping_Check(env)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5501 | PyErr_Format(PyExc_TypeError, |
| 5502 | "%s: environment must be a mapping object", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5503 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5504 | } |
| 5505 | |
| 5506 | argvlist = parse_arglist(argv, &argc); |
| 5507 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5508 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5509 | } |
| 5510 | if (!argvlist[0][0]) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5511 | PyErr_Format(PyExc_ValueError, |
| 5512 | "%s: argv first element cannot be empty", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5513 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5514 | } |
| 5515 | |
| 5516 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5517 | if (envlist == NULL) { |
| 5518 | goto exit; |
| 5519 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5520 | |
Anthony Shaw | 948ed8c | 2019-05-10 12:00:06 +1000 | [diff] [blame] | 5521 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5522 | /* There is a bug in old versions of glibc that makes some of the |
| 5523 | * helper functions for manipulating file actions not copy the provided |
| 5524 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 5525 | * copy the value of path for some old versions of glibc (<2.20). |
| 5526 | * The use of temp_buffer here is a workaround that keeps the |
| 5527 | * python objects that own the buffers alive until posix_spawn gets called. |
| 5528 | * Check https://bugs.python.org/issue33630 and |
| 5529 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 5530 | temp_buffer = PyList_New(0); |
| 5531 | if (!temp_buffer) { |
| 5532 | goto exit; |
| 5533 | } |
| 5534 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5535 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5536 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5537 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5538 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5539 | |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5540 | if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid, |
| 5541 | setsigmask, setsigdef, scheduler, &attr)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5542 | goto exit; |
| 5543 | } |
| 5544 | attrp = &attr; |
| 5545 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5546 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5547 | #ifdef HAVE_POSIX_SPAWNP |
| 5548 | if (use_posix_spawnp) { |
| 5549 | err_code = posix_spawnp(&pid, path->narrow, |
| 5550 | file_actionsp, attrp, argvlist, envlist); |
| 5551 | } |
| 5552 | else |
| 5553 | #endif /* HAVE_POSIX_SPAWNP */ |
| 5554 | { |
| 5555 | err_code = posix_spawn(&pid, path->narrow, |
| 5556 | file_actionsp, attrp, argvlist, envlist); |
| 5557 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5558 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5559 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5560 | if (err_code) { |
| 5561 | errno = err_code; |
| 5562 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5563 | goto exit; |
| 5564 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 5565 | #ifdef _Py_MEMORY_SANITIZER |
| 5566 | __msan_unpoison(&pid, sizeof(pid)); |
| 5567 | #endif |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5568 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5569 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5570 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5571 | if (file_actionsp) { |
| 5572 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5573 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5574 | if (attrp) { |
| 5575 | (void)posix_spawnattr_destroy(attrp); |
| 5576 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5577 | if (envlist) { |
| 5578 | free_string_array(envlist, envc); |
| 5579 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5580 | if (argvlist) { |
| 5581 | free_string_array(argvlist, argc); |
| 5582 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5583 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5584 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5585 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5586 | |
| 5587 | |
| 5588 | /*[clinic input] |
| 5589 | |
| 5590 | os.posix_spawn |
| 5591 | path: path_t |
| 5592 | Path of executable file. |
| 5593 | argv: object |
| 5594 | Tuple or list of strings. |
| 5595 | env: object |
| 5596 | Dictionary of strings mapping to strings. |
| 5597 | / |
| 5598 | * |
| 5599 | file_actions: object(c_default='NULL') = () |
| 5600 | A sequence of file action tuples. |
| 5601 | setpgroup: object = NULL |
| 5602 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5603 | resetids: bool(accept={int}) = False |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5604 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
| 5605 | setsid: bool(accept={int}) = False |
| 5606 | 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] | 5607 | setsigmask: object(c_default='NULL') = () |
| 5608 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5609 | setsigdef: object(c_default='NULL') = () |
| 5610 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5611 | scheduler: object = NULL |
| 5612 | A tuple with the scheduler policy (optional) and parameters. |
| 5613 | |
| 5614 | Execute the program specified by path in a new process. |
| 5615 | [clinic start generated code]*/ |
| 5616 | |
| 5617 | static PyObject * |
| 5618 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5619 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5620 | PyObject *setpgroup, int resetids, int setsid, |
| 5621 | PyObject *setsigmask, PyObject *setsigdef, |
| 5622 | PyObject *scheduler) |
| 5623 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5624 | { |
| 5625 | return py_posix_spawn(0, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5626 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5627 | scheduler); |
| 5628 | } |
| 5629 | #endif /* HAVE_POSIX_SPAWN */ |
| 5630 | |
| 5631 | |
| 5632 | |
| 5633 | #ifdef HAVE_POSIX_SPAWNP |
| 5634 | /*[clinic input] |
| 5635 | |
| 5636 | os.posix_spawnp |
| 5637 | path: path_t |
| 5638 | Path of executable file. |
| 5639 | argv: object |
| 5640 | Tuple or list of strings. |
| 5641 | env: object |
| 5642 | Dictionary of strings mapping to strings. |
| 5643 | / |
| 5644 | * |
| 5645 | file_actions: object(c_default='NULL') = () |
| 5646 | A sequence of file action tuples. |
| 5647 | setpgroup: object = NULL |
| 5648 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5649 | resetids: bool(accept={int}) = False |
| 5650 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5651 | setsid: bool(accept={int}) = False |
| 5652 | 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] | 5653 | setsigmask: object(c_default='NULL') = () |
| 5654 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5655 | setsigdef: object(c_default='NULL') = () |
| 5656 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5657 | scheduler: object = NULL |
| 5658 | A tuple with the scheduler policy (optional) and parameters. |
| 5659 | |
| 5660 | Execute the program specified by path in a new process. |
| 5661 | [clinic start generated code]*/ |
| 5662 | |
| 5663 | static PyObject * |
| 5664 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5665 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5666 | PyObject *setpgroup, int resetids, int setsid, |
| 5667 | PyObject *setsigmask, PyObject *setsigdef, |
| 5668 | PyObject *scheduler) |
| 5669 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5670 | { |
| 5671 | return py_posix_spawn(1, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5672 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5673 | scheduler); |
| 5674 | } |
| 5675 | #endif /* HAVE_POSIX_SPAWNP */ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5676 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5677 | #ifdef HAVE_RTPSPAWN |
| 5678 | static intptr_t |
| 5679 | _rtp_spawn(int mode, const char *rtpFileName, const char *argv[], |
| 5680 | const char *envp[]) |
| 5681 | { |
| 5682 | RTP_ID rtpid; |
| 5683 | int status; |
| 5684 | pid_t res; |
| 5685 | int async_err = 0; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5686 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5687 | /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes. |
| 5688 | uStackSize=0 cannot be used, the default stack size is too small for |
| 5689 | Python. */ |
| 5690 | if (envp) { |
| 5691 | rtpid = rtpSpawn(rtpFileName, argv, envp, |
| 5692 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5693 | } |
| 5694 | else { |
| 5695 | rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ, |
| 5696 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5697 | } |
| 5698 | if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) { |
| 5699 | do { |
| 5700 | res = waitpid((pid_t)rtpid, &status, 0); |
| 5701 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 5702 | |
| 5703 | if (res < 0) |
| 5704 | return RTP_ID_ERROR; |
| 5705 | return ((intptr_t)status); |
| 5706 | } |
| 5707 | return ((intptr_t)rtpid); |
| 5708 | } |
| 5709 | #endif |
| 5710 | |
| 5711 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5712 | /*[clinic input] |
| 5713 | os.spawnv |
| 5714 | |
| 5715 | mode: int |
| 5716 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5717 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5718 | Path of executable file. |
| 5719 | argv: object |
| 5720 | Tuple or list of strings. |
| 5721 | / |
| 5722 | |
| 5723 | Execute the program specified by path in a new process. |
| 5724 | [clinic start generated code]*/ |
| 5725 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5726 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5727 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5728 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5729 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5730 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5731 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5732 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5733 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5734 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5735 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5736 | /* spawnv has three arguments: (mode, path, argv), where |
| 5737 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5738 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5739 | if (PyList_Check(argv)) { |
| 5740 | argc = PyList_Size(argv); |
| 5741 | getitem = PyList_GetItem; |
| 5742 | } |
| 5743 | else if (PyTuple_Check(argv)) { |
| 5744 | argc = PyTuple_Size(argv); |
| 5745 | getitem = PyTuple_GetItem; |
| 5746 | } |
| 5747 | else { |
| 5748 | PyErr_SetString(PyExc_TypeError, |
| 5749 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5750 | return NULL; |
| 5751 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5752 | if (argc == 0) { |
| 5753 | PyErr_SetString(PyExc_ValueError, |
| 5754 | "spawnv() arg 2 cannot be empty"); |
| 5755 | return NULL; |
| 5756 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5757 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5758 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5759 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5760 | return PyErr_NoMemory(); |
| 5761 | } |
| 5762 | for (i = 0; i < argc; i++) { |
| 5763 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5764 | &argvlist[i])) { |
| 5765 | free_string_array(argvlist, i); |
| 5766 | PyErr_SetString( |
| 5767 | PyExc_TypeError, |
| 5768 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5769 | return NULL; |
| 5770 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5771 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5772 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5773 | PyErr_SetString( |
| 5774 | PyExc_ValueError, |
| 5775 | "spawnv() arg 2 first element cannot be empty"); |
| 5776 | return NULL; |
| 5777 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5778 | } |
| 5779 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5780 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5781 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5782 | if (mode == _OLD_P_OVERLAY) |
| 5783 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5784 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5785 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5786 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5787 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5788 | #ifdef HAVE_WSPAWNV |
| 5789 | spawnval = _wspawnv(mode, path->wide, argvlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5790 | #elif defined(HAVE_RTPSPAWN) |
| 5791 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5792 | #else |
| 5793 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 5794 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5795 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5796 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5797 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5798 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5799 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5800 | if (spawnval == -1) |
| 5801 | return posix_error(); |
| 5802 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5803 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5804 | } |
| 5805 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5806 | /*[clinic input] |
| 5807 | os.spawnve |
| 5808 | |
| 5809 | mode: int |
| 5810 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5811 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5812 | Path of executable file. |
| 5813 | argv: object |
| 5814 | Tuple or list of strings. |
| 5815 | env: object |
| 5816 | Dictionary of strings mapping to strings. |
| 5817 | / |
| 5818 | |
| 5819 | Execute the program specified by path in a new process. |
| 5820 | [clinic start generated code]*/ |
| 5821 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5822 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5823 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5824 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5825 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5826 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5827 | EXECV_CHAR **argvlist; |
| 5828 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5829 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5830 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5831 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5832 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5833 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5834 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5835 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5836 | argv is a list or tuple of strings and env is a dictionary |
| 5837 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5838 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5839 | if (PyList_Check(argv)) { |
| 5840 | argc = PyList_Size(argv); |
| 5841 | getitem = PyList_GetItem; |
| 5842 | } |
| 5843 | else if (PyTuple_Check(argv)) { |
| 5844 | argc = PyTuple_Size(argv); |
| 5845 | getitem = PyTuple_GetItem; |
| 5846 | } |
| 5847 | else { |
| 5848 | PyErr_SetString(PyExc_TypeError, |
| 5849 | "spawnve() arg 2 must be a tuple or list"); |
| 5850 | goto fail_0; |
| 5851 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5852 | if (argc == 0) { |
| 5853 | PyErr_SetString(PyExc_ValueError, |
| 5854 | "spawnve() arg 2 cannot be empty"); |
| 5855 | goto fail_0; |
| 5856 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5857 | if (!PyMapping_Check(env)) { |
| 5858 | PyErr_SetString(PyExc_TypeError, |
| 5859 | "spawnve() arg 3 must be a mapping object"); |
| 5860 | goto fail_0; |
| 5861 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5862 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5863 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5864 | if (argvlist == NULL) { |
| 5865 | PyErr_NoMemory(); |
| 5866 | goto fail_0; |
| 5867 | } |
| 5868 | for (i = 0; i < argc; i++) { |
| 5869 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5870 | &argvlist[i])) |
| 5871 | { |
| 5872 | lastarg = i; |
| 5873 | goto fail_1; |
| 5874 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5875 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5876 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5877 | PyErr_SetString( |
| 5878 | PyExc_ValueError, |
| 5879 | "spawnv() arg 2 first element cannot be empty"); |
| 5880 | goto fail_1; |
| 5881 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5882 | } |
| 5883 | lastarg = argc; |
| 5884 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5885 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5886 | envlist = parse_envlist(env, &envc); |
| 5887 | if (envlist == NULL) |
| 5888 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5889 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5890 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5891 | if (mode == _OLD_P_OVERLAY) |
| 5892 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5893 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5894 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5895 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5896 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5897 | #ifdef HAVE_WSPAWNV |
| 5898 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5899 | #elif defined(HAVE_RTPSPAWN) |
| 5900 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, |
| 5901 | (const char **)envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5902 | #else |
| 5903 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 5904 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5905 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5906 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5907 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5908 | if (spawnval == -1) |
| 5909 | (void) posix_error(); |
| 5910 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5911 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5912 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5913 | while (--envc >= 0) |
| 5914 | PyMem_DEL(envlist[envc]); |
| 5915 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5916 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5917 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5918 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5919 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5920 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5921 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5922 | #endif /* HAVE_SPAWNV */ |
| 5923 | |
| 5924 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5925 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5926 | |
| 5927 | /* Helper function to validate arguments. |
| 5928 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 5929 | If obj is non-NULL it must be callable. */ |
| 5930 | static int |
| 5931 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 5932 | { |
| 5933 | if (obj && !PyCallable_Check(obj)) { |
| 5934 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
| 5935 | obj_name, Py_TYPE(obj)->tp_name); |
| 5936 | return -1; |
| 5937 | } |
| 5938 | return 0; |
| 5939 | } |
| 5940 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5941 | /*[clinic input] |
| 5942 | os.register_at_fork |
| 5943 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5944 | * |
| 5945 | before: object=NULL |
| 5946 | A callable to be called in the parent before the fork() syscall. |
| 5947 | after_in_child: object=NULL |
| 5948 | A callable to be called in the child after fork(). |
| 5949 | after_in_parent: object=NULL |
| 5950 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5951 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5952 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5953 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5954 | 'before' callbacks are called in reverse order. |
| 5955 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5956 | |
| 5957 | [clinic start generated code]*/ |
| 5958 | |
| 5959 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5960 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 5961 | PyObject *after_in_child, PyObject *after_in_parent) |
| 5962 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5963 | { |
| 5964 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5965 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5966 | if (!before && !after_in_child && !after_in_parent) { |
| 5967 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 5968 | return NULL; |
| 5969 | } |
| 5970 | if (check_null_or_callable(before, "before") || |
| 5971 | check_null_or_callable(after_in_child, "after_in_child") || |
| 5972 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5973 | return NULL; |
| 5974 | } |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 5975 | interp = _PyInterpreterState_Get(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5976 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5977 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5978 | return NULL; |
| 5979 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5980 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5981 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5982 | } |
| 5983 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 5984 | return NULL; |
| 5985 | } |
| 5986 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5987 | } |
| 5988 | #endif /* HAVE_FORK */ |
| 5989 | |
| 5990 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5991 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5992 | /*[clinic input] |
| 5993 | os.fork1 |
| 5994 | |
| 5995 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 5996 | |
| 5997 | Return 0 to child process and PID of child to parent process. |
| 5998 | [clinic start generated code]*/ |
| 5999 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6000 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6001 | os_fork1_impl(PyObject *module) |
| 6002 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6003 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6004 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6005 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6006 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6007 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6008 | return NULL; |
| 6009 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6010 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6011 | pid = fork1(); |
| 6012 | if (pid == 0) { |
| 6013 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6014 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6015 | } else { |
| 6016 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6017 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6018 | } |
| 6019 | if (pid == -1) |
| 6020 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6021 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6022 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6023 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6024 | |
| 6025 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6026 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6027 | /*[clinic input] |
| 6028 | os.fork |
| 6029 | |
| 6030 | Fork a child process. |
| 6031 | |
| 6032 | Return 0 to child process and PID of child to parent process. |
| 6033 | [clinic start generated code]*/ |
| 6034 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6035 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6036 | os_fork_impl(PyObject *module) |
| 6037 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6038 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6039 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6040 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6041 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6042 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6043 | return NULL; |
| 6044 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6045 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6046 | pid = fork(); |
| 6047 | if (pid == 0) { |
| 6048 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6049 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6050 | } else { |
| 6051 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6052 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6053 | } |
| 6054 | if (pid == -1) |
| 6055 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6056 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6057 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6058 | #endif /* HAVE_FORK */ |
| 6059 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6060 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6061 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6062 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6063 | /*[clinic input] |
| 6064 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6065 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6066 | policy: int |
| 6067 | |
| 6068 | Get the maximum scheduling priority for policy. |
| 6069 | [clinic start generated code]*/ |
| 6070 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6071 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6072 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 6073 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6074 | { |
| 6075 | int max; |
| 6076 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6077 | max = sched_get_priority_max(policy); |
| 6078 | if (max < 0) |
| 6079 | return posix_error(); |
| 6080 | return PyLong_FromLong(max); |
| 6081 | } |
| 6082 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6083 | |
| 6084 | /*[clinic input] |
| 6085 | os.sched_get_priority_min |
| 6086 | |
| 6087 | policy: int |
| 6088 | |
| 6089 | Get the minimum scheduling priority for policy. |
| 6090 | [clinic start generated code]*/ |
| 6091 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6092 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6093 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 6094 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6095 | { |
| 6096 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6097 | if (min < 0) |
| 6098 | return posix_error(); |
| 6099 | return PyLong_FromLong(min); |
| 6100 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6101 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 6102 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6103 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6104 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 6105 | /*[clinic input] |
| 6106 | os.sched_getscheduler |
| 6107 | pid: pid_t |
| 6108 | / |
| 6109 | |
| 6110 | Get the scheduling policy for the process identifiedy by pid. |
| 6111 | |
| 6112 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 6113 | [clinic start generated code]*/ |
| 6114 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6115 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6116 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
| 6117 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6118 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6119 | int policy; |
| 6120 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6121 | policy = sched_getscheduler(pid); |
| 6122 | if (policy < 0) |
| 6123 | return posix_error(); |
| 6124 | return PyLong_FromLong(policy); |
| 6125 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6126 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6127 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6128 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6129 | #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] | 6130 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6131 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6132 | |
| 6133 | @classmethod |
| 6134 | os.sched_param.__new__ |
| 6135 | |
| 6136 | sched_priority: object |
| 6137 | A scheduling parameter. |
| 6138 | |
| 6139 | Current has only one field: sched_priority"); |
| 6140 | [clinic start generated code]*/ |
| 6141 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6142 | static PyObject * |
| 6143 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6144 | /*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6145 | { |
| 6146 | PyObject *res; |
| 6147 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6148 | res = PyStructSequence_New(type); |
| 6149 | if (!res) |
| 6150 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6151 | Py_INCREF(sched_priority); |
| 6152 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6153 | return res; |
| 6154 | } |
| 6155 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6156 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6157 | PyDoc_VAR(os_sched_param__doc__); |
| 6158 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6159 | static PyStructSequence_Field sched_param_fields[] = { |
| 6160 | {"sched_priority", "the scheduling priority"}, |
| 6161 | {0} |
| 6162 | }; |
| 6163 | |
| 6164 | static PyStructSequence_Desc sched_param_desc = { |
| 6165 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6166 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6167 | sched_param_fields, |
| 6168 | 1 |
| 6169 | }; |
| 6170 | |
| 6171 | static int |
| 6172 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 6173 | { |
| 6174 | long priority; |
| 6175 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6176 | if (Py_TYPE(param) != SchedParamType) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6177 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 6178 | return 0; |
| 6179 | } |
| 6180 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 6181 | if (priority == -1 && PyErr_Occurred()) |
| 6182 | return 0; |
| 6183 | if (priority > INT_MAX || priority < INT_MIN) { |
| 6184 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 6185 | return 0; |
| 6186 | } |
| 6187 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 6188 | return 1; |
| 6189 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6190 | #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] | 6191 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6192 | |
| 6193 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6194 | /*[clinic input] |
| 6195 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6196 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6197 | pid: pid_t |
| 6198 | policy: int |
| 6199 | param: sched_param |
| 6200 | / |
| 6201 | |
| 6202 | Set the scheduling policy for the process identified by pid. |
| 6203 | |
| 6204 | If pid is 0, the calling process is changed. |
| 6205 | param is an instance of sched_param. |
| 6206 | [clinic start generated code]*/ |
| 6207 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6208 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6209 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6210 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6211 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6212 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6213 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6214 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6215 | ** scheduling policy under Solaris/Illumos, and others. |
| 6216 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6217 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6218 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6219 | return posix_error(); |
| 6220 | Py_RETURN_NONE; |
| 6221 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6222 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6223 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6224 | |
| 6225 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6226 | /*[clinic input] |
| 6227 | os.sched_getparam |
| 6228 | pid: pid_t |
| 6229 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6230 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6231 | Returns scheduling parameters for the process identified by pid. |
| 6232 | |
| 6233 | If pid is 0, returns parameters for the calling process. |
| 6234 | Return value is an instance of sched_param. |
| 6235 | [clinic start generated code]*/ |
| 6236 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6237 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6238 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6239 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6240 | { |
| 6241 | struct sched_param param; |
| 6242 | PyObject *result; |
| 6243 | PyObject *priority; |
| 6244 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6245 | if (sched_getparam(pid, ¶m)) |
| 6246 | return posix_error(); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6247 | result = PyStructSequence_New(SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6248 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6249 | return NULL; |
| 6250 | priority = PyLong_FromLong(param.sched_priority); |
| 6251 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6252 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6253 | return NULL; |
| 6254 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6255 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6256 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6257 | } |
| 6258 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6259 | |
| 6260 | /*[clinic input] |
| 6261 | os.sched_setparam |
| 6262 | pid: pid_t |
| 6263 | param: sched_param |
| 6264 | / |
| 6265 | |
| 6266 | Set scheduling parameters for the process identified by pid. |
| 6267 | |
| 6268 | If pid is 0, sets parameters for the calling process. |
| 6269 | param should be an instance of sched_param. |
| 6270 | [clinic start generated code]*/ |
| 6271 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6272 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6273 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6274 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6275 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6276 | { |
| 6277 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6278 | return posix_error(); |
| 6279 | Py_RETURN_NONE; |
| 6280 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6281 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6282 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6283 | |
| 6284 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6285 | /*[clinic input] |
| 6286 | os.sched_rr_get_interval -> double |
| 6287 | pid: pid_t |
| 6288 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6290 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6291 | |
| 6292 | Value returned is a float. |
| 6293 | [clinic start generated code]*/ |
| 6294 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6295 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6296 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6297 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6298 | { |
| 6299 | struct timespec interval; |
| 6300 | if (sched_rr_get_interval(pid, &interval)) { |
| 6301 | posix_error(); |
| 6302 | return -1.0; |
| 6303 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6304 | #ifdef _Py_MEMORY_SANITIZER |
| 6305 | __msan_unpoison(&interval, sizeof(interval)); |
| 6306 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6307 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6308 | } |
| 6309 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6310 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6311 | |
| 6312 | /*[clinic input] |
| 6313 | os.sched_yield |
| 6314 | |
| 6315 | Voluntarily relinquish the CPU. |
| 6316 | [clinic start generated code]*/ |
| 6317 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6318 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6319 | os_sched_yield_impl(PyObject *module) |
| 6320 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6321 | { |
| 6322 | if (sched_yield()) |
| 6323 | return posix_error(); |
| 6324 | Py_RETURN_NONE; |
| 6325 | } |
| 6326 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6327 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6328 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6329 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6330 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6331 | /*[clinic input] |
| 6332 | os.sched_setaffinity |
| 6333 | pid: pid_t |
| 6334 | mask : object |
| 6335 | / |
| 6336 | |
| 6337 | Set the CPU affinity of the process identified by pid to mask. |
| 6338 | |
| 6339 | mask should be an iterable of integers identifying CPUs. |
| 6340 | [clinic start generated code]*/ |
| 6341 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6342 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6343 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6344 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6345 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6346 | int ncpus; |
| 6347 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6348 | cpu_set_t *cpu_set = NULL; |
| 6349 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6350 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6351 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6352 | if (iterator == NULL) |
| 6353 | return NULL; |
| 6354 | |
| 6355 | ncpus = NCPUS_START; |
| 6356 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6357 | cpu_set = CPU_ALLOC(ncpus); |
| 6358 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6359 | PyErr_NoMemory(); |
| 6360 | goto error; |
| 6361 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6362 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6363 | |
| 6364 | while ((item = PyIter_Next(iterator))) { |
| 6365 | long cpu; |
| 6366 | if (!PyLong_Check(item)) { |
| 6367 | PyErr_Format(PyExc_TypeError, |
| 6368 | "expected an iterator of ints, " |
| 6369 | "but iterator yielded %R", |
| 6370 | Py_TYPE(item)); |
| 6371 | Py_DECREF(item); |
| 6372 | goto error; |
| 6373 | } |
| 6374 | cpu = PyLong_AsLong(item); |
| 6375 | Py_DECREF(item); |
| 6376 | if (cpu < 0) { |
| 6377 | if (!PyErr_Occurred()) |
| 6378 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6379 | goto error; |
| 6380 | } |
| 6381 | if (cpu > INT_MAX - 1) { |
| 6382 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6383 | goto error; |
| 6384 | } |
| 6385 | if (cpu >= ncpus) { |
| 6386 | /* Grow CPU mask to fit the CPU number */ |
| 6387 | int newncpus = ncpus; |
| 6388 | cpu_set_t *newmask; |
| 6389 | size_t newsetsize; |
| 6390 | while (newncpus <= cpu) { |
| 6391 | if (newncpus > INT_MAX / 2) |
| 6392 | newncpus = cpu + 1; |
| 6393 | else |
| 6394 | newncpus = newncpus * 2; |
| 6395 | } |
| 6396 | newmask = CPU_ALLOC(newncpus); |
| 6397 | if (newmask == NULL) { |
| 6398 | PyErr_NoMemory(); |
| 6399 | goto error; |
| 6400 | } |
| 6401 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 6402 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6403 | memcpy(newmask, cpu_set, setsize); |
| 6404 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6405 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6406 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6407 | ncpus = newncpus; |
| 6408 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6409 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6410 | } |
| 6411 | Py_CLEAR(iterator); |
| 6412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6413 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6414 | posix_error(); |
| 6415 | goto error; |
| 6416 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6417 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6418 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6419 | |
| 6420 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6421 | if (cpu_set) |
| 6422 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6423 | Py_XDECREF(iterator); |
| 6424 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6425 | } |
| 6426 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6427 | |
| 6428 | /*[clinic input] |
| 6429 | os.sched_getaffinity |
| 6430 | pid: pid_t |
| 6431 | / |
| 6432 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6433 | 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] | 6434 | |
| 6435 | The affinity is returned as a set of CPU identifiers. |
| 6436 | [clinic start generated code]*/ |
| 6437 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6438 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6439 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6440 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6441 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6442 | int cpu, ncpus, count; |
| 6443 | size_t setsize; |
| 6444 | cpu_set_t *mask = NULL; |
| 6445 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6446 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6447 | ncpus = NCPUS_START; |
| 6448 | while (1) { |
| 6449 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6450 | mask = CPU_ALLOC(ncpus); |
| 6451 | if (mask == NULL) |
| 6452 | return PyErr_NoMemory(); |
| 6453 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6454 | break; |
| 6455 | CPU_FREE(mask); |
| 6456 | if (errno != EINVAL) |
| 6457 | return posix_error(); |
| 6458 | if (ncpus > INT_MAX / 2) { |
| 6459 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6460 | "a large enough CPU set"); |
| 6461 | return NULL; |
| 6462 | } |
| 6463 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6464 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6465 | |
| 6466 | res = PySet_New(NULL); |
| 6467 | if (res == NULL) |
| 6468 | goto error; |
| 6469 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6470 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6471 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6472 | --count; |
| 6473 | if (cpu_num == NULL) |
| 6474 | goto error; |
| 6475 | if (PySet_Add(res, cpu_num)) { |
| 6476 | Py_DECREF(cpu_num); |
| 6477 | goto error; |
| 6478 | } |
| 6479 | Py_DECREF(cpu_num); |
| 6480 | } |
| 6481 | } |
| 6482 | CPU_FREE(mask); |
| 6483 | return res; |
| 6484 | |
| 6485 | error: |
| 6486 | if (mask) |
| 6487 | CPU_FREE(mask); |
| 6488 | Py_XDECREF(res); |
| 6489 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6490 | } |
| 6491 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6492 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6493 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6494 | #endif /* HAVE_SCHED_H */ |
| 6495 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6496 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6497 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6498 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6499 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6500 | #define DEV_PTY_FILE "/dev/ptc" |
| 6501 | #define HAVE_DEV_PTMX |
| 6502 | #else |
| 6503 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6504 | #endif |
| 6505 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6506 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6507 | #ifdef HAVE_PTY_H |
| 6508 | #include <pty.h> |
| 6509 | #else |
| 6510 | #ifdef HAVE_LIBUTIL_H |
| 6511 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6512 | #else |
| 6513 | #ifdef HAVE_UTIL_H |
| 6514 | #include <util.h> |
| 6515 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6516 | #endif /* HAVE_LIBUTIL_H */ |
| 6517 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6518 | #ifdef HAVE_STROPTS_H |
| 6519 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6520 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6521 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6522 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6523 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6524 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6525 | /*[clinic input] |
| 6526 | os.openpty |
| 6527 | |
| 6528 | Open a pseudo-terminal. |
| 6529 | |
| 6530 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6531 | for both the master and slave ends. |
| 6532 | [clinic start generated code]*/ |
| 6533 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6534 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6535 | os_openpty_impl(PyObject *module) |
| 6536 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6537 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6538 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6539 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6540 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6541 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6542 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6543 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 6544 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6545 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6546 | #endif |
| 6547 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6548 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6549 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6550 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6551 | goto posix_error; |
| 6552 | |
| 6553 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6554 | goto error; |
| 6555 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6556 | goto error; |
| 6557 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6558 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6559 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6560 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6561 | goto posix_error; |
| 6562 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6563 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6564 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6565 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6566 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6567 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6568 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6569 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6570 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6571 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6572 | goto posix_error; |
| 6573 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6574 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6575 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6576 | /* change permission of slave */ |
| 6577 | if (grantpt(master_fd) < 0) { |
| 6578 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6579 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6580 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6581 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6582 | /* unlock slave */ |
| 6583 | if (unlockpt(master_fd) < 0) { |
| 6584 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6585 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6586 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6587 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6588 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6589 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6590 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6591 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6592 | goto posix_error; |
| 6593 | |
| 6594 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6595 | if (slave_fd == -1) |
| 6596 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6597 | |
| 6598 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6599 | goto posix_error; |
| 6600 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6601 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6602 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6603 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6604 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6605 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6606 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6607 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6608 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6609 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6610 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6611 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6612 | posix_error: |
| 6613 | posix_error(); |
| 6614 | error: |
| 6615 | if (master_fd != -1) |
| 6616 | close(master_fd); |
| 6617 | if (slave_fd != -1) |
| 6618 | close(slave_fd); |
| 6619 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6620 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6621 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6622 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6623 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6624 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6625 | /*[clinic input] |
| 6626 | os.forkpty |
| 6627 | |
| 6628 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6629 | |
| 6630 | Returns a tuple of (pid, master_fd). |
| 6631 | Like fork(), return pid of 0 to the child process, |
| 6632 | and pid of child to the parent process. |
| 6633 | To both, return fd of newly opened pseudo-terminal. |
| 6634 | [clinic start generated code]*/ |
| 6635 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6636 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6637 | os_forkpty_impl(PyObject *module) |
| 6638 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6639 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6640 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6641 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6642 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6643 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6644 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6645 | return NULL; |
| 6646 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6647 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6648 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6649 | if (pid == 0) { |
| 6650 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6651 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6652 | } else { |
| 6653 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6654 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6655 | } |
| 6656 | if (pid == -1) |
| 6657 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6658 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6659 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6660 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6661 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6662 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6663 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6664 | /*[clinic input] |
| 6665 | os.getegid |
| 6666 | |
| 6667 | Return the current process's effective group id. |
| 6668 | [clinic start generated code]*/ |
| 6669 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6670 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6671 | os_getegid_impl(PyObject *module) |
| 6672 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6673 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6674 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6675 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6676 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6677 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6678 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6679 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6680 | /*[clinic input] |
| 6681 | os.geteuid |
| 6682 | |
| 6683 | Return the current process's effective user id. |
| 6684 | [clinic start generated code]*/ |
| 6685 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6686 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6687 | os_geteuid_impl(PyObject *module) |
| 6688 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6689 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6690 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6691 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6692 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6693 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6694 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6695 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6696 | /*[clinic input] |
| 6697 | os.getgid |
| 6698 | |
| 6699 | Return the current process's group id. |
| 6700 | [clinic start generated code]*/ |
| 6701 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6702 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6703 | os_getgid_impl(PyObject *module) |
| 6704 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6705 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6706 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6707 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6708 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6709 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6710 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6711 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6712 | /*[clinic input] |
| 6713 | os.getpid |
| 6714 | |
| 6715 | Return the current process id. |
| 6716 | [clinic start generated code]*/ |
| 6717 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6718 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6719 | os_getpid_impl(PyObject *module) |
| 6720 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6721 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6722 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6723 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6724 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6725 | |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6726 | #ifdef NGROUPS_MAX |
| 6727 | #define MAX_GROUPS NGROUPS_MAX |
| 6728 | #else |
| 6729 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6730 | #define MAX_GROUPS 64 |
| 6731 | #endif |
| 6732 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6733 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6734 | |
| 6735 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6736 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6737 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6738 | Returns a list of groups to which a user belongs.\n\n\ |
| 6739 | user: username to lookup\n\ |
| 6740 | group: base group id of the user"); |
| 6741 | |
| 6742 | static PyObject * |
| 6743 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6744 | { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6745 | const char *user; |
| 6746 | int i, ngroups; |
| 6747 | PyObject *list; |
| 6748 | #ifdef __APPLE__ |
| 6749 | int *groups, basegid; |
| 6750 | #else |
| 6751 | gid_t *groups, basegid; |
| 6752 | #endif |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6753 | |
| 6754 | /* |
| 6755 | * NGROUPS_MAX is defined by POSIX.1 as the maximum |
| 6756 | * number of supplimental groups a users can belong to. |
| 6757 | * We have to increment it by one because |
| 6758 | * getgrouplist() returns both the supplemental groups |
| 6759 | * and the primary group, i.e. all of the groups the |
| 6760 | * user belongs to. |
| 6761 | */ |
| 6762 | ngroups = 1 + MAX_GROUPS; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6763 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6764 | #ifdef __APPLE__ |
| 6765 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6766 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6767 | #else |
| 6768 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6769 | _Py_Gid_Converter, &basegid)) |
| 6770 | return NULL; |
| 6771 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6772 | |
| 6773 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6774 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6775 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6776 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6777 | #endif |
| 6778 | if (groups == NULL) |
| 6779 | return PyErr_NoMemory(); |
| 6780 | |
| 6781 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6782 | PyMem_Del(groups); |
| 6783 | return posix_error(); |
| 6784 | } |
| 6785 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6786 | #ifdef _Py_MEMORY_SANITIZER |
| 6787 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
| 6788 | __msan_unpoison(&ngroups, sizeof(ngroups)); |
| 6789 | __msan_unpoison(groups, ngroups*sizeof(*groups)); |
| 6790 | #endif |
| 6791 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6792 | list = PyList_New(ngroups); |
| 6793 | if (list == NULL) { |
| 6794 | PyMem_Del(groups); |
| 6795 | return NULL; |
| 6796 | } |
| 6797 | |
| 6798 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6799 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6800 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6801 | #else |
| 6802 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6803 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6804 | if (o == NULL) { |
| 6805 | Py_DECREF(list); |
| 6806 | PyMem_Del(groups); |
| 6807 | return NULL; |
| 6808 | } |
| 6809 | PyList_SET_ITEM(list, i, o); |
| 6810 | } |
| 6811 | |
| 6812 | PyMem_Del(groups); |
| 6813 | |
| 6814 | return list; |
| 6815 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6816 | #endif /* HAVE_GETGROUPLIST */ |
| 6817 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6818 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6819 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6820 | /*[clinic input] |
| 6821 | os.getgroups |
| 6822 | |
| 6823 | Return list of supplemental group IDs for the process. |
| 6824 | [clinic start generated code]*/ |
| 6825 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6826 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6827 | os_getgroups_impl(PyObject *module) |
| 6828 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6829 | { |
| 6830 | PyObject *result = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6831 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6832 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6833 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6834 | * This is a helper variable to store the intermediate result when |
| 6835 | * that happens. |
| 6836 | * |
| 6837 | * To keep the code readable the OSX behaviour is unconditional, |
| 6838 | * according to the POSIX spec this should be safe on all unix-y |
| 6839 | * systems. |
| 6840 | */ |
| 6841 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6842 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6843 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6844 | #ifdef __APPLE__ |
| 6845 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6846 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6847 | * always first call getgroups with length 0 to get the actual number |
| 6848 | * of groups. |
| 6849 | */ |
| 6850 | n = getgroups(0, NULL); |
| 6851 | if (n < 0) { |
| 6852 | return posix_error(); |
| 6853 | } else if (n <= MAX_GROUPS) { |
| 6854 | /* groups will fit in existing array */ |
| 6855 | alt_grouplist = grouplist; |
| 6856 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6857 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6858 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 6859 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6860 | } |
| 6861 | } |
| 6862 | |
| 6863 | n = getgroups(n, alt_grouplist); |
| 6864 | if (n == -1) { |
| 6865 | if (alt_grouplist != grouplist) { |
| 6866 | PyMem_Free(alt_grouplist); |
| 6867 | } |
| 6868 | return posix_error(); |
| 6869 | } |
| 6870 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6871 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6872 | if (n < 0) { |
| 6873 | if (errno == EINVAL) { |
| 6874 | n = getgroups(0, NULL); |
| 6875 | if (n == -1) { |
| 6876 | return posix_error(); |
| 6877 | } |
| 6878 | if (n == 0) { |
| 6879 | /* Avoid malloc(0) */ |
| 6880 | alt_grouplist = grouplist; |
| 6881 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6882 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6883 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 6884 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6885 | } |
| 6886 | n = getgroups(n, alt_grouplist); |
| 6887 | if (n == -1) { |
| 6888 | PyMem_Free(alt_grouplist); |
| 6889 | return posix_error(); |
| 6890 | } |
| 6891 | } |
| 6892 | } else { |
| 6893 | return posix_error(); |
| 6894 | } |
| 6895 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6896 | #endif |
| 6897 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6898 | result = PyList_New(n); |
| 6899 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6900 | int i; |
| 6901 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6902 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6903 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6904 | Py_DECREF(result); |
| 6905 | result = NULL; |
| 6906 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6907 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6908 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6909 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6910 | } |
| 6911 | |
| 6912 | if (alt_grouplist != grouplist) { |
| 6913 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6914 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6915 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6916 | return result; |
| 6917 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6918 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6919 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6920 | #ifdef HAVE_INITGROUPS |
| 6921 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6922 | "initgroups(username, gid) -> None\n\n\ |
| 6923 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6924 | the groups of which the specified username is a member, plus the specified\n\ |
| 6925 | group id."); |
| 6926 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6927 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6928 | static PyObject * |
| 6929 | posix_initgroups(PyObject *self, PyObject *args) |
| 6930 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6931 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6932 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6933 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6934 | #ifdef __APPLE__ |
| 6935 | int gid; |
| 6936 | #else |
| 6937 | gid_t gid; |
| 6938 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6939 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6940 | #ifdef __APPLE__ |
| 6941 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6942 | PyUnicode_FSConverter, &oname, |
| 6943 | &gid)) |
| 6944 | #else |
| 6945 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6946 | PyUnicode_FSConverter, &oname, |
| 6947 | _Py_Gid_Converter, &gid)) |
| 6948 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6949 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6950 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6951 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6952 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6953 | Py_DECREF(oname); |
| 6954 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6955 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6956 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 6957 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6958 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6959 | #endif /* HAVE_INITGROUPS */ |
| 6960 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6961 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6962 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6963 | /*[clinic input] |
| 6964 | os.getpgid |
| 6965 | |
| 6966 | pid: pid_t |
| 6967 | |
| 6968 | Call the system call getpgid(), and return the result. |
| 6969 | [clinic start generated code]*/ |
| 6970 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6971 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6972 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 6973 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6974 | { |
| 6975 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6976 | if (pgid < 0) |
| 6977 | return posix_error(); |
| 6978 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6979 | } |
| 6980 | #endif /* HAVE_GETPGID */ |
| 6981 | |
| 6982 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6983 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6984 | /*[clinic input] |
| 6985 | os.getpgrp |
| 6986 | |
| 6987 | Return the current process group id. |
| 6988 | [clinic start generated code]*/ |
| 6989 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6990 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6991 | os_getpgrp_impl(PyObject *module) |
| 6992 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6993 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6994 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6995 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6996 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6997 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6998 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6999 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7000 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7001 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7002 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7003 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7004 | /*[clinic input] |
| 7005 | os.setpgrp |
| 7006 | |
| 7007 | Make the current process the leader of its process group. |
| 7008 | [clinic start generated code]*/ |
| 7009 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7010 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7011 | os_setpgrp_impl(PyObject *module) |
| 7012 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7013 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7014 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7015 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7016 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7017 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7018 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7019 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7020 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7021 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7022 | #endif /* HAVE_SETPGRP */ |
| 7023 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7024 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7025 | |
| 7026 | #ifdef MS_WINDOWS |
| 7027 | #include <tlhelp32.h> |
| 7028 | |
| 7029 | static PyObject* |
| 7030 | win32_getppid() |
| 7031 | { |
| 7032 | HANDLE snapshot; |
| 7033 | pid_t mypid; |
| 7034 | PyObject* result = NULL; |
| 7035 | BOOL have_record; |
| 7036 | PROCESSENTRY32 pe; |
| 7037 | |
| 7038 | mypid = getpid(); /* This function never fails */ |
| 7039 | |
| 7040 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 7041 | if (snapshot == INVALID_HANDLE_VALUE) |
| 7042 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 7043 | |
| 7044 | pe.dwSize = sizeof(pe); |
| 7045 | have_record = Process32First(snapshot, &pe); |
| 7046 | while (have_record) { |
| 7047 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 7048 | /* We could cache the ulong value in a static variable. */ |
| 7049 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 7050 | break; |
| 7051 | } |
| 7052 | |
| 7053 | have_record = Process32Next(snapshot, &pe); |
| 7054 | } |
| 7055 | |
| 7056 | /* If our loop exits and our pid was not found (result will be NULL) |
| 7057 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 7058 | * error anyway, so let's raise it. */ |
| 7059 | if (!result) |
| 7060 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7061 | |
| 7062 | CloseHandle(snapshot); |
| 7063 | |
| 7064 | return result; |
| 7065 | } |
| 7066 | #endif /*MS_WINDOWS*/ |
| 7067 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7068 | |
| 7069 | /*[clinic input] |
| 7070 | os.getppid |
| 7071 | |
| 7072 | Return the parent's process id. |
| 7073 | |
| 7074 | If the parent process has already exited, Windows machines will still |
| 7075 | return its id; others systems will return the id of the 'init' process (1). |
| 7076 | [clinic start generated code]*/ |
| 7077 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7078 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7079 | os_getppid_impl(PyObject *module) |
| 7080 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7081 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7082 | #ifdef MS_WINDOWS |
| 7083 | return win32_getppid(); |
| 7084 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7085 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7086 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7087 | } |
| 7088 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7089 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7090 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7091 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7092 | /*[clinic input] |
| 7093 | os.getlogin |
| 7094 | |
| 7095 | Return the actual login name. |
| 7096 | [clinic start generated code]*/ |
| 7097 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7098 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7099 | os_getlogin_impl(PyObject *module) |
| 7100 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7101 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7102 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7103 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7104 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7105 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7106 | |
| 7107 | if (GetUserNameW(user_name, &num_chars)) { |
| 7108 | /* num_chars is the number of unicode chars plus null terminator */ |
| 7109 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7110 | } |
| 7111 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7112 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7113 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7114 | char *name; |
| 7115 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7116 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7117 | errno = 0; |
| 7118 | name = getlogin(); |
| 7119 | if (name == NULL) { |
| 7120 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7121 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7122 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7123 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7124 | } |
| 7125 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7126 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7127 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7128 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7129 | return result; |
| 7130 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7131 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7132 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7133 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7134 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7135 | /*[clinic input] |
| 7136 | os.getuid |
| 7137 | |
| 7138 | Return the current process's user id. |
| 7139 | [clinic start generated code]*/ |
| 7140 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7141 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7142 | os_getuid_impl(PyObject *module) |
| 7143 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7144 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7145 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7146 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7147 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7148 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7149 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7150 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7151 | #define HAVE_KILL |
| 7152 | #endif /* MS_WINDOWS */ |
| 7153 | |
| 7154 | #ifdef HAVE_KILL |
| 7155 | /*[clinic input] |
| 7156 | os.kill |
| 7157 | |
| 7158 | pid: pid_t |
| 7159 | signal: Py_ssize_t |
| 7160 | / |
| 7161 | |
| 7162 | Kill a process with a signal. |
| 7163 | [clinic start generated code]*/ |
| 7164 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7165 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7166 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 7167 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7168 | #ifndef MS_WINDOWS |
| 7169 | { |
| 7170 | if (kill(pid, (int)signal) == -1) |
| 7171 | return posix_error(); |
| 7172 | Py_RETURN_NONE; |
| 7173 | } |
| 7174 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7175 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7176 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7177 | DWORD sig = (DWORD)signal; |
| 7178 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7179 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7180 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7181 | /* Console processes which share a common console can be sent CTRL+C or |
| 7182 | CTRL+BREAK events, provided they handle said events. */ |
| 7183 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7184 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7185 | err = GetLastError(); |
| 7186 | PyErr_SetFromWindowsErr(err); |
| 7187 | } |
| 7188 | else |
| 7189 | Py_RETURN_NONE; |
| 7190 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7191 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7192 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 7193 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7194 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7195 | if (handle == NULL) { |
| 7196 | err = GetLastError(); |
| 7197 | return PyErr_SetFromWindowsErr(err); |
| 7198 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7199 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7200 | if (TerminateProcess(handle, sig) == 0) { |
| 7201 | err = GetLastError(); |
| 7202 | result = PyErr_SetFromWindowsErr(err); |
| 7203 | } else { |
| 7204 | Py_INCREF(Py_None); |
| 7205 | result = Py_None; |
| 7206 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7207 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7208 | CloseHandle(handle); |
| 7209 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7210 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7211 | #endif /* !MS_WINDOWS */ |
| 7212 | #endif /* HAVE_KILL */ |
| 7213 | |
| 7214 | |
| 7215 | #ifdef HAVE_KILLPG |
| 7216 | /*[clinic input] |
| 7217 | os.killpg |
| 7218 | |
| 7219 | pgid: pid_t |
| 7220 | signal: int |
| 7221 | / |
| 7222 | |
| 7223 | Kill a process group with a signal. |
| 7224 | [clinic start generated code]*/ |
| 7225 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7226 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7227 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7228 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7229 | { |
| 7230 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7231 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7232 | take the same type. Moreover, pid_t is always at least as wide as |
| 7233 | int (else compilation of this module fails), which is safe. */ |
| 7234 | if (killpg(pgid, signal) == -1) |
| 7235 | return posix_error(); |
| 7236 | Py_RETURN_NONE; |
| 7237 | } |
| 7238 | #endif /* HAVE_KILLPG */ |
| 7239 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7240 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7241 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7242 | #ifdef HAVE_SYS_LOCK_H |
| 7243 | #include <sys/lock.h> |
| 7244 | #endif |
| 7245 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7246 | /*[clinic input] |
| 7247 | os.plock |
| 7248 | op: int |
| 7249 | / |
| 7250 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7251 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7252 | [clinic start generated code]*/ |
| 7253 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7254 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7255 | os_plock_impl(PyObject *module, int op) |
| 7256 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7257 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7258 | if (plock(op) == -1) |
| 7259 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7260 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7261 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7262 | #endif /* HAVE_PLOCK */ |
| 7263 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7264 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7265 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7266 | /*[clinic input] |
| 7267 | os.setuid |
| 7268 | |
| 7269 | uid: uid_t |
| 7270 | / |
| 7271 | |
| 7272 | Set the current process's user id. |
| 7273 | [clinic start generated code]*/ |
| 7274 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7275 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7276 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7277 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7278 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7279 | if (setuid(uid) < 0) |
| 7280 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7281 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7282 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7283 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7284 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7285 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7286 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7287 | /*[clinic input] |
| 7288 | os.seteuid |
| 7289 | |
| 7290 | euid: uid_t |
| 7291 | / |
| 7292 | |
| 7293 | Set the current process's effective user id. |
| 7294 | [clinic start generated code]*/ |
| 7295 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7296 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7297 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7298 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7299 | { |
| 7300 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7301 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7302 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7303 | } |
| 7304 | #endif /* HAVE_SETEUID */ |
| 7305 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7306 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7307 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7308 | /*[clinic input] |
| 7309 | os.setegid |
| 7310 | |
| 7311 | egid: gid_t |
| 7312 | / |
| 7313 | |
| 7314 | Set the current process's effective group id. |
| 7315 | [clinic start generated code]*/ |
| 7316 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7317 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7318 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7319 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7320 | { |
| 7321 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7322 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7323 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7324 | } |
| 7325 | #endif /* HAVE_SETEGID */ |
| 7326 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7327 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7328 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7329 | /*[clinic input] |
| 7330 | os.setreuid |
| 7331 | |
| 7332 | ruid: uid_t |
| 7333 | euid: uid_t |
| 7334 | / |
| 7335 | |
| 7336 | Set the current process's real and effective user ids. |
| 7337 | [clinic start generated code]*/ |
| 7338 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7339 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7340 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 7341 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7342 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7343 | if (setreuid(ruid, euid) < 0) { |
| 7344 | return posix_error(); |
| 7345 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7346 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7347 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7348 | } |
| 7349 | #endif /* HAVE_SETREUID */ |
| 7350 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7351 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7352 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7353 | /*[clinic input] |
| 7354 | os.setregid |
| 7355 | |
| 7356 | rgid: gid_t |
| 7357 | egid: gid_t |
| 7358 | / |
| 7359 | |
| 7360 | Set the current process's real and effective group ids. |
| 7361 | [clinic start generated code]*/ |
| 7362 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7363 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7364 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 7365 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7366 | { |
| 7367 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7368 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7369 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7370 | } |
| 7371 | #endif /* HAVE_SETREGID */ |
| 7372 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7373 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7374 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7375 | /*[clinic input] |
| 7376 | os.setgid |
| 7377 | gid: gid_t |
| 7378 | / |
| 7379 | |
| 7380 | Set the current process's group id. |
| 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_setgid_impl(PyObject *module, gid_t gid) |
| 7385 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
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 (setgid(gid) < 0) |
| 7388 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7389 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7390 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7391 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7392 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7393 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7394 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7395 | /*[clinic input] |
| 7396 | os.setgroups |
| 7397 | |
| 7398 | groups: object |
| 7399 | / |
| 7400 | |
| 7401 | Set the groups of the current process to list. |
| 7402 | [clinic start generated code]*/ |
| 7403 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7404 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7405 | os_setgroups(PyObject *module, PyObject *groups) |
| 7406 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7407 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7408 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7409 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7410 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7411 | if (!PySequence_Check(groups)) { |
| 7412 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 7413 | return NULL; |
| 7414 | } |
| 7415 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7416 | if (len < 0) { |
| 7417 | return NULL; |
| 7418 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7419 | if (len > MAX_GROUPS) { |
| 7420 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7421 | return NULL; |
| 7422 | } |
| 7423 | for(i = 0; i < len; i++) { |
| 7424 | PyObject *elem; |
| 7425 | elem = PySequence_GetItem(groups, i); |
| 7426 | if (!elem) |
| 7427 | return NULL; |
| 7428 | if (!PyLong_Check(elem)) { |
| 7429 | PyErr_SetString(PyExc_TypeError, |
| 7430 | "groups must be integers"); |
| 7431 | Py_DECREF(elem); |
| 7432 | return NULL; |
| 7433 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7434 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7435 | Py_DECREF(elem); |
| 7436 | return NULL; |
| 7437 | } |
| 7438 | } |
| 7439 | Py_DECREF(elem); |
| 7440 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7441 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7442 | if (setgroups(len, grouplist) < 0) |
| 7443 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7444 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7445 | } |
| 7446 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7447 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7448 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7449 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7450 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7451 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7452 | PyObject *result; |
| 7453 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 7454 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7455 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7456 | if (pid == -1) |
| 7457 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7458 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7459 | if (struct_rusage == NULL) { |
| 7460 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7461 | if (m == NULL) |
| 7462 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 7463 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7464 | Py_DECREF(m); |
| 7465 | if (struct_rusage == NULL) |
| 7466 | return NULL; |
| 7467 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7468 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7469 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7470 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 7471 | if (!result) |
| 7472 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7473 | |
| 7474 | #ifndef doubletime |
| 7475 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7476 | #endif |
| 7477 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7478 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7479 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7480 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7481 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7482 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7483 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7484 | SET_INT(result, 2, ru->ru_maxrss); |
| 7485 | SET_INT(result, 3, ru->ru_ixrss); |
| 7486 | SET_INT(result, 4, ru->ru_idrss); |
| 7487 | SET_INT(result, 5, ru->ru_isrss); |
| 7488 | SET_INT(result, 6, ru->ru_minflt); |
| 7489 | SET_INT(result, 7, ru->ru_majflt); |
| 7490 | SET_INT(result, 8, ru->ru_nswap); |
| 7491 | SET_INT(result, 9, ru->ru_inblock); |
| 7492 | SET_INT(result, 10, ru->ru_oublock); |
| 7493 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7494 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7495 | SET_INT(result, 13, ru->ru_nsignals); |
| 7496 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7497 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7498 | #undef SET_INT |
| 7499 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7500 | if (PyErr_Occurred()) { |
| 7501 | Py_DECREF(result); |
| 7502 | return NULL; |
| 7503 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7504 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7505 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7506 | } |
| 7507 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7508 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7509 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7510 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7511 | /*[clinic input] |
| 7512 | os.wait3 |
| 7513 | |
| 7514 | options: int |
| 7515 | Wait for completion of a child process. |
| 7516 | |
| 7517 | Returns a tuple of information about the child process: |
| 7518 | (pid, status, rusage) |
| 7519 | [clinic start generated code]*/ |
| 7520 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7521 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7522 | os_wait3_impl(PyObject *module, int options) |
| 7523 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7524 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7525 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7526 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7527 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7528 | WAIT_TYPE status; |
| 7529 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7530 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7531 | do { |
| 7532 | Py_BEGIN_ALLOW_THREADS |
| 7533 | pid = wait3(&status, options, &ru); |
| 7534 | Py_END_ALLOW_THREADS |
| 7535 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7536 | if (pid < 0) |
| 7537 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7538 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7539 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7540 | } |
| 7541 | #endif /* HAVE_WAIT3 */ |
| 7542 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7543 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7544 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7545 | /*[clinic input] |
| 7546 | |
| 7547 | os.wait4 |
| 7548 | |
| 7549 | pid: pid_t |
| 7550 | options: int |
| 7551 | |
| 7552 | Wait for completion of a specific child process. |
| 7553 | |
| 7554 | Returns a tuple of information about the child process: |
| 7555 | (pid, status, rusage) |
| 7556 | [clinic start generated code]*/ |
| 7557 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7558 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7559 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7560 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7561 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7562 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7563 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7564 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7565 | WAIT_TYPE status; |
| 7566 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7567 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7568 | do { |
| 7569 | Py_BEGIN_ALLOW_THREADS |
| 7570 | res = wait4(pid, &status, options, &ru); |
| 7571 | Py_END_ALLOW_THREADS |
| 7572 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7573 | if (res < 0) |
| 7574 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7575 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7576 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7577 | } |
| 7578 | #endif /* HAVE_WAIT4 */ |
| 7579 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7580 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7581 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7582 | /*[clinic input] |
| 7583 | os.waitid |
| 7584 | |
| 7585 | idtype: idtype_t |
| 7586 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7587 | id: id_t |
| 7588 | The id to wait on. |
| 7589 | options: int |
| 7590 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7591 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7592 | / |
| 7593 | |
| 7594 | Returns the result of waiting for a process or processes. |
| 7595 | |
| 7596 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7597 | no children in a waitable state. |
| 7598 | [clinic start generated code]*/ |
| 7599 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7600 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7601 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7602 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7603 | { |
| 7604 | PyObject *result; |
| 7605 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7606 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7607 | siginfo_t si; |
| 7608 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7609 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7610 | do { |
| 7611 | Py_BEGIN_ALLOW_THREADS |
| 7612 | res = waitid(idtype, id, &si, options); |
| 7613 | Py_END_ALLOW_THREADS |
| 7614 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7615 | if (res < 0) |
| 7616 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7617 | |
| 7618 | if (si.si_pid == 0) |
| 7619 | Py_RETURN_NONE; |
| 7620 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 7621 | result = PyStructSequence_New(WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7622 | if (!result) |
| 7623 | return NULL; |
| 7624 | |
| 7625 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7626 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7627 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7628 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7629 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7630 | if (PyErr_Occurred()) { |
| 7631 | Py_DECREF(result); |
| 7632 | return NULL; |
| 7633 | } |
| 7634 | |
| 7635 | return result; |
| 7636 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7637 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7638 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7639 | |
| 7640 | #if defined(HAVE_WAITPID) |
| 7641 | /*[clinic input] |
| 7642 | os.waitpid |
| 7643 | pid: pid_t |
| 7644 | options: int |
| 7645 | / |
| 7646 | |
| 7647 | Wait for completion of a given child process. |
| 7648 | |
| 7649 | Returns a tuple of information regarding the child process: |
| 7650 | (pid, status) |
| 7651 | |
| 7652 | The options argument is ignored on Windows. |
| 7653 | [clinic start generated code]*/ |
| 7654 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7655 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7656 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7657 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7658 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7659 | pid_t res; |
| 7660 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7661 | WAIT_TYPE status; |
| 7662 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7663 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7664 | do { |
| 7665 | Py_BEGIN_ALLOW_THREADS |
| 7666 | res = waitpid(pid, &status, options); |
| 7667 | Py_END_ALLOW_THREADS |
| 7668 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7669 | if (res < 0) |
| 7670 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7671 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7672 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7673 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7674 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7675 | /* 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] | 7676 | /*[clinic input] |
| 7677 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7678 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7679 | options: int |
| 7680 | / |
| 7681 | |
| 7682 | Wait for completion of a given process. |
| 7683 | |
| 7684 | Returns a tuple of information regarding the process: |
| 7685 | (pid, status << 8) |
| 7686 | |
| 7687 | The options argument is ignored on Windows. |
| 7688 | [clinic start generated code]*/ |
| 7689 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7690 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7691 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7692 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7693 | { |
| 7694 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7695 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7696 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7697 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7698 | do { |
| 7699 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7700 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7701 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7702 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7703 | Py_END_ALLOW_THREADS |
| 7704 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7705 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7706 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7707 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7708 | /* 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] | 7709 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7710 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7711 | #endif |
| 7712 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7713 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7714 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7715 | /*[clinic input] |
| 7716 | os.wait |
| 7717 | |
| 7718 | Wait for completion of a child process. |
| 7719 | |
| 7720 | Returns a tuple of information about the child process: |
| 7721 | (pid, status) |
| 7722 | [clinic start generated code]*/ |
| 7723 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7724 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7725 | os_wait_impl(PyObject *module) |
| 7726 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7727 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7728 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7729 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7730 | WAIT_TYPE status; |
| 7731 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7732 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7733 | do { |
| 7734 | Py_BEGIN_ALLOW_THREADS |
| 7735 | pid = wait(&status); |
| 7736 | Py_END_ALLOW_THREADS |
| 7737 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7738 | if (pid < 0) |
| 7739 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7740 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7741 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7742 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7743 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7744 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7745 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7746 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7747 | /*[clinic input] |
| 7748 | os.readlink |
| 7749 | |
| 7750 | path: path_t |
| 7751 | * |
| 7752 | dir_fd: dir_fd(requires='readlinkat') = None |
| 7753 | |
| 7754 | Return a string representing the path to which the symbolic link points. |
| 7755 | |
| 7756 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7757 | and path should be relative; path will then be relative to that directory. |
| 7758 | |
| 7759 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 7760 | using it will raise a NotImplementedError. |
| 7761 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7762 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7763 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7764 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 7765 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7766 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7767 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 7768 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7769 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7770 | |
| 7771 | Py_BEGIN_ALLOW_THREADS |
| 7772 | #ifdef HAVE_READLINKAT |
| 7773 | if (dir_fd != DEFAULT_DIR_FD) |
| 7774 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 7775 | else |
| 7776 | #endif |
| 7777 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 7778 | Py_END_ALLOW_THREADS |
| 7779 | |
| 7780 | if (length < 0) { |
| 7781 | return path_error(path); |
| 7782 | } |
| 7783 | buffer[length] = '\0'; |
| 7784 | |
| 7785 | if (PyUnicode_Check(path->object)) |
| 7786 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7787 | else |
| 7788 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7789 | #elif defined(MS_WINDOWS) |
| 7790 | DWORD n_bytes_returned; |
| 7791 | DWORD io_result; |
| 7792 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7793 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7794 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
| 7795 | const wchar_t *print_name; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7796 | PyObject *result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7797 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7798 | /* First get a handle to the reparse point */ |
| 7799 | Py_BEGIN_ALLOW_THREADS |
| 7800 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7801 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7802 | 0, |
| 7803 | 0, |
| 7804 | 0, |
| 7805 | OPEN_EXISTING, |
| 7806 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7807 | 0); |
| 7808 | Py_END_ALLOW_THREADS |
| 7809 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7810 | if (reparse_point_handle == INVALID_HANDLE_VALUE) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7811 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7812 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7813 | |
| 7814 | Py_BEGIN_ALLOW_THREADS |
| 7815 | /* New call DeviceIoControl to read the reparse point */ |
| 7816 | io_result = DeviceIoControl( |
| 7817 | reparse_point_handle, |
| 7818 | FSCTL_GET_REPARSE_POINT, |
| 7819 | 0, 0, /* in buffer */ |
| 7820 | target_buffer, sizeof(target_buffer), |
| 7821 | &n_bytes_returned, |
| 7822 | 0 /* we're not using OVERLAPPED_IO */ |
| 7823 | ); |
| 7824 | CloseHandle(reparse_point_handle); |
| 7825 | Py_END_ALLOW_THREADS |
| 7826 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7827 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7828 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7829 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7830 | |
| 7831 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7832 | { |
| 7833 | PyErr_SetString(PyExc_ValueError, |
| 7834 | "not a symbolic link"); |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7835 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7836 | } |
SSE4 | 3c34aad | 2018-02-13 00:10:35 +0700 | [diff] [blame] | 7837 | print_name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7838 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7839 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7840 | result = PyUnicode_FromWideChar(print_name, |
| 7841 | rdb->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t)); |
| 7842 | if (path->narrow) { |
| 7843 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7844 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7845 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7846 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7847 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7848 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7849 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7850 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7851 | |
| 7852 | #if defined(MS_WINDOWS) |
| 7853 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7854 | /* Remove the last portion of the path - return 0 on success */ |
| 7855 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7856 | _dirnameW(WCHAR *path) |
| 7857 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7858 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7859 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 7860 | if (length == MAX_PATH) { |
| 7861 | return -1; |
| 7862 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7863 | |
| 7864 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7865 | for(ptr = path + length; ptr != path; ptr--) { |
| 7866 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7867 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7868 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7869 | } |
| 7870 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7871 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7872 | } |
| 7873 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7874 | /* Is this path absolute? */ |
| 7875 | static int |
| 7876 | _is_absW(const WCHAR *path) |
| 7877 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7878 | return path[0] == L'\\' || path[0] == L'/' || |
| 7879 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7880 | } |
| 7881 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7882 | /* join root and rest with a backslash - return 0 on success */ |
| 7883 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7884 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7885 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7886 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7887 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7888 | } |
| 7889 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7890 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 7891 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7892 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7893 | |
| 7894 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 7895 | return -1; |
| 7896 | } |
| 7897 | |
| 7898 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7899 | } |
| 7900 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7901 | /* Return True if the path at src relative to dest is a directory */ |
| 7902 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7903 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7904 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7905 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7906 | WCHAR dest_parent[MAX_PATH]; |
| 7907 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7908 | |
| 7909 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7910 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 7911 | _dirnameW(dest_parent)) { |
| 7912 | return 0; |
| 7913 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7914 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7915 | if (_joinW(src_resolved, dest_parent, src)) { |
| 7916 | return 0; |
| 7917 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7918 | return ( |
| 7919 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7920 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7921 | ); |
| 7922 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7923 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7924 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7925 | |
| 7926 | /*[clinic input] |
| 7927 | os.symlink |
| 7928 | src: path_t |
| 7929 | dst: path_t |
| 7930 | target_is_directory: bool = False |
| 7931 | * |
| 7932 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7933 | |
| 7934 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7935 | |
| 7936 | Create a symbolic link pointing to src named dst. |
| 7937 | |
| 7938 | target_is_directory is required on Windows if the target is to be |
| 7939 | interpreted as a directory. (On Windows, symlink requires |
| 7940 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7941 | target_is_directory is ignored on non-Windows platforms. |
| 7942 | |
| 7943 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7944 | and path should be relative; path will then be relative to that directory. |
| 7945 | dir_fd may not be implemented on your platform. |
| 7946 | If it is unavailable, using it will raise a NotImplementedError. |
| 7947 | |
| 7948 | [clinic start generated code]*/ |
| 7949 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7950 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7951 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7952 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7953 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7954 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7955 | #ifdef MS_WINDOWS |
| 7956 | DWORD result; |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 7957 | DWORD flags = 0; |
| 7958 | |
| 7959 | /* Assumed true, set to false if detected to not be available. */ |
| 7960 | static int windows_has_symlink_unprivileged_flag = TRUE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7961 | #else |
| 7962 | int result; |
| 7963 | #endif |
| 7964 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7965 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7966 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 7967 | if (windows_has_symlink_unprivileged_flag) { |
| 7968 | /* Allow non-admin symlinks if system allows it. */ |
| 7969 | flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
| 7970 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7971 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7972 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7973 | _Py_BEGIN_SUPPRESS_IPH |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 7974 | /* if src is a directory, ensure flags==1 (target_is_directory bit) */ |
| 7975 | if (target_is_directory || _check_dirW(src->wide, dst->wide)) { |
| 7976 | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 7977 | } |
| 7978 | |
| 7979 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7980 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7981 | Py_END_ALLOW_THREADS |
| 7982 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 7983 | if (windows_has_symlink_unprivileged_flag && !result && |
| 7984 | ERROR_INVALID_PARAMETER == GetLastError()) { |
| 7985 | |
| 7986 | Py_BEGIN_ALLOW_THREADS |
| 7987 | _Py_BEGIN_SUPPRESS_IPH |
| 7988 | /* This error might be caused by |
| 7989 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported. |
| 7990 | Try again, and update windows_has_symlink_unprivileged_flag if we |
| 7991 | are successful this time. |
| 7992 | |
| 7993 | NOTE: There is a risk of a race condition here if there are other |
| 7994 | conditions than the flag causing ERROR_INVALID_PARAMETER, and |
| 7995 | another process (or thread) changes that condition in between our |
| 7996 | calls to CreateSymbolicLink. |
| 7997 | */ |
| 7998 | flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 7999 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
| 8000 | _Py_END_SUPPRESS_IPH |
| 8001 | Py_END_ALLOW_THREADS |
| 8002 | |
| 8003 | if (result || ERROR_INVALID_PARAMETER != GetLastError()) { |
| 8004 | windows_has_symlink_unprivileged_flag = FALSE; |
| 8005 | } |
| 8006 | } |
| 8007 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8008 | if (!result) |
| 8009 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8010 | |
| 8011 | #else |
| 8012 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8013 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 8014 | PyErr_SetString(PyExc_ValueError, |
| 8015 | "symlink: src and dst must be the same type"); |
| 8016 | return NULL; |
| 8017 | } |
| 8018 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8019 | Py_BEGIN_ALLOW_THREADS |
| 8020 | #if HAVE_SYMLINKAT |
| 8021 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8022 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8023 | else |
| 8024 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8025 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8026 | Py_END_ALLOW_THREADS |
| 8027 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8028 | if (result) |
| 8029 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8030 | #endif |
| 8031 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8032 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8033 | } |
| 8034 | #endif /* HAVE_SYMLINK */ |
| 8035 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8036 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8037 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8038 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8039 | static PyStructSequence_Field times_result_fields[] = { |
| 8040 | {"user", "user time"}, |
| 8041 | {"system", "system time"}, |
| 8042 | {"children_user", "user time of children"}, |
| 8043 | {"children_system", "system time of children"}, |
| 8044 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 8045 | {NULL} |
| 8046 | }; |
| 8047 | |
| 8048 | PyDoc_STRVAR(times_result__doc__, |
| 8049 | "times_result: Result from os.times().\n\n\ |
| 8050 | This object may be accessed either as a tuple of\n\ |
| 8051 | (user, system, children_user, children_system, elapsed),\n\ |
| 8052 | or via the attributes user, system, children_user, children_system,\n\ |
| 8053 | and elapsed.\n\ |
| 8054 | \n\ |
| 8055 | See os.times for more information."); |
| 8056 | |
| 8057 | static PyStructSequence_Desc times_result_desc = { |
| 8058 | "times_result", /* name */ |
| 8059 | times_result__doc__, /* doc */ |
| 8060 | times_result_fields, |
| 8061 | 5 |
| 8062 | }; |
| 8063 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 8064 | static PyTypeObject* TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8065 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8066 | #ifdef MS_WINDOWS |
| 8067 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 8068 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8069 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8070 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8071 | |
| 8072 | static PyObject * |
| 8073 | build_times_result(double user, double system, |
| 8074 | double children_user, double children_system, |
| 8075 | double elapsed) |
| 8076 | { |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 8077 | PyObject *value = PyStructSequence_New(TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8078 | if (value == NULL) |
| 8079 | return NULL; |
| 8080 | |
| 8081 | #define SET(i, field) \ |
| 8082 | { \ |
| 8083 | PyObject *o = PyFloat_FromDouble(field); \ |
| 8084 | if (!o) { \ |
| 8085 | Py_DECREF(value); \ |
| 8086 | return NULL; \ |
| 8087 | } \ |
| 8088 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 8089 | } \ |
| 8090 | |
| 8091 | SET(0, user); |
| 8092 | SET(1, system); |
| 8093 | SET(2, children_user); |
| 8094 | SET(3, children_system); |
| 8095 | SET(4, elapsed); |
| 8096 | |
| 8097 | #undef SET |
| 8098 | |
| 8099 | return value; |
| 8100 | } |
| 8101 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8102 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8103 | #ifndef MS_WINDOWS |
| 8104 | #define NEED_TICKS_PER_SECOND |
| 8105 | static long ticks_per_second = -1; |
| 8106 | #endif /* MS_WINDOWS */ |
| 8107 | |
| 8108 | /*[clinic input] |
| 8109 | os.times |
| 8110 | |
| 8111 | Return a collection containing process timing information. |
| 8112 | |
| 8113 | The object returned behaves like a named tuple with these fields: |
| 8114 | (utime, stime, cutime, cstime, elapsed_time) |
| 8115 | All fields are floating point numbers. |
| 8116 | [clinic start generated code]*/ |
| 8117 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8118 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8119 | os_times_impl(PyObject *module) |
| 8120 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8121 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8122 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8123 | FILETIME create, exit, kernel, user; |
| 8124 | HANDLE hProc; |
| 8125 | hProc = GetCurrentProcess(); |
| 8126 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 8127 | /* The fields of a FILETIME structure are the hi and lo part |
| 8128 | of a 64-bit value expressed in 100 nanosecond units. |
| 8129 | 1e7 is one second in such units; 1e-7 the inverse. |
| 8130 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 8131 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8132 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8133 | (double)(user.dwHighDateTime*429.4967296 + |
| 8134 | user.dwLowDateTime*1e-7), |
| 8135 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 8136 | kernel.dwLowDateTime*1e-7), |
| 8137 | (double)0, |
| 8138 | (double)0, |
| 8139 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8140 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8141 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8142 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8143 | |
| 8144 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8145 | struct tms t; |
| 8146 | clock_t c; |
| 8147 | errno = 0; |
| 8148 | c = times(&t); |
| 8149 | if (c == (clock_t) -1) |
| 8150 | return posix_error(); |
| 8151 | return build_times_result( |
| 8152 | (double)t.tms_utime / ticks_per_second, |
| 8153 | (double)t.tms_stime / ticks_per_second, |
| 8154 | (double)t.tms_cutime / ticks_per_second, |
| 8155 | (double)t.tms_cstime / ticks_per_second, |
| 8156 | (double)c / ticks_per_second); |
| 8157 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8158 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8159 | #endif /* HAVE_TIMES */ |
| 8160 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8161 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8162 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8163 | /*[clinic input] |
| 8164 | os.getsid |
| 8165 | |
| 8166 | pid: pid_t |
| 8167 | / |
| 8168 | |
| 8169 | Call the system call getsid(pid) and return the result. |
| 8170 | [clinic start generated code]*/ |
| 8171 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8172 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8173 | os_getsid_impl(PyObject *module, pid_t pid) |
| 8174 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8175 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8176 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8177 | sid = getsid(pid); |
| 8178 | if (sid < 0) |
| 8179 | return posix_error(); |
| 8180 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8181 | } |
| 8182 | #endif /* HAVE_GETSID */ |
| 8183 | |
| 8184 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8185 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8186 | /*[clinic input] |
| 8187 | os.setsid |
| 8188 | |
| 8189 | Call the system call setsid(). |
| 8190 | [clinic start generated code]*/ |
| 8191 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8192 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8193 | os_setsid_impl(PyObject *module) |
| 8194 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8195 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8196 | if (setsid() < 0) |
| 8197 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8198 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8199 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8200 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8201 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8202 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8203 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8204 | /*[clinic input] |
| 8205 | os.setpgid |
| 8206 | |
| 8207 | pid: pid_t |
| 8208 | pgrp: pid_t |
| 8209 | / |
| 8210 | |
| 8211 | Call the system call setpgid(pid, pgrp). |
| 8212 | [clinic start generated code]*/ |
| 8213 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8214 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8215 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 8216 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8217 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8218 | if (setpgid(pid, pgrp) < 0) |
| 8219 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8220 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8221 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8222 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8223 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8224 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8225 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8226 | /*[clinic input] |
| 8227 | os.tcgetpgrp |
| 8228 | |
| 8229 | fd: int |
| 8230 | / |
| 8231 | |
| 8232 | Return the process group associated with the terminal specified by fd. |
| 8233 | [clinic start generated code]*/ |
| 8234 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8235 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8236 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8237 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8238 | { |
| 8239 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8240 | if (pgid < 0) |
| 8241 | return posix_error(); |
| 8242 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8243 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8244 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8245 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8246 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8247 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8248 | /*[clinic input] |
| 8249 | os.tcsetpgrp |
| 8250 | |
| 8251 | fd: int |
| 8252 | pgid: pid_t |
| 8253 | / |
| 8254 | |
| 8255 | Set the process group associated with the terminal specified by fd. |
| 8256 | [clinic start generated code]*/ |
| 8257 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8258 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8259 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8260 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8261 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8262 | if (tcsetpgrp(fd, pgid) < 0) |
| 8263 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8264 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8265 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8266 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8267 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8268 | /* Functions acting on file descriptors */ |
| 8269 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8270 | #ifdef O_CLOEXEC |
| 8271 | extern int _Py_open_cloexec_works; |
| 8272 | #endif |
| 8273 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8274 | |
| 8275 | /*[clinic input] |
| 8276 | os.open -> int |
| 8277 | path: path_t |
| 8278 | flags: int |
| 8279 | mode: int = 0o777 |
| 8280 | * |
| 8281 | dir_fd: dir_fd(requires='openat') = None |
| 8282 | |
| 8283 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 8284 | |
| 8285 | Open a file for low level IO. Returns a file descriptor (integer). |
| 8286 | |
| 8287 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8288 | and path should be relative; path will then be relative to that directory. |
| 8289 | dir_fd may not be implemented on your platform. |
| 8290 | If it is unavailable, using it will raise a NotImplementedError. |
| 8291 | [clinic start generated code]*/ |
| 8292 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8293 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8294 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 8295 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8296 | { |
| 8297 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8298 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8299 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8300 | #ifdef O_CLOEXEC |
| 8301 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 8302 | #elif !defined(MS_WINDOWS) |
| 8303 | int *atomic_flag_works = NULL; |
| 8304 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8305 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8306 | #ifdef MS_WINDOWS |
| 8307 | flags |= O_NOINHERIT; |
| 8308 | #elif defined(O_CLOEXEC) |
| 8309 | flags |= O_CLOEXEC; |
| 8310 | #endif |
| 8311 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 8312 | if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) { |
| 8313 | return -1; |
| 8314 | } |
| 8315 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8316 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8317 | do { |
| 8318 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8319 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8320 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8321 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8322 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8323 | if (dir_fd != DEFAULT_DIR_FD) |
| 8324 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 8325 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8326 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8327 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8328 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8329 | Py_END_ALLOW_THREADS |
| 8330 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8331 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8332 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8333 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8334 | if (!async_err) |
| 8335 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8336 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8337 | } |
| 8338 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8339 | #ifndef MS_WINDOWS |
| 8340 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 8341 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8342 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8343 | } |
| 8344 | #endif |
| 8345 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8346 | return fd; |
| 8347 | } |
| 8348 | |
| 8349 | |
| 8350 | /*[clinic input] |
| 8351 | os.close |
| 8352 | |
| 8353 | fd: int |
| 8354 | |
| 8355 | Close a file descriptor. |
| 8356 | [clinic start generated code]*/ |
| 8357 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8358 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8359 | os_close_impl(PyObject *module, int fd) |
| 8360 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8361 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8362 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8363 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 8364 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 8365 | * for more details. |
| 8366 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8367 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8368 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8369 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8370 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8371 | Py_END_ALLOW_THREADS |
| 8372 | if (res < 0) |
| 8373 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8374 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8375 | } |
| 8376 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8377 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8378 | /*[clinic input] |
| 8379 | os.closerange |
| 8380 | |
| 8381 | fd_low: int |
| 8382 | fd_high: int |
| 8383 | / |
| 8384 | |
| 8385 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 8386 | [clinic start generated code]*/ |
| 8387 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8388 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8389 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 8390 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8391 | { |
| 8392 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8393 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8394 | _Py_BEGIN_SUPPRESS_IPH |
Benjamin Peterson | 207116b | 2016-09-08 11:28:06 -0700 | [diff] [blame] | 8395 | for (i = Py_MAX(fd_low, 0); i < fd_high; i++) |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 8396 | close(i); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8397 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8398 | Py_END_ALLOW_THREADS |
| 8399 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8400 | } |
| 8401 | |
| 8402 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8403 | /*[clinic input] |
| 8404 | os.dup -> int |
| 8405 | |
| 8406 | fd: int |
| 8407 | / |
| 8408 | |
| 8409 | Return a duplicate of a file descriptor. |
| 8410 | [clinic start generated code]*/ |
| 8411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8412 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8413 | os_dup_impl(PyObject *module, int fd) |
| 8414 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8415 | { |
| 8416 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8417 | } |
| 8418 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8419 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8420 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8421 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8422 | fd: int |
| 8423 | fd2: int |
| 8424 | inheritable: bool=True |
| 8425 | |
| 8426 | Duplicate file descriptor. |
| 8427 | [clinic start generated code]*/ |
| 8428 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8429 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8430 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8431 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8432 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8433 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8434 | #if defined(HAVE_DUP3) && \ |
| 8435 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8436 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8437 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8438 | #endif |
| 8439 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8440 | if (fd < 0 || fd2 < 0) { |
| 8441 | posix_error(); |
| 8442 | return -1; |
| 8443 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8444 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8445 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8446 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8447 | * upon close(), and therefore below. |
| 8448 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8449 | #ifdef MS_WINDOWS |
| 8450 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8451 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8452 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8453 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8454 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8455 | if (res < 0) { |
| 8456 | posix_error(); |
| 8457 | return -1; |
| 8458 | } |
| 8459 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8460 | |
| 8461 | /* Character files like console cannot be make non-inheritable */ |
| 8462 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8463 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8464 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8465 | } |
| 8466 | |
| 8467 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8468 | Py_BEGIN_ALLOW_THREADS |
| 8469 | if (!inheritable) |
| 8470 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8471 | else |
| 8472 | res = dup2(fd, fd2); |
| 8473 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8474 | if (res < 0) { |
| 8475 | posix_error(); |
| 8476 | return -1; |
| 8477 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8478 | |
| 8479 | #else |
| 8480 | |
| 8481 | #ifdef HAVE_DUP3 |
| 8482 | if (!inheritable && dup3_works != 0) { |
| 8483 | Py_BEGIN_ALLOW_THREADS |
| 8484 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8485 | Py_END_ALLOW_THREADS |
| 8486 | if (res < 0) { |
| 8487 | if (dup3_works == -1) |
| 8488 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8489 | if (dup3_works) { |
| 8490 | posix_error(); |
| 8491 | return -1; |
| 8492 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8493 | } |
| 8494 | } |
| 8495 | |
| 8496 | if (inheritable || dup3_works == 0) |
| 8497 | { |
| 8498 | #endif |
| 8499 | Py_BEGIN_ALLOW_THREADS |
| 8500 | res = dup2(fd, fd2); |
| 8501 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8502 | if (res < 0) { |
| 8503 | posix_error(); |
| 8504 | return -1; |
| 8505 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8506 | |
| 8507 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8508 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8509 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8510 | } |
| 8511 | #ifdef HAVE_DUP3 |
| 8512 | } |
| 8513 | #endif |
| 8514 | |
| 8515 | #endif |
| 8516 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8517 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8518 | } |
| 8519 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8520 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8521 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8522 | /*[clinic input] |
| 8523 | os.lockf |
| 8524 | |
| 8525 | fd: int |
| 8526 | An open file descriptor. |
| 8527 | command: int |
| 8528 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8529 | length: Py_off_t |
| 8530 | The number of bytes to lock, starting at the current position. |
| 8531 | / |
| 8532 | |
| 8533 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8534 | |
| 8535 | [clinic start generated code]*/ |
| 8536 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8537 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8538 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8539 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8540 | { |
| 8541 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8542 | |
| 8543 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8544 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8545 | Py_END_ALLOW_THREADS |
| 8546 | |
| 8547 | if (res < 0) |
| 8548 | return posix_error(); |
| 8549 | |
| 8550 | Py_RETURN_NONE; |
| 8551 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8552 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8553 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8554 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8555 | /*[clinic input] |
| 8556 | os.lseek -> Py_off_t |
| 8557 | |
| 8558 | fd: int |
| 8559 | position: Py_off_t |
| 8560 | how: int |
| 8561 | / |
| 8562 | |
| 8563 | Set the position of a file descriptor. Return the new position. |
| 8564 | |
| 8565 | Return the new cursor position in number of bytes |
| 8566 | relative to the beginning of the file. |
| 8567 | [clinic start generated code]*/ |
| 8568 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8569 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8570 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8571 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8572 | { |
| 8573 | Py_off_t result; |
| 8574 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8575 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8576 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8577 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8578 | case 0: how = SEEK_SET; break; |
| 8579 | case 1: how = SEEK_CUR; break; |
| 8580 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8581 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8582 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8583 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8584 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8585 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8586 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8587 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8588 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8589 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8590 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8591 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8592 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8593 | if (result < 0) |
| 8594 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8595 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8596 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8597 | } |
| 8598 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8599 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8600 | /*[clinic input] |
| 8601 | os.read |
| 8602 | fd: int |
| 8603 | length: Py_ssize_t |
| 8604 | / |
| 8605 | |
| 8606 | Read from a file descriptor. Returns a bytes object. |
| 8607 | [clinic start generated code]*/ |
| 8608 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8609 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8610 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 8611 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8612 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8613 | Py_ssize_t n; |
| 8614 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8615 | |
| 8616 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8617 | errno = EINVAL; |
| 8618 | return posix_error(); |
| 8619 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8620 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 8621 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8622 | |
| 8623 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8624 | if (buffer == NULL) |
| 8625 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8626 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8627 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8628 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8629 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8630 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8631 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8632 | |
| 8633 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8634 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8635 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8636 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8637 | } |
| 8638 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8639 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8640 | || defined(__APPLE__))) \ |
| 8641 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 8642 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 8643 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8644 | 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] | 8645 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8646 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8647 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8648 | *iov = PyMem_New(struct iovec, cnt); |
| 8649 | if (*iov == NULL) { |
| 8650 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8651 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8652 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8653 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8654 | *buf = PyMem_New(Py_buffer, cnt); |
| 8655 | if (*buf == NULL) { |
| 8656 | PyMem_Del(*iov); |
| 8657 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8658 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8659 | } |
| 8660 | |
| 8661 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8662 | PyObject *item = PySequence_GetItem(seq, i); |
| 8663 | if (item == NULL) |
| 8664 | goto fail; |
| 8665 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8666 | Py_DECREF(item); |
| 8667 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8668 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8669 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8670 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8671 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8672 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8673 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8674 | |
| 8675 | fail: |
| 8676 | PyMem_Del(*iov); |
| 8677 | for (j = 0; j < i; j++) { |
| 8678 | PyBuffer_Release(&(*buf)[j]); |
| 8679 | } |
| 8680 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8681 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8682 | } |
| 8683 | |
| 8684 | static void |
| 8685 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8686 | { |
| 8687 | int i; |
| 8688 | PyMem_Del(iov); |
| 8689 | for (i = 0; i < cnt; i++) { |
| 8690 | PyBuffer_Release(&buf[i]); |
| 8691 | } |
| 8692 | PyMem_Del(buf); |
| 8693 | } |
| 8694 | #endif |
| 8695 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8696 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8697 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8698 | /*[clinic input] |
| 8699 | os.readv -> Py_ssize_t |
| 8700 | |
| 8701 | fd: int |
| 8702 | buffers: object |
| 8703 | / |
| 8704 | |
| 8705 | Read from a file descriptor fd into an iterable of buffers. |
| 8706 | |
| 8707 | The buffers should be mutable buffers accepting bytes. |
| 8708 | readv will transfer data into each buffer until it is full |
| 8709 | and then move on to the next buffer in the sequence to hold |
| 8710 | the rest of the data. |
| 8711 | |
| 8712 | readv returns the total number of bytes read, |
| 8713 | which may be less than the total capacity of all the buffers. |
| 8714 | [clinic start generated code]*/ |
| 8715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8716 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8717 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 8718 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8719 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8720 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8721 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8722 | struct iovec *iov; |
| 8723 | Py_buffer *buf; |
| 8724 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8725 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8726 | PyErr_SetString(PyExc_TypeError, |
| 8727 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8728 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8729 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8730 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8731 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8732 | if (cnt < 0) |
| 8733 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8734 | |
| 8735 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 8736 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8737 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8738 | do { |
| 8739 | Py_BEGIN_ALLOW_THREADS |
| 8740 | n = readv(fd, iov, cnt); |
| 8741 | Py_END_ALLOW_THREADS |
| 8742 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8743 | |
| 8744 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8745 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8746 | if (!async_err) |
| 8747 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8748 | return -1; |
| 8749 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8750 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8751 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8752 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8753 | #endif /* HAVE_READV */ |
| 8754 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8755 | |
| 8756 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8757 | /*[clinic input] |
| 8758 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 8759 | os.pread |
| 8760 | |
| 8761 | fd: int |
| 8762 | length: int |
| 8763 | offset: Py_off_t |
| 8764 | / |
| 8765 | |
| 8766 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 8767 | |
| 8768 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 8769 | the beginning of the file. The file offset remains unchanged. |
| 8770 | [clinic start generated code]*/ |
| 8771 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8772 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8773 | os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset) |
| 8774 | /*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8775 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8776 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8777 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8778 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8779 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8780 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8781 | errno = EINVAL; |
| 8782 | return posix_error(); |
| 8783 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8784 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8785 | if (buffer == NULL) |
| 8786 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8787 | |
| 8788 | do { |
| 8789 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8790 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8791 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8792 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8793 | Py_END_ALLOW_THREADS |
| 8794 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8795 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8796 | if (n < 0) { |
| 8797 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8798 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8799 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8800 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8801 | _PyBytes_Resize(&buffer, n); |
| 8802 | return buffer; |
| 8803 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8804 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8805 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 8806 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 8807 | /*[clinic input] |
| 8808 | os.preadv -> Py_ssize_t |
| 8809 | |
| 8810 | fd: int |
| 8811 | buffers: object |
| 8812 | offset: Py_off_t |
| 8813 | flags: int = 0 |
| 8814 | / |
| 8815 | |
| 8816 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 8817 | |
| 8818 | Combines the functionality of readv() and pread(). As readv(), it will |
| 8819 | transfer data into each buffer until it is full and then move on to the next |
| 8820 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 8821 | specifies the file offset at which the input operation is to be performed. It |
| 8822 | will return the total number of bytes read (which can be less than the total |
| 8823 | capacity of all the objects). |
| 8824 | |
| 8825 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 8826 | |
| 8827 | - RWF_HIPRI |
| 8828 | - RWF_NOWAIT |
| 8829 | |
| 8830 | Using non-zero flags requires Linux 4.6 or newer. |
| 8831 | [clinic start generated code]*/ |
| 8832 | |
| 8833 | static Py_ssize_t |
| 8834 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 8835 | int flags) |
| 8836 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 8837 | { |
| 8838 | Py_ssize_t cnt, n; |
| 8839 | int async_err = 0; |
| 8840 | struct iovec *iov; |
| 8841 | Py_buffer *buf; |
| 8842 | |
| 8843 | if (!PySequence_Check(buffers)) { |
| 8844 | PyErr_SetString(PyExc_TypeError, |
| 8845 | "preadv2() arg 2 must be a sequence"); |
| 8846 | return -1; |
| 8847 | } |
| 8848 | |
| 8849 | cnt = PySequence_Size(buffers); |
| 8850 | if (cnt < 0) { |
| 8851 | return -1; |
| 8852 | } |
| 8853 | |
| 8854 | #ifndef HAVE_PREADV2 |
| 8855 | if(flags != 0) { |
| 8856 | argument_unavailable_error("preadv2", "flags"); |
| 8857 | return -1; |
| 8858 | } |
| 8859 | #endif |
| 8860 | |
| 8861 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 8862 | return -1; |
| 8863 | } |
| 8864 | #ifdef HAVE_PREADV2 |
| 8865 | do { |
| 8866 | Py_BEGIN_ALLOW_THREADS |
| 8867 | _Py_BEGIN_SUPPRESS_IPH |
| 8868 | n = preadv2(fd, iov, cnt, offset, flags); |
| 8869 | _Py_END_SUPPRESS_IPH |
| 8870 | Py_END_ALLOW_THREADS |
| 8871 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8872 | #else |
| 8873 | do { |
| 8874 | Py_BEGIN_ALLOW_THREADS |
| 8875 | _Py_BEGIN_SUPPRESS_IPH |
| 8876 | n = preadv(fd, iov, cnt, offset); |
| 8877 | _Py_END_SUPPRESS_IPH |
| 8878 | Py_END_ALLOW_THREADS |
| 8879 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8880 | #endif |
| 8881 | |
| 8882 | iov_cleanup(iov, buf, cnt); |
| 8883 | if (n < 0) { |
| 8884 | if (!async_err) { |
| 8885 | posix_error(); |
| 8886 | } |
| 8887 | return -1; |
| 8888 | } |
| 8889 | |
| 8890 | return n; |
| 8891 | } |
| 8892 | #endif /* HAVE_PREADV */ |
| 8893 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8894 | |
| 8895 | /*[clinic input] |
| 8896 | os.write -> Py_ssize_t |
| 8897 | |
| 8898 | fd: int |
| 8899 | data: Py_buffer |
| 8900 | / |
| 8901 | |
| 8902 | Write a bytes object to a file descriptor. |
| 8903 | [clinic start generated code]*/ |
| 8904 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8905 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8906 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 8907 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8908 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8909 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8910 | } |
| 8911 | |
| 8912 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8913 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8914 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8915 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8916 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8917 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8918 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8919 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8920 | static PyObject * |
| 8921 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8922 | { |
| 8923 | int in, out; |
| 8924 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8925 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8926 | off_t offset; |
| 8927 | |
| 8928 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8929 | #ifndef __APPLE__ |
| 8930 | Py_ssize_t len; |
| 8931 | #endif |
| 8932 | PyObject *headers = NULL, *trailers = NULL; |
| 8933 | Py_buffer *hbuf, *tbuf; |
| 8934 | off_t sbytes; |
| 8935 | struct sf_hdtr sf; |
| 8936 | int flags = 0; |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8937 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8938 | static char *keywords[] = {"out", "in", |
| 8939 | "offset", "count", |
| 8940 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8941 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8942 | sf.headers = NULL; |
| 8943 | sf.trailers = NULL; |
| 8944 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8945 | #ifdef __APPLE__ |
| 8946 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8947 | 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] | 8948 | #else |
| 8949 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8950 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8951 | #endif |
| 8952 | &headers, &trailers, &flags)) |
| 8953 | return NULL; |
| 8954 | if (headers != NULL) { |
| 8955 | if (!PySequence_Check(headers)) { |
| 8956 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8957 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8958 | return NULL; |
| 8959 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8960 | Py_ssize_t i = PySequence_Size(headers); |
| 8961 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8962 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8963 | if (i > INT_MAX) { |
| 8964 | PyErr_SetString(PyExc_OverflowError, |
| 8965 | "sendfile() header is too large"); |
| 8966 | return NULL; |
| 8967 | } |
| 8968 | if (i > 0) { |
| 8969 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8970 | if (iov_setup(&(sf.headers), &hbuf, |
| 8971 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8972 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8973 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8974 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 8975 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 8976 | # define OFF_T_MAX 0x7fffffffffffffff |
| 8977 | if (sbytes >= OFF_T_MAX - blen) { |
| 8978 | PyErr_SetString(PyExc_OverflowError, |
| 8979 | "sendfile() header is too large"); |
| 8980 | return NULL; |
| 8981 | } |
| 8982 | sbytes += blen; |
| 8983 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8984 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8985 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8986 | } |
| 8987 | } |
| 8988 | if (trailers != NULL) { |
| 8989 | if (!PySequence_Check(trailers)) { |
| 8990 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8991 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8992 | return NULL; |
| 8993 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8994 | Py_ssize_t i = PySequence_Size(trailers); |
| 8995 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8996 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8997 | if (i > INT_MAX) { |
| 8998 | PyErr_SetString(PyExc_OverflowError, |
| 8999 | "sendfile() trailer is too large"); |
| 9000 | return NULL; |
| 9001 | } |
| 9002 | if (i > 0) { |
| 9003 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9004 | if (iov_setup(&(sf.trailers), &tbuf, |
| 9005 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9006 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9007 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9008 | } |
| 9009 | } |
| 9010 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9011 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9012 | do { |
| 9013 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9014 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9015 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9016 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9017 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9018 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9019 | Py_END_ALLOW_THREADS |
| 9020 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9021 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9022 | |
| 9023 | if (sf.headers != NULL) |
| 9024 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 9025 | if (sf.trailers != NULL) |
| 9026 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 9027 | |
| 9028 | if (ret < 0) { |
| 9029 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 9030 | if (sbytes != 0) { |
| 9031 | // some data has been sent |
| 9032 | goto done; |
| 9033 | } |
| 9034 | else { |
| 9035 | // no data has been sent; upper application is supposed |
| 9036 | // to retry on EAGAIN or EBUSY |
| 9037 | return posix_error(); |
| 9038 | } |
| 9039 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9040 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9041 | } |
| 9042 | goto done; |
| 9043 | |
| 9044 | done: |
| 9045 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 9046 | return Py_BuildValue("l", sbytes); |
| 9047 | #else |
| 9048 | return Py_BuildValue("L", sbytes); |
| 9049 | #endif |
| 9050 | |
| 9051 | #else |
| 9052 | Py_ssize_t count; |
| 9053 | PyObject *offobj; |
| 9054 | static char *keywords[] = {"out", "in", |
| 9055 | "offset", "count", NULL}; |
| 9056 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 9057 | keywords, &out, &in, &offobj, &count)) |
| 9058 | return NULL; |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 9059 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9060 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9061 | do { |
| 9062 | Py_BEGIN_ALLOW_THREADS |
| 9063 | ret = sendfile(out, in, NULL, count); |
| 9064 | Py_END_ALLOW_THREADS |
| 9065 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9066 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9067 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9068 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9069 | } |
| 9070 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9071 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 9072 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9073 | |
| 9074 | do { |
| 9075 | Py_BEGIN_ALLOW_THREADS |
| 9076 | ret = sendfile(out, in, &offset, count); |
| 9077 | Py_END_ALLOW_THREADS |
| 9078 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9079 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9080 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9081 | return Py_BuildValue("n", ret); |
| 9082 | #endif |
| 9083 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9084 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9085 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9086 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9087 | #if defined(__APPLE__) |
| 9088 | /*[clinic input] |
| 9089 | os._fcopyfile |
| 9090 | |
| 9091 | infd: int |
| 9092 | outfd: int |
| 9093 | flags: int |
| 9094 | / |
| 9095 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9096 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9097 | [clinic start generated code]*/ |
| 9098 | |
| 9099 | static PyObject * |
| 9100 | os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags) |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9101 | /*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9102 | { |
| 9103 | int ret; |
| 9104 | |
| 9105 | Py_BEGIN_ALLOW_THREADS |
| 9106 | ret = fcopyfile(infd, outfd, NULL, flags); |
| 9107 | Py_END_ALLOW_THREADS |
| 9108 | if (ret < 0) |
| 9109 | return posix_error(); |
| 9110 | Py_RETURN_NONE; |
| 9111 | } |
| 9112 | #endif |
| 9113 | |
| 9114 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9115 | /*[clinic input] |
| 9116 | os.fstat |
| 9117 | |
| 9118 | fd : int |
| 9119 | |
| 9120 | Perform a stat system call on the given file descriptor. |
| 9121 | |
| 9122 | Like stat(), but for an open file descriptor. |
| 9123 | Equivalent to os.stat(fd). |
| 9124 | [clinic start generated code]*/ |
| 9125 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9126 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9127 | os_fstat_impl(PyObject *module, int fd) |
| 9128 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9129 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9130 | STRUCT_STAT st; |
| 9131 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9132 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9133 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9134 | do { |
| 9135 | Py_BEGIN_ALLOW_THREADS |
| 9136 | res = FSTAT(fd, &st); |
| 9137 | Py_END_ALLOW_THREADS |
| 9138 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9139 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9140 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9141 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9142 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9143 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9144 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9145 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9146 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9147 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9148 | } |
| 9149 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9150 | |
| 9151 | /*[clinic input] |
| 9152 | os.isatty -> bool |
| 9153 | fd: int |
| 9154 | / |
| 9155 | |
| 9156 | Return True if the fd is connected to a terminal. |
| 9157 | |
| 9158 | Return True if the file descriptor is an open file descriptor |
| 9159 | connected to the slave end of a terminal. |
| 9160 | [clinic start generated code]*/ |
| 9161 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9162 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9163 | os_isatty_impl(PyObject *module, int fd) |
| 9164 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9165 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9166 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9167 | _Py_BEGIN_SUPPRESS_IPH |
| 9168 | return_value = isatty(fd); |
| 9169 | _Py_END_SUPPRESS_IPH |
| 9170 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9171 | } |
| 9172 | |
| 9173 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9174 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9175 | /*[clinic input] |
| 9176 | os.pipe |
| 9177 | |
| 9178 | Create a pipe. |
| 9179 | |
| 9180 | Returns a tuple of two file descriptors: |
| 9181 | (read_fd, write_fd) |
| 9182 | [clinic start generated code]*/ |
| 9183 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9184 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9185 | os_pipe_impl(PyObject *module) |
| 9186 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9187 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9188 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9189 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9190 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9191 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9192 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9193 | #else |
| 9194 | int res; |
| 9195 | #endif |
| 9196 | |
| 9197 | #ifdef MS_WINDOWS |
| 9198 | attr.nLength = sizeof(attr); |
| 9199 | attr.lpSecurityDescriptor = NULL; |
| 9200 | attr.bInheritHandle = FALSE; |
| 9201 | |
| 9202 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9203 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9204 | ok = CreatePipe(&read, &write, &attr, 0); |
| 9205 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9206 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 9207 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9208 | if (fds[0] == -1 || fds[1] == -1) { |
| 9209 | CloseHandle(read); |
| 9210 | CloseHandle(write); |
| 9211 | ok = 0; |
| 9212 | } |
| 9213 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9214 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9215 | Py_END_ALLOW_THREADS |
| 9216 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9217 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9218 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9219 | #else |
| 9220 | |
| 9221 | #ifdef HAVE_PIPE2 |
| 9222 | Py_BEGIN_ALLOW_THREADS |
| 9223 | res = pipe2(fds, O_CLOEXEC); |
| 9224 | Py_END_ALLOW_THREADS |
| 9225 | |
| 9226 | if (res != 0 && errno == ENOSYS) |
| 9227 | { |
| 9228 | #endif |
| 9229 | Py_BEGIN_ALLOW_THREADS |
| 9230 | res = pipe(fds); |
| 9231 | Py_END_ALLOW_THREADS |
| 9232 | |
| 9233 | if (res == 0) { |
| 9234 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 9235 | close(fds[0]); |
| 9236 | close(fds[1]); |
| 9237 | return NULL; |
| 9238 | } |
| 9239 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 9240 | close(fds[0]); |
| 9241 | close(fds[1]); |
| 9242 | return NULL; |
| 9243 | } |
| 9244 | } |
| 9245 | #ifdef HAVE_PIPE2 |
| 9246 | } |
| 9247 | #endif |
| 9248 | |
| 9249 | if (res != 0) |
| 9250 | return PyErr_SetFromErrno(PyExc_OSError); |
| 9251 | #endif /* !MS_WINDOWS */ |
| 9252 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9253 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9254 | #endif /* HAVE_PIPE */ |
| 9255 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9256 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9257 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9258 | /*[clinic input] |
| 9259 | os.pipe2 |
| 9260 | |
| 9261 | flags: int |
| 9262 | / |
| 9263 | |
| 9264 | Create a pipe with flags set atomically. |
| 9265 | |
| 9266 | Returns a tuple of two file descriptors: |
| 9267 | (read_fd, write_fd) |
| 9268 | |
| 9269 | flags can be constructed by ORing together one or more of these values: |
| 9270 | O_NONBLOCK, O_CLOEXEC. |
| 9271 | [clinic start generated code]*/ |
| 9272 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9273 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9274 | os_pipe2_impl(PyObject *module, int flags) |
| 9275 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9276 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9277 | int fds[2]; |
| 9278 | int res; |
| 9279 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9280 | res = pipe2(fds, flags); |
| 9281 | if (res != 0) |
| 9282 | return posix_error(); |
| 9283 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 9284 | } |
| 9285 | #endif /* HAVE_PIPE2 */ |
| 9286 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9287 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9288 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9289 | /*[clinic input] |
| 9290 | os.writev -> Py_ssize_t |
| 9291 | fd: int |
| 9292 | buffers: object |
| 9293 | / |
| 9294 | |
| 9295 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 9296 | |
| 9297 | Returns the total number of bytes written. |
| 9298 | buffers must be a sequence of bytes-like objects. |
| 9299 | [clinic start generated code]*/ |
| 9300 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9301 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9302 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 9303 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9304 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9305 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9306 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9307 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9308 | struct iovec *iov; |
| 9309 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9310 | |
| 9311 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9312 | PyErr_SetString(PyExc_TypeError, |
| 9313 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9314 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9315 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9316 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9317 | if (cnt < 0) |
| 9318 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9319 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9320 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9321 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9322 | } |
| 9323 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9324 | do { |
| 9325 | Py_BEGIN_ALLOW_THREADS |
| 9326 | result = writev(fd, iov, cnt); |
| 9327 | Py_END_ALLOW_THREADS |
| 9328 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9329 | |
| 9330 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9331 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9332 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9333 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9334 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9335 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9336 | #endif /* HAVE_WRITEV */ |
| 9337 | |
| 9338 | |
| 9339 | #ifdef HAVE_PWRITE |
| 9340 | /*[clinic input] |
| 9341 | os.pwrite -> Py_ssize_t |
| 9342 | |
| 9343 | fd: int |
| 9344 | buffer: Py_buffer |
| 9345 | offset: Py_off_t |
| 9346 | / |
| 9347 | |
| 9348 | Write bytes to a file descriptor starting at a particular offset. |
| 9349 | |
| 9350 | Write buffer to fd, starting at offset bytes from the beginning of |
| 9351 | the file. Returns the number of bytes writte. Does not change the |
| 9352 | current file offset. |
| 9353 | [clinic start generated code]*/ |
| 9354 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9355 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9356 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 9357 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9358 | { |
| 9359 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9360 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9361 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9362 | do { |
| 9363 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9364 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9365 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9366 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9367 | Py_END_ALLOW_THREADS |
| 9368 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9369 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9370 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9371 | posix_error(); |
| 9372 | return size; |
| 9373 | } |
| 9374 | #endif /* HAVE_PWRITE */ |
| 9375 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9376 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9377 | /*[clinic input] |
| 9378 | os.pwritev -> Py_ssize_t |
| 9379 | |
| 9380 | fd: int |
| 9381 | buffers: object |
| 9382 | offset: Py_off_t |
| 9383 | flags: int = 0 |
| 9384 | / |
| 9385 | |
| 9386 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 9387 | |
| 9388 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 9389 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 9390 | buffer is written before proceeding to second, and so on. The operating system may |
| 9391 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 9392 | This function writes the contents of each object to the file descriptor and returns |
| 9393 | the total number of bytes written. |
| 9394 | |
| 9395 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9396 | |
| 9397 | - RWF_DSYNC |
| 9398 | - RWF_SYNC |
| 9399 | |
| 9400 | Using non-zero flags requires Linux 4.7 or newer. |
| 9401 | [clinic start generated code]*/ |
| 9402 | |
| 9403 | static Py_ssize_t |
| 9404 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9405 | int flags) |
| 9406 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/ |
| 9407 | { |
| 9408 | Py_ssize_t cnt; |
| 9409 | Py_ssize_t result; |
| 9410 | int async_err = 0; |
| 9411 | struct iovec *iov; |
| 9412 | Py_buffer *buf; |
| 9413 | |
| 9414 | if (!PySequence_Check(buffers)) { |
| 9415 | PyErr_SetString(PyExc_TypeError, |
| 9416 | "pwritev() arg 2 must be a sequence"); |
| 9417 | return -1; |
| 9418 | } |
| 9419 | |
| 9420 | cnt = PySequence_Size(buffers); |
| 9421 | if (cnt < 0) { |
| 9422 | return -1; |
| 9423 | } |
| 9424 | |
| 9425 | #ifndef HAVE_PWRITEV2 |
| 9426 | if(flags != 0) { |
| 9427 | argument_unavailable_error("pwritev2", "flags"); |
| 9428 | return -1; |
| 9429 | } |
| 9430 | #endif |
| 9431 | |
| 9432 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9433 | return -1; |
| 9434 | } |
| 9435 | #ifdef HAVE_PWRITEV2 |
| 9436 | do { |
| 9437 | Py_BEGIN_ALLOW_THREADS |
| 9438 | _Py_BEGIN_SUPPRESS_IPH |
| 9439 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9440 | _Py_END_SUPPRESS_IPH |
| 9441 | Py_END_ALLOW_THREADS |
| 9442 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9443 | #else |
| 9444 | do { |
| 9445 | Py_BEGIN_ALLOW_THREADS |
| 9446 | _Py_BEGIN_SUPPRESS_IPH |
| 9447 | result = pwritev(fd, iov, cnt, offset); |
| 9448 | _Py_END_SUPPRESS_IPH |
| 9449 | Py_END_ALLOW_THREADS |
| 9450 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9451 | #endif |
| 9452 | |
| 9453 | iov_cleanup(iov, buf, cnt); |
| 9454 | if (result < 0) { |
| 9455 | if (!async_err) { |
| 9456 | posix_error(); |
| 9457 | } |
| 9458 | return -1; |
| 9459 | } |
| 9460 | |
| 9461 | return result; |
| 9462 | } |
| 9463 | #endif /* HAVE_PWRITEV */ |
| 9464 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9465 | #ifdef HAVE_COPY_FILE_RANGE |
| 9466 | /*[clinic input] |
| 9467 | |
| 9468 | os.copy_file_range |
| 9469 | src: int |
| 9470 | Source file descriptor. |
| 9471 | dst: int |
| 9472 | Destination file descriptor. |
| 9473 | count: Py_ssize_t |
| 9474 | Number of bytes to copy. |
| 9475 | offset_src: object = None |
| 9476 | Starting offset in src. |
| 9477 | offset_dst: object = None |
| 9478 | Starting offset in dst. |
| 9479 | |
| 9480 | Copy count bytes from one file descriptor to another. |
| 9481 | |
| 9482 | If offset_src is None, then src is read from the current position; |
| 9483 | respectively for offset_dst. |
| 9484 | [clinic start generated code]*/ |
| 9485 | |
| 9486 | static PyObject * |
| 9487 | os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, |
| 9488 | PyObject *offset_src, PyObject *offset_dst) |
| 9489 | /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/ |
| 9490 | { |
| 9491 | off_t offset_src_val, offset_dst_val; |
| 9492 | off_t *p_offset_src = NULL; |
| 9493 | off_t *p_offset_dst = NULL; |
| 9494 | Py_ssize_t ret; |
| 9495 | int async_err = 0; |
| 9496 | /* The flags argument is provided to allow |
| 9497 | * for future extensions and currently must be to 0. */ |
| 9498 | int flags = 0; |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9499 | |
| 9500 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9501 | if (count < 0) { |
| 9502 | PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); |
| 9503 | return NULL; |
| 9504 | } |
| 9505 | |
| 9506 | if (offset_src != Py_None) { |
| 9507 | if (!Py_off_t_converter(offset_src, &offset_src_val)) { |
| 9508 | return NULL; |
| 9509 | } |
| 9510 | p_offset_src = &offset_src_val; |
| 9511 | } |
| 9512 | |
| 9513 | if (offset_dst != Py_None) { |
| 9514 | if (!Py_off_t_converter(offset_dst, &offset_dst_val)) { |
| 9515 | return NULL; |
| 9516 | } |
| 9517 | p_offset_dst = &offset_dst_val; |
| 9518 | } |
| 9519 | |
| 9520 | do { |
| 9521 | Py_BEGIN_ALLOW_THREADS |
| 9522 | ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags); |
| 9523 | Py_END_ALLOW_THREADS |
| 9524 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9525 | |
| 9526 | if (ret < 0) { |
| 9527 | return (!async_err) ? posix_error() : NULL; |
| 9528 | } |
| 9529 | |
| 9530 | return PyLong_FromSsize_t(ret); |
| 9531 | } |
| 9532 | #endif /* HAVE_COPY_FILE_RANGE*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9533 | |
| 9534 | #ifdef HAVE_MKFIFO |
| 9535 | /*[clinic input] |
| 9536 | os.mkfifo |
| 9537 | |
| 9538 | path: path_t |
| 9539 | mode: int=0o666 |
| 9540 | * |
| 9541 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9542 | |
| 9543 | Create a "fifo" (a POSIX named pipe). |
| 9544 | |
| 9545 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9546 | and path should be relative; path will then be relative to that directory. |
| 9547 | dir_fd may not be implemented on your platform. |
| 9548 | If it is unavailable, using it will raise a NotImplementedError. |
| 9549 | [clinic start generated code]*/ |
| 9550 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9551 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9552 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 9553 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9554 | { |
| 9555 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9556 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9557 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9558 | do { |
| 9559 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9560 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9561 | if (dir_fd != DEFAULT_DIR_FD) |
| 9562 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 9563 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9564 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9565 | result = mkfifo(path->narrow, mode); |
| 9566 | Py_END_ALLOW_THREADS |
| 9567 | } while (result != 0 && errno == EINTR && |
| 9568 | !(async_err = PyErr_CheckSignals())); |
| 9569 | if (result != 0) |
| 9570 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9571 | |
| 9572 | Py_RETURN_NONE; |
| 9573 | } |
| 9574 | #endif /* HAVE_MKFIFO */ |
| 9575 | |
| 9576 | |
| 9577 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 9578 | /*[clinic input] |
| 9579 | os.mknod |
| 9580 | |
| 9581 | path: path_t |
| 9582 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9583 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9584 | * |
| 9585 | dir_fd: dir_fd(requires='mknodat')=None |
| 9586 | |
| 9587 | Create a node in the file system. |
| 9588 | |
| 9589 | Create a node in the file system (file, device special file or named pipe) |
| 9590 | at path. mode specifies both the permissions to use and the |
| 9591 | type of node to be created, being combined (bitwise OR) with one of |
| 9592 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 9593 | device defines the newly created device special file (probably using |
| 9594 | os.makedev()). Otherwise device is ignored. |
| 9595 | |
| 9596 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9597 | and path should be relative; path will then be relative to that directory. |
| 9598 | dir_fd may not be implemented on your platform. |
| 9599 | If it is unavailable, using it will raise a NotImplementedError. |
| 9600 | [clinic start generated code]*/ |
| 9601 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9602 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9603 | 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] | 9604 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9605 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9606 | { |
| 9607 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9608 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9609 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9610 | do { |
| 9611 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9612 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9613 | if (dir_fd != DEFAULT_DIR_FD) |
| 9614 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 9615 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9616 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9617 | result = mknod(path->narrow, mode, device); |
| 9618 | Py_END_ALLOW_THREADS |
| 9619 | } while (result != 0 && errno == EINTR && |
| 9620 | !(async_err = PyErr_CheckSignals())); |
| 9621 | if (result != 0) |
| 9622 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9623 | |
| 9624 | Py_RETURN_NONE; |
| 9625 | } |
| 9626 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 9627 | |
| 9628 | |
| 9629 | #ifdef HAVE_DEVICE_MACROS |
| 9630 | /*[clinic input] |
| 9631 | os.major -> unsigned_int |
| 9632 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9633 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9634 | / |
| 9635 | |
| 9636 | Extracts a device major number from a raw device number. |
| 9637 | [clinic start generated code]*/ |
| 9638 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9639 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9640 | os_major_impl(PyObject *module, dev_t device) |
| 9641 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9642 | { |
| 9643 | return major(device); |
| 9644 | } |
| 9645 | |
| 9646 | |
| 9647 | /*[clinic input] |
| 9648 | os.minor -> unsigned_int |
| 9649 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9650 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9651 | / |
| 9652 | |
| 9653 | Extracts a device minor number from a raw device number. |
| 9654 | [clinic start generated code]*/ |
| 9655 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9656 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9657 | os_minor_impl(PyObject *module, dev_t device) |
| 9658 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9659 | { |
| 9660 | return minor(device); |
| 9661 | } |
| 9662 | |
| 9663 | |
| 9664 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9665 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9666 | |
| 9667 | major: int |
| 9668 | minor: int |
| 9669 | / |
| 9670 | |
| 9671 | Composes a raw device number from the major and minor device numbers. |
| 9672 | [clinic start generated code]*/ |
| 9673 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9674 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9675 | os_makedev_impl(PyObject *module, int major, int minor) |
| 9676 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9677 | { |
| 9678 | return makedev(major, minor); |
| 9679 | } |
| 9680 | #endif /* HAVE_DEVICE_MACROS */ |
| 9681 | |
| 9682 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9683 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9684 | /*[clinic input] |
| 9685 | os.ftruncate |
| 9686 | |
| 9687 | fd: int |
| 9688 | length: Py_off_t |
| 9689 | / |
| 9690 | |
| 9691 | Truncate a file, specified by file descriptor, to a specific length. |
| 9692 | [clinic start generated code]*/ |
| 9693 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9694 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9695 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 9696 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9697 | { |
| 9698 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9699 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9700 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 9701 | if (PySys_Audit("os.truncate", "in", fd, length) < 0) { |
| 9702 | return NULL; |
| 9703 | } |
| 9704 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9705 | do { |
| 9706 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9707 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9708 | #ifdef MS_WINDOWS |
| 9709 | result = _chsize_s(fd, length); |
| 9710 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9711 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9712 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9713 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9714 | Py_END_ALLOW_THREADS |
| 9715 | } while (result != 0 && errno == EINTR && |
| 9716 | !(async_err = PyErr_CheckSignals())); |
| 9717 | if (result != 0) |
| 9718 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9719 | Py_RETURN_NONE; |
| 9720 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9721 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9722 | |
| 9723 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9724 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9725 | /*[clinic input] |
| 9726 | os.truncate |
| 9727 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 9728 | length: Py_off_t |
| 9729 | |
| 9730 | Truncate a file, specified by path, to a specific length. |
| 9731 | |
| 9732 | On some platforms, path may also be specified as an open file descriptor. |
| 9733 | If this functionality is unavailable, using it raises an exception. |
| 9734 | [clinic start generated code]*/ |
| 9735 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9736 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9737 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 9738 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9739 | { |
| 9740 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9741 | #ifdef MS_WINDOWS |
| 9742 | int fd; |
| 9743 | #endif |
| 9744 | |
| 9745 | if (path->fd != -1) |
| 9746 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9747 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 9748 | if (PySys_Audit("os.truncate", "On", path->object, length) < 0) { |
| 9749 | return NULL; |
| 9750 | } |
| 9751 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9752 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9753 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9754 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 9755 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 9756 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9757 | result = -1; |
| 9758 | else { |
| 9759 | result = _chsize_s(fd, length); |
| 9760 | close(fd); |
| 9761 | if (result < 0) |
| 9762 | errno = result; |
| 9763 | } |
| 9764 | #else |
| 9765 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9766 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9767 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9768 | Py_END_ALLOW_THREADS |
| 9769 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 9770 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9771 | |
| 9772 | Py_RETURN_NONE; |
| 9773 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9774 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9775 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9776 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9777 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 9778 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 9779 | defined, which is the case in Python on AIX. AIX bug report: |
| 9780 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 9781 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 9782 | # define POSIX_FADVISE_AIX_BUG |
| 9783 | #endif |
| 9784 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9785 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9786 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9787 | /*[clinic input] |
| 9788 | os.posix_fallocate |
| 9789 | |
| 9790 | fd: int |
| 9791 | offset: Py_off_t |
| 9792 | length: Py_off_t |
| 9793 | / |
| 9794 | |
| 9795 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 9796 | |
| 9797 | Ensure that the file specified by fd encompasses a range of bytes |
| 9798 | starting at offset bytes from the beginning and continuing for length bytes. |
| 9799 | [clinic start generated code]*/ |
| 9800 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9801 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9802 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9803 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9804 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9805 | { |
| 9806 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9807 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9808 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9809 | do { |
| 9810 | Py_BEGIN_ALLOW_THREADS |
| 9811 | result = posix_fallocate(fd, offset, length); |
| 9812 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9813 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9814 | |
| 9815 | if (result == 0) |
| 9816 | Py_RETURN_NONE; |
| 9817 | |
| 9818 | if (async_err) |
| 9819 | return NULL; |
| 9820 | |
| 9821 | errno = result; |
| 9822 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9823 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9824 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9825 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9826 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9827 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9828 | /*[clinic input] |
| 9829 | os.posix_fadvise |
| 9830 | |
| 9831 | fd: int |
| 9832 | offset: Py_off_t |
| 9833 | length: Py_off_t |
| 9834 | advice: int |
| 9835 | / |
| 9836 | |
| 9837 | Announce an intention to access data in a specific pattern. |
| 9838 | |
| 9839 | Announce an intention to access data in a specific pattern, thus allowing |
| 9840 | the kernel to make optimizations. |
| 9841 | The advice applies to the region of the file specified by fd starting at |
| 9842 | offset and continuing for length bytes. |
| 9843 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 9844 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 9845 | POSIX_FADV_DONTNEED. |
| 9846 | [clinic start generated code]*/ |
| 9847 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9848 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9849 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9850 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9851 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9852 | { |
| 9853 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9854 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9855 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9856 | do { |
| 9857 | Py_BEGIN_ALLOW_THREADS |
| 9858 | result = posix_fadvise(fd, offset, length, advice); |
| 9859 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9860 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9861 | |
| 9862 | if (result == 0) |
| 9863 | Py_RETURN_NONE; |
| 9864 | |
| 9865 | if (async_err) |
| 9866 | return NULL; |
| 9867 | |
| 9868 | errno = result; |
| 9869 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9870 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9871 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9872 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9873 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9874 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 9875 | /* Save putenv() parameters as values here, so we can collect them when they |
| 9876 | * get re-set with another call for the same key. */ |
| 9877 | static PyObject *posix_putenv_garbage; |
| 9878 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9879 | static void |
| 9880 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9881 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9882 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 9883 | * this will cause previous value to be collected. This has to |
| 9884 | * happen after the real putenv() call because the old value |
| 9885 | * was still accessible until then. */ |
| 9886 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 9887 | /* really not much we can do; just leak */ |
| 9888 | PyErr_Clear(); |
| 9889 | else |
| 9890 | Py_DECREF(value); |
| 9891 | } |
| 9892 | |
| 9893 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 9894 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9895 | /*[clinic input] |
| 9896 | os.putenv |
| 9897 | |
| 9898 | name: unicode |
| 9899 | value: unicode |
| 9900 | / |
| 9901 | |
| 9902 | Change or add an environment variable. |
| 9903 | [clinic start generated code]*/ |
| 9904 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9905 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9906 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 9907 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9908 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 9909 | const wchar_t *env; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9910 | Py_ssize_t size; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9911 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9912 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 9913 | defining hidden environment variables. */ |
| 9914 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 9915 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 9916 | { |
| 9917 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 9918 | return NULL; |
| 9919 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9920 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 9921 | if (unicode == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9922 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9923 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9924 | |
| 9925 | env = PyUnicode_AsUnicodeAndSize(unicode, &size); |
| 9926 | if (env == NULL) |
| 9927 | goto error; |
| 9928 | if (size > _MAX_ENV) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9929 | PyErr_Format(PyExc_ValueError, |
| 9930 | "the environment variable is longer than %u characters", |
| 9931 | _MAX_ENV); |
| 9932 | goto error; |
| 9933 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9934 | if (wcslen(env) != (size_t)size) { |
| 9935 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9936 | goto error; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9937 | } |
| 9938 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9939 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9940 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9941 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9942 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9943 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9944 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9945 | Py_RETURN_NONE; |
| 9946 | |
| 9947 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9948 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9949 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9950 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9951 | #else /* MS_WINDOWS */ |
| 9952 | /*[clinic input] |
| 9953 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9954 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9955 | name: FSConverter |
| 9956 | value: FSConverter |
| 9957 | / |
| 9958 | |
| 9959 | Change or add an environment variable. |
| 9960 | [clinic start generated code]*/ |
| 9961 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9962 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9963 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 9964 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9965 | { |
| 9966 | PyObject *bytes = NULL; |
| 9967 | char *env; |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9968 | const char *name_string = PyBytes_AS_STRING(name); |
| 9969 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9970 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9971 | if (strchr(name_string, '=') != NULL) { |
| 9972 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 9973 | return NULL; |
| 9974 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9975 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 9976 | if (bytes == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9977 | return NULL; |
| 9978 | } |
| 9979 | |
| 9980 | env = PyBytes_AS_STRING(bytes); |
| 9981 | if (putenv(env)) { |
| 9982 | Py_DECREF(bytes); |
| 9983 | return posix_error(); |
| 9984 | } |
| 9985 | |
| 9986 | posix_putenv_garbage_setitem(name, bytes); |
| 9987 | Py_RETURN_NONE; |
| 9988 | } |
| 9989 | #endif /* MS_WINDOWS */ |
| 9990 | #endif /* HAVE_PUTENV */ |
| 9991 | |
| 9992 | |
| 9993 | #ifdef HAVE_UNSETENV |
| 9994 | /*[clinic input] |
| 9995 | os.unsetenv |
| 9996 | name: FSConverter |
| 9997 | / |
| 9998 | |
| 9999 | Delete an environment variable. |
| 10000 | [clinic start generated code]*/ |
| 10001 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10002 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10003 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10004 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10005 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10006 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10007 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10008 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10009 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10010 | #ifdef HAVE_BROKEN_UNSETENV |
| 10011 | unsetenv(PyBytes_AS_STRING(name)); |
| 10012 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 10013 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10014 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10015 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10016 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10017 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10018 | /* Remove the key from posix_putenv_garbage; |
| 10019 | * this will cause it to be collected. This has to |
| 10020 | * happen after the real unsetenv() call because the |
| 10021 | * old value was still accessible until then. |
| 10022 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 10023 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10024 | /* really not much we can do; just leak */ |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 10025 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 10026 | return NULL; |
| 10027 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10028 | PyErr_Clear(); |
| 10029 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10030 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10031 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10032 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10033 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10034 | |
| 10035 | /*[clinic input] |
| 10036 | os.strerror |
| 10037 | |
| 10038 | code: int |
| 10039 | / |
| 10040 | |
| 10041 | Translate an error code to a message string. |
| 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_strerror_impl(PyObject *module, int code) |
| 10046 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10047 | { |
| 10048 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10049 | if (message == NULL) { |
| 10050 | PyErr_SetString(PyExc_ValueError, |
| 10051 | "strerror() argument out of range"); |
| 10052 | return NULL; |
| 10053 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 10054 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10055 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10056 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10057 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10058 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10059 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10060 | /*[clinic input] |
| 10061 | os.WCOREDUMP -> bool |
| 10062 | |
| 10063 | status: int |
| 10064 | / |
| 10065 | |
| 10066 | Return True if the process returning status was dumped to a core file. |
| 10067 | [clinic start generated code]*/ |
| 10068 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10069 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10070 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 10071 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10072 | { |
| 10073 | WAIT_TYPE wait_status; |
| 10074 | WAIT_STATUS_INT(wait_status) = status; |
| 10075 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10076 | } |
| 10077 | #endif /* WCOREDUMP */ |
| 10078 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10079 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10080 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10081 | /*[clinic input] |
| 10082 | os.WIFCONTINUED -> bool |
| 10083 | |
| 10084 | status: int |
| 10085 | |
| 10086 | Return True if a particular process was continued from a job control stop. |
| 10087 | |
| 10088 | Return True if the process returning status was continued from a |
| 10089 | job control stop. |
| 10090 | [clinic start generated code]*/ |
| 10091 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10092 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10093 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 10094 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10095 | { |
| 10096 | WAIT_TYPE wait_status; |
| 10097 | WAIT_STATUS_INT(wait_status) = status; |
| 10098 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10099 | } |
| 10100 | #endif /* WIFCONTINUED */ |
| 10101 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10102 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10103 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10104 | /*[clinic input] |
| 10105 | os.WIFSTOPPED -> bool |
| 10106 | |
| 10107 | status: int |
| 10108 | |
| 10109 | Return True if the process returning status was stopped. |
| 10110 | [clinic start generated code]*/ |
| 10111 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10112 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10113 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 10114 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10115 | { |
| 10116 | WAIT_TYPE wait_status; |
| 10117 | WAIT_STATUS_INT(wait_status) = status; |
| 10118 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10119 | } |
| 10120 | #endif /* WIFSTOPPED */ |
| 10121 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10122 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10123 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10124 | /*[clinic input] |
| 10125 | os.WIFSIGNALED -> bool |
| 10126 | |
| 10127 | status: int |
| 10128 | |
| 10129 | Return True if the process returning status was terminated by a signal. |
| 10130 | [clinic start generated code]*/ |
| 10131 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10132 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10133 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 10134 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10135 | { |
| 10136 | WAIT_TYPE wait_status; |
| 10137 | WAIT_STATUS_INT(wait_status) = status; |
| 10138 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10139 | } |
| 10140 | #endif /* WIFSIGNALED */ |
| 10141 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10142 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10143 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10144 | /*[clinic input] |
| 10145 | os.WIFEXITED -> bool |
| 10146 | |
| 10147 | status: int |
| 10148 | |
| 10149 | Return True if the process returning status exited via the exit() system call. |
| 10150 | [clinic start generated code]*/ |
| 10151 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10152 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10153 | os_WIFEXITED_impl(PyObject *module, int status) |
| 10154 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10155 | { |
| 10156 | WAIT_TYPE wait_status; |
| 10157 | WAIT_STATUS_INT(wait_status) = status; |
| 10158 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10159 | } |
| 10160 | #endif /* WIFEXITED */ |
| 10161 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10162 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 10163 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10164 | /*[clinic input] |
| 10165 | os.WEXITSTATUS -> int |
| 10166 | |
| 10167 | status: int |
| 10168 | |
| 10169 | Return the process return code from status. |
| 10170 | [clinic start generated code]*/ |
| 10171 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10172 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10173 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 10174 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10175 | { |
| 10176 | WAIT_TYPE wait_status; |
| 10177 | WAIT_STATUS_INT(wait_status) = status; |
| 10178 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10179 | } |
| 10180 | #endif /* WEXITSTATUS */ |
| 10181 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10182 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10183 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10184 | /*[clinic input] |
| 10185 | os.WTERMSIG -> int |
| 10186 | |
| 10187 | status: int |
| 10188 | |
| 10189 | Return the signal that terminated the process that provided the status value. |
| 10190 | [clinic start generated code]*/ |
| 10191 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10192 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10193 | os_WTERMSIG_impl(PyObject *module, int status) |
| 10194 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10195 | { |
| 10196 | WAIT_TYPE wait_status; |
| 10197 | WAIT_STATUS_INT(wait_status) = status; |
| 10198 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10199 | } |
| 10200 | #endif /* WTERMSIG */ |
| 10201 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10202 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10203 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10204 | /*[clinic input] |
| 10205 | os.WSTOPSIG -> int |
| 10206 | |
| 10207 | status: int |
| 10208 | |
| 10209 | Return the signal that stopped the process that provided the status value. |
| 10210 | [clinic start generated code]*/ |
| 10211 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10212 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10213 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 10214 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10215 | { |
| 10216 | WAIT_TYPE wait_status; |
| 10217 | WAIT_STATUS_INT(wait_status) = status; |
| 10218 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10219 | } |
| 10220 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10221 | #endif /* HAVE_SYS_WAIT_H */ |
| 10222 | |
| 10223 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10224 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 10225 | #ifdef _SCO_DS |
| 10226 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 10227 | needed definitions in sys/statvfs.h */ |
| 10228 | #define _SVID3 |
| 10229 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10230 | #include <sys/statvfs.h> |
| 10231 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10232 | static PyObject* |
| 10233 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 10234 | PyObject *v = PyStructSequence_New(StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10235 | if (v == NULL) |
| 10236 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10237 | |
| 10238 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10239 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10240 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10241 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 10242 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 10243 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 10244 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 10245 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 10246 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 10247 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10248 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10249 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10250 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10251 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10252 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10253 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10254 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10255 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10256 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10257 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10258 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10259 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10260 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10261 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10262 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10263 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10264 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10265 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10266 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10267 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 10268 | * (issue #32390). */ |
| 10269 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 10270 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 10271 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 10272 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10273 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 10274 | if (PyErr_Occurred()) { |
| 10275 | Py_DECREF(v); |
| 10276 | return NULL; |
| 10277 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10278 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10279 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10280 | } |
| 10281 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10282 | |
| 10283 | /*[clinic input] |
| 10284 | os.fstatvfs |
| 10285 | fd: int |
| 10286 | / |
| 10287 | |
| 10288 | Perform an fstatvfs system call on the given fd. |
| 10289 | |
| 10290 | Equivalent to statvfs(fd). |
| 10291 | [clinic start generated code]*/ |
| 10292 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10293 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10294 | os_fstatvfs_impl(PyObject *module, int fd) |
| 10295 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10296 | { |
| 10297 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10298 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10299 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10300 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10301 | do { |
| 10302 | Py_BEGIN_ALLOW_THREADS |
| 10303 | result = fstatvfs(fd, &st); |
| 10304 | Py_END_ALLOW_THREADS |
| 10305 | } while (result != 0 && errno == EINTR && |
| 10306 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10307 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10308 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10309 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10310 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10311 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10312 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10313 | |
| 10314 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10315 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10316 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10317 | /*[clinic input] |
| 10318 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10319 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10320 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 10321 | |
| 10322 | Perform a statvfs system call on the given path. |
| 10323 | |
| 10324 | path may always be specified as a string. |
| 10325 | On some platforms, path may also be specified as an open file descriptor. |
| 10326 | If this functionality is unavailable, using it raises an exception. |
| 10327 | [clinic start generated code]*/ |
| 10328 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10329 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10330 | os_statvfs_impl(PyObject *module, path_t *path) |
| 10331 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10332 | { |
| 10333 | int result; |
| 10334 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10335 | |
| 10336 | Py_BEGIN_ALLOW_THREADS |
| 10337 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10338 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10339 | #ifdef __APPLE__ |
| 10340 | /* handle weak-linking on Mac OS X 10.3 */ |
| 10341 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10342 | fd_specified("statvfs", path->fd); |
| 10343 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10344 | } |
| 10345 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10346 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10347 | } |
| 10348 | else |
| 10349 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10350 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10351 | Py_END_ALLOW_THREADS |
| 10352 | |
| 10353 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10354 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10355 | } |
| 10356 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10357 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10358 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10359 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 10360 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10361 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10362 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10363 | /*[clinic input] |
| 10364 | os._getdiskusage |
| 10365 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10366 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10367 | |
| 10368 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 10369 | [clinic start generated code]*/ |
| 10370 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10371 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10372 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 10373 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10374 | { |
| 10375 | BOOL retval; |
| 10376 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10377 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10378 | |
| 10379 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10380 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10381 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10382 | if (retval == 0) { |
| 10383 | if (GetLastError() == ERROR_DIRECTORY) { |
| 10384 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10385 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10386 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 10387 | if (dir_path == NULL) { |
| 10388 | return PyErr_NoMemory(); |
| 10389 | } |
| 10390 | |
| 10391 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 10392 | |
| 10393 | if (_dirnameW(dir_path) != -1) { |
| 10394 | Py_BEGIN_ALLOW_THREADS |
| 10395 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 10396 | Py_END_ALLOW_THREADS |
| 10397 | } |
| 10398 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 10399 | err = GetLastError(); |
| 10400 | PyMem_Free(dir_path); |
| 10401 | if (retval) { |
| 10402 | goto success; |
| 10403 | } |
| 10404 | } |
| 10405 | return PyErr_SetFromWindowsErr(err); |
| 10406 | } |
| 10407 | |
| 10408 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10409 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 10410 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10411 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10412 | |
| 10413 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10414 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 10415 | * It maps strings representing configuration variable names to |
| 10416 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10417 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10418 | * rarely-used constants. There are three separate tables that use |
| 10419 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10420 | * |
| 10421 | * This code is always included, even if none of the interfaces that |
| 10422 | * need it are included. The #if hackery needed to avoid it would be |
| 10423 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10424 | */ |
| 10425 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10426 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10427 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10428 | }; |
| 10429 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10430 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10431 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10432 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10433 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10434 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10435 | int value = _PyLong_AsInt(arg); |
| 10436 | if (value == -1 && PyErr_Occurred()) |
| 10437 | return 0; |
| 10438 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10439 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10440 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10441 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10442 | /* look up the value in the table using a binary search */ |
| 10443 | size_t lo = 0; |
| 10444 | size_t mid; |
| 10445 | size_t hi = tablesize; |
| 10446 | int cmp; |
| 10447 | const char *confname; |
| 10448 | if (!PyUnicode_Check(arg)) { |
| 10449 | PyErr_SetString(PyExc_TypeError, |
| 10450 | "configuration names must be strings or integers"); |
| 10451 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10452 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10453 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10454 | if (confname == NULL) |
| 10455 | return 0; |
| 10456 | while (lo < hi) { |
| 10457 | mid = (lo + hi) / 2; |
| 10458 | cmp = strcmp(confname, table[mid].name); |
| 10459 | if (cmp < 0) |
| 10460 | hi = mid; |
| 10461 | else if (cmp > 0) |
| 10462 | lo = mid + 1; |
| 10463 | else { |
| 10464 | *valuep = table[mid].value; |
| 10465 | return 1; |
| 10466 | } |
| 10467 | } |
| 10468 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 10469 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10470 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10471 | } |
| 10472 | |
| 10473 | |
| 10474 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 10475 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10476 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10477 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10478 | #endif |
| 10479 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10480 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10481 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10482 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10483 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10484 | #endif |
| 10485 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10486 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10487 | #endif |
| 10488 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10489 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10490 | #endif |
| 10491 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10492 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10493 | #endif |
| 10494 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10495 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10496 | #endif |
| 10497 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10498 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10499 | #endif |
| 10500 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10501 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10502 | #endif |
| 10503 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10504 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10505 | #endif |
| 10506 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10507 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10508 | #endif |
| 10509 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10510 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10511 | #endif |
| 10512 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10513 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10514 | #endif |
| 10515 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10516 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10517 | #endif |
| 10518 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10519 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10520 | #endif |
| 10521 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10522 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10523 | #endif |
| 10524 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10525 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10526 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10527 | #ifdef _PC_ACL_ENABLED |
| 10528 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10529 | #endif |
| 10530 | #ifdef _PC_MIN_HOLE_SIZE |
| 10531 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10532 | #endif |
| 10533 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10534 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10535 | #endif |
| 10536 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10537 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10538 | #endif |
| 10539 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10540 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10541 | #endif |
| 10542 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10543 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10544 | #endif |
| 10545 | #ifdef _PC_REC_XFER_ALIGN |
| 10546 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10547 | #endif |
| 10548 | #ifdef _PC_SYMLINK_MAX |
| 10549 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 10550 | #endif |
| 10551 | #ifdef _PC_XATTR_ENABLED |
| 10552 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 10553 | #endif |
| 10554 | #ifdef _PC_XATTR_EXISTS |
| 10555 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 10556 | #endif |
| 10557 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 10558 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 10559 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10560 | }; |
| 10561 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10562 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10563 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10564 | { |
| 10565 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 10566 | sizeof(posix_constants_pathconf) |
| 10567 | / sizeof(struct constdef)); |
| 10568 | } |
| 10569 | #endif |
| 10570 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10571 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10572 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10573 | /*[clinic input] |
| 10574 | os.fpathconf -> long |
| 10575 | |
| 10576 | fd: int |
| 10577 | name: path_confname |
| 10578 | / |
| 10579 | |
| 10580 | Return the configuration limit name for the file descriptor fd. |
| 10581 | |
| 10582 | If there is no limit, return -1. |
| 10583 | [clinic start generated code]*/ |
| 10584 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10585 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10586 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 10587 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10588 | { |
| 10589 | long limit; |
| 10590 | |
| 10591 | errno = 0; |
| 10592 | limit = fpathconf(fd, name); |
| 10593 | if (limit == -1 && errno != 0) |
| 10594 | posix_error(); |
| 10595 | |
| 10596 | return limit; |
| 10597 | } |
| 10598 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10599 | |
| 10600 | |
| 10601 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10602 | /*[clinic input] |
| 10603 | os.pathconf -> long |
| 10604 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 10605 | name: path_confname |
| 10606 | |
| 10607 | Return the configuration limit name for the file or directory path. |
| 10608 | |
| 10609 | If there is no limit, return -1. |
| 10610 | On some platforms, path may also be specified as an open file descriptor. |
| 10611 | If this functionality is unavailable, using it raises an exception. |
| 10612 | [clinic start generated code]*/ |
| 10613 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10614 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10615 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 10616 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10617 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10618 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10619 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10620 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10621 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10622 | if (path->fd != -1) |
| 10623 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10624 | else |
| 10625 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10626 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10627 | if (limit == -1 && errno != 0) { |
| 10628 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 10629 | /* could be a path or name problem */ |
| 10630 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10631 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10632 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10633 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10634 | |
| 10635 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10636 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10637 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10638 | |
| 10639 | #ifdef HAVE_CONFSTR |
| 10640 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10641 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10642 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10643 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10644 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10645 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10646 | #endif |
| 10647 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10648 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10649 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10650 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10651 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10652 | #endif |
| 10653 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10654 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10655 | #endif |
| 10656 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10657 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10658 | #endif |
| 10659 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10660 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10661 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10662 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10663 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10664 | #endif |
| 10665 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10666 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10667 | #endif |
| 10668 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10669 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10670 | #endif |
| 10671 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10672 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10673 | #endif |
| 10674 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10675 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10676 | #endif |
| 10677 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10678 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10679 | #endif |
| 10680 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10681 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10682 | #endif |
| 10683 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10684 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10685 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10686 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10687 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10688 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10689 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10690 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10691 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10692 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10693 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10694 | #endif |
| 10695 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10696 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10697 | #endif |
| 10698 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10699 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10700 | #endif |
| 10701 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10702 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10703 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10704 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10705 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10706 | #endif |
| 10707 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10708 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10709 | #endif |
| 10710 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10711 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10712 | #endif |
| 10713 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10714 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10715 | #endif |
| 10716 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10717 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10718 | #endif |
| 10719 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10720 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10721 | #endif |
| 10722 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10723 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10724 | #endif |
| 10725 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10726 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10727 | #endif |
| 10728 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10729 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10730 | #endif |
| 10731 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10732 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10733 | #endif |
| 10734 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10735 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10736 | #endif |
| 10737 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10738 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10739 | #endif |
| 10740 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10741 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10742 | #endif |
| 10743 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10744 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10745 | #endif |
| 10746 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10747 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10748 | #endif |
| 10749 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10750 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10751 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10752 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10753 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10754 | #endif |
| 10755 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10756 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10757 | #endif |
| 10758 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10759 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10760 | #endif |
| 10761 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10762 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10763 | #endif |
| 10764 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10765 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10766 | #endif |
| 10767 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10768 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10769 | #endif |
| 10770 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10771 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10772 | #endif |
| 10773 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10774 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10775 | #endif |
| 10776 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10777 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10778 | #endif |
| 10779 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10780 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10781 | #endif |
| 10782 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10783 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10784 | #endif |
| 10785 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10786 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10787 | #endif |
| 10788 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10789 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10790 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10791 | }; |
| 10792 | |
| 10793 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10794 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10795 | { |
| 10796 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 10797 | sizeof(posix_constants_confstr) |
| 10798 | / sizeof(struct constdef)); |
| 10799 | } |
| 10800 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10801 | |
| 10802 | /*[clinic input] |
| 10803 | os.confstr |
| 10804 | |
| 10805 | name: confstr_confname |
| 10806 | / |
| 10807 | |
| 10808 | Return a string-valued system configuration variable. |
| 10809 | [clinic start generated code]*/ |
| 10810 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10811 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10812 | os_confstr_impl(PyObject *module, int name) |
| 10813 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10814 | { |
| 10815 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10816 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10817 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10818 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10819 | errno = 0; |
| 10820 | len = confstr(name, buffer, sizeof(buffer)); |
| 10821 | if (len == 0) { |
| 10822 | if (errno) { |
| 10823 | posix_error(); |
| 10824 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10825 | } |
| 10826 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10827 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10828 | } |
| 10829 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10830 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10831 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 10832 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10833 | char *buf = PyMem_Malloc(len); |
| 10834 | if (buf == NULL) |
| 10835 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 10836 | len2 = confstr(name, buf, len); |
| 10837 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 10838 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10839 | PyMem_Free(buf); |
| 10840 | } |
| 10841 | else |
| 10842 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10843 | return result; |
| 10844 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10845 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10846 | |
| 10847 | |
| 10848 | #ifdef HAVE_SYSCONF |
| 10849 | static struct constdef posix_constants_sysconf[] = { |
| 10850 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10851 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10852 | #endif |
| 10853 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10854 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10855 | #endif |
| 10856 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10857 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10858 | #endif |
| 10859 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10860 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10861 | #endif |
| 10862 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10863 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10864 | #endif |
| 10865 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10866 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10867 | #endif |
| 10868 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10869 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10870 | #endif |
| 10871 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10872 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10873 | #endif |
| 10874 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10875 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10876 | #endif |
| 10877 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10878 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10879 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10880 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10881 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10882 | #endif |
| 10883 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10884 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10885 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10886 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10887 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10888 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10889 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10890 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10891 | #endif |
| 10892 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10893 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10894 | #endif |
| 10895 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10896 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10897 | #endif |
| 10898 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10899 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10900 | #endif |
| 10901 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10902 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10903 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10904 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10905 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10906 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10907 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10908 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10909 | #endif |
| 10910 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10911 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10912 | #endif |
| 10913 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10914 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10915 | #endif |
| 10916 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10917 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10918 | #endif |
| 10919 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10920 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10921 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10922 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10923 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10924 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10925 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10926 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10927 | #endif |
| 10928 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10929 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10930 | #endif |
| 10931 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10932 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10933 | #endif |
| 10934 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10935 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10936 | #endif |
| 10937 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10938 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10939 | #endif |
| 10940 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10941 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10942 | #endif |
| 10943 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10944 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10945 | #endif |
| 10946 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10947 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10948 | #endif |
| 10949 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10950 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10951 | #endif |
| 10952 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10953 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10954 | #endif |
| 10955 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10956 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10957 | #endif |
| 10958 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10959 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10960 | #endif |
| 10961 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10962 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10963 | #endif |
| 10964 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10965 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10966 | #endif |
| 10967 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10968 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10969 | #endif |
| 10970 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10971 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10972 | #endif |
| 10973 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10974 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10975 | #endif |
| 10976 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10977 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10978 | #endif |
| 10979 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10980 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10981 | #endif |
| 10982 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10983 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10984 | #endif |
| 10985 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10986 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10987 | #endif |
| 10988 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10989 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10990 | #endif |
| 10991 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10992 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10993 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10994 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10995 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10996 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10997 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10998 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10999 | #endif |
| 11000 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11001 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11002 | #endif |
| 11003 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11004 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11005 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11006 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11007 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11008 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11009 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11010 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11011 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11012 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11013 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11014 | #endif |
| 11015 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11016 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11017 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11018 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11019 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11020 | #endif |
| 11021 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11022 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11023 | #endif |
| 11024 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11025 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11026 | #endif |
| 11027 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11028 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11029 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11030 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11031 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11032 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11033 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11034 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11035 | #endif |
| 11036 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11037 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11038 | #endif |
| 11039 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11040 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11041 | #endif |
| 11042 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11043 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11044 | #endif |
| 11045 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11046 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11047 | #endif |
| 11048 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11049 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11050 | #endif |
| 11051 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11052 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11053 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11054 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11055 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11056 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11057 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11058 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11059 | #endif |
| 11060 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11061 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11062 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11063 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11064 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11065 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11066 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11067 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11068 | #endif |
| 11069 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11070 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11071 | #endif |
| 11072 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11073 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11074 | #endif |
| 11075 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11076 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11077 | #endif |
| 11078 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11079 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11080 | #endif |
| 11081 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11082 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11083 | #endif |
| 11084 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11085 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11086 | #endif |
| 11087 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11088 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11089 | #endif |
| 11090 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11091 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11092 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11093 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11094 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11095 | #endif |
| 11096 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11097 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11098 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11099 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11100 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11101 | #endif |
| 11102 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11103 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11104 | #endif |
| 11105 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11106 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11107 | #endif |
| 11108 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11109 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11110 | #endif |
| 11111 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11112 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11113 | #endif |
| 11114 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11115 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11116 | #endif |
| 11117 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11118 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11119 | #endif |
| 11120 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11121 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11122 | #endif |
| 11123 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11124 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11125 | #endif |
| 11126 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11127 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11128 | #endif |
| 11129 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11130 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11131 | #endif |
| 11132 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11133 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11134 | #endif |
| 11135 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11136 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11137 | #endif |
| 11138 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11139 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11140 | #endif |
| 11141 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11142 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11143 | #endif |
| 11144 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11145 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11146 | #endif |
| 11147 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11148 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11149 | #endif |
| 11150 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11151 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11152 | #endif |
| 11153 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11154 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11155 | #endif |
| 11156 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11157 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11158 | #endif |
| 11159 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11160 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11161 | #endif |
| 11162 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11163 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11164 | #endif |
| 11165 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11166 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11167 | #endif |
| 11168 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11169 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11170 | #endif |
| 11171 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11172 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11173 | #endif |
| 11174 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11175 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11176 | #endif |
| 11177 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11178 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11179 | #endif |
| 11180 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11181 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11182 | #endif |
| 11183 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11184 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11185 | #endif |
| 11186 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11187 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11188 | #endif |
| 11189 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11190 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11191 | #endif |
| 11192 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11193 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11194 | #endif |
| 11195 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11196 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11197 | #endif |
| 11198 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11199 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11200 | #endif |
| 11201 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11202 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11203 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11204 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11205 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11206 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11207 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11208 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11209 | #endif |
| 11210 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11211 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11212 | #endif |
| 11213 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11214 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11215 | #endif |
| 11216 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11217 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11218 | #endif |
| 11219 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11220 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11221 | #endif |
| 11222 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11223 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11224 | #endif |
| 11225 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11226 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11227 | #endif |
| 11228 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11229 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11230 | #endif |
| 11231 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11232 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11233 | #endif |
| 11234 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11235 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11236 | #endif |
| 11237 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11238 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11239 | #endif |
| 11240 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11241 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11242 | #endif |
| 11243 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11244 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11245 | #endif |
| 11246 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11247 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11248 | #endif |
| 11249 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11250 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11251 | #endif |
| 11252 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11253 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11254 | #endif |
| 11255 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11256 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11257 | #endif |
| 11258 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11259 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11260 | #endif |
| 11261 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11262 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11263 | #endif |
| 11264 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11265 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11266 | #endif |
| 11267 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11268 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11269 | #endif |
| 11270 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11271 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11272 | #endif |
| 11273 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11274 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11275 | #endif |
| 11276 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11277 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11278 | #endif |
| 11279 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11280 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11281 | #endif |
| 11282 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11283 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11284 | #endif |
| 11285 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11286 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11287 | #endif |
| 11288 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11289 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11290 | #endif |
| 11291 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11292 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11293 | #endif |
| 11294 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11295 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11296 | #endif |
| 11297 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11298 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11299 | #endif |
| 11300 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11301 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11302 | #endif |
| 11303 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11304 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11305 | #endif |
| 11306 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11307 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11308 | #endif |
| 11309 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11310 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11311 | #endif |
| 11312 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11313 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11314 | #endif |
| 11315 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11316 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11317 | #endif |
| 11318 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11319 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11320 | #endif |
| 11321 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11322 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11323 | #endif |
| 11324 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11325 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11326 | #endif |
| 11327 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11328 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11329 | #endif |
| 11330 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11331 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11332 | #endif |
| 11333 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11334 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11335 | #endif |
| 11336 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11337 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11338 | #endif |
| 11339 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11340 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11341 | #endif |
| 11342 | }; |
| 11343 | |
| 11344 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11345 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11346 | { |
| 11347 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 11348 | sizeof(posix_constants_sysconf) |
| 11349 | / sizeof(struct constdef)); |
| 11350 | } |
| 11351 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11352 | |
| 11353 | /*[clinic input] |
| 11354 | os.sysconf -> long |
| 11355 | name: sysconf_confname |
| 11356 | / |
| 11357 | |
| 11358 | Return an integer-valued system configuration variable. |
| 11359 | [clinic start generated code]*/ |
| 11360 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11361 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11362 | os_sysconf_impl(PyObject *module, int name) |
| 11363 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11364 | { |
| 11365 | long value; |
| 11366 | |
| 11367 | errno = 0; |
| 11368 | value = sysconf(name); |
| 11369 | if (value == -1 && errno != 0) |
| 11370 | posix_error(); |
| 11371 | return value; |
| 11372 | } |
| 11373 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11374 | |
| 11375 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11376 | /* 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] | 11377 | * 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] | 11378 | * the exported dictionaries that are used to publish information about the |
| 11379 | * names available on the host platform. |
| 11380 | * |
| 11381 | * Sorting the table at runtime ensures that the table is properly ordered |
| 11382 | * when used, even for platforms we're not able to test on. It also makes |
| 11383 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11384 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11385 | |
| 11386 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11387 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11388 | { |
| 11389 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11390 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11391 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11392 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11393 | |
| 11394 | return strcmp(c1->name, c2->name); |
| 11395 | } |
| 11396 | |
| 11397 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11398 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11399 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11400 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11401 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11402 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11403 | |
| 11404 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 11405 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11406 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11407 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11408 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11409 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11410 | PyObject *o = PyLong_FromLong(table[i].value); |
| 11411 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 11412 | Py_XDECREF(o); |
| 11413 | Py_DECREF(d); |
| 11414 | return -1; |
| 11415 | } |
| 11416 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11417 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11418 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11419 | } |
| 11420 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11421 | /* Return -1 on failure, 0 on success. */ |
| 11422 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11423 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11424 | { |
| 11425 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11426 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11427 | sizeof(posix_constants_pathconf) |
| 11428 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11429 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11430 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11431 | #endif |
| 11432 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11433 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11434 | sizeof(posix_constants_confstr) |
| 11435 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11436 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11437 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11438 | #endif |
| 11439 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11440 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11441 | sizeof(posix_constants_sysconf) |
| 11442 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11443 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11444 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11445 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11446 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11447 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11448 | |
| 11449 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11450 | /*[clinic input] |
| 11451 | os.abort |
| 11452 | |
| 11453 | Abort the interpreter immediately. |
| 11454 | |
| 11455 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 11456 | on the hosting operating system. This function never returns. |
| 11457 | [clinic start generated code]*/ |
| 11458 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11459 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11460 | os_abort_impl(PyObject *module) |
| 11461 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11462 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11463 | abort(); |
| 11464 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11465 | #ifndef __clang__ |
| 11466 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 11467 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 11468 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11469 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 11470 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11471 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11472 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11473 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11474 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11475 | /* Grab ShellExecute dynamically from shell32 */ |
| 11476 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11477 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 11478 | LPCWSTR, INT); |
| 11479 | static int |
| 11480 | check_ShellExecute() |
| 11481 | { |
| 11482 | HINSTANCE hShell32; |
| 11483 | |
| 11484 | /* only recheck */ |
| 11485 | if (-1 == has_ShellExecute) { |
| 11486 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11487 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 11488 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 11489 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 11490 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11491 | hShell32 = LoadLibraryW(L"SHELL32"); |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11492 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11493 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 11494 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11495 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11496 | } else { |
| 11497 | has_ShellExecute = 0; |
| 11498 | } |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 11499 | Py_END_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11500 | } |
| 11501 | return has_ShellExecute; |
| 11502 | } |
| 11503 | |
| 11504 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11505 | /*[clinic input] |
| 11506 | os.startfile |
| 11507 | filepath: path_t |
| 11508 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11509 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11510 | startfile(filepath [, operation]) |
| 11511 | |
| 11512 | Start a file with its associated application. |
| 11513 | |
| 11514 | When "operation" is not specified or "open", this acts like |
| 11515 | double-clicking the file in Explorer, or giving the file name as an |
| 11516 | argument to the DOS "start" command: the file is opened with whatever |
| 11517 | application (if any) its extension is associated. |
| 11518 | When another "operation" is given, it specifies what should be done with |
| 11519 | the file. A typical operation is "print". |
| 11520 | |
| 11521 | startfile returns as soon as the associated application is launched. |
| 11522 | There is no option to wait for the application to close, and no way |
| 11523 | to retrieve the application's exit status. |
| 11524 | |
| 11525 | The filepath is relative to the current directory. If you want to use |
| 11526 | an absolute path, make sure the first character is not a slash ("/"); |
| 11527 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 11528 | [clinic start generated code]*/ |
| 11529 | |
| 11530 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11531 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 11532 | const Py_UNICODE *operation) |
| 11533 | /*[clinic end generated code: output=66dc311c94d50797 input=63950bf2986380d0]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11534 | { |
| 11535 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11536 | |
| 11537 | if(!check_ShellExecute()) { |
| 11538 | /* If the OS doesn't have ShellExecute, return a |
| 11539 | NotImplementedError. */ |
| 11540 | return PyErr_Format(PyExc_NotImplementedError, |
| 11541 | "startfile not available on this platform"); |
| 11542 | } |
| 11543 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11544 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11545 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11546 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11547 | Py_END_ALLOW_THREADS |
| 11548 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11549 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11550 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11551 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11552 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11553 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11554 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11555 | #endif /* MS_WINDOWS */ |
| 11556 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11557 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11558 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11559 | /*[clinic input] |
| 11560 | os.getloadavg |
| 11561 | |
| 11562 | Return average recent system load information. |
| 11563 | |
| 11564 | Return the number of processes in the system run queue averaged over |
| 11565 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 11566 | Raises OSError if the load average was unobtainable. |
| 11567 | [clinic start generated code]*/ |
| 11568 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11569 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11570 | os_getloadavg_impl(PyObject *module) |
| 11571 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11572 | { |
| 11573 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11574 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11575 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 11576 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11577 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11578 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11579 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11580 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11581 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11582 | |
| 11583 | /*[clinic input] |
| 11584 | os.device_encoding |
| 11585 | fd: int |
| 11586 | |
| 11587 | Return a string describing the encoding of a terminal's file descriptor. |
| 11588 | |
| 11589 | The file descriptor must be attached to a terminal. |
| 11590 | If the device is not a terminal, return None. |
| 11591 | [clinic start generated code]*/ |
| 11592 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11593 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11594 | os_device_encoding_impl(PyObject *module, int fd) |
| 11595 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11596 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 11597 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 11598 | } |
| 11599 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11600 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11601 | #ifdef HAVE_SETRESUID |
| 11602 | /*[clinic input] |
| 11603 | os.setresuid |
| 11604 | |
| 11605 | ruid: uid_t |
| 11606 | euid: uid_t |
| 11607 | suid: uid_t |
| 11608 | / |
| 11609 | |
| 11610 | Set the current process's real, effective, and saved user ids. |
| 11611 | [clinic start generated code]*/ |
| 11612 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11613 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11614 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 11615 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11616 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11617 | if (setresuid(ruid, euid, suid) < 0) |
| 11618 | return posix_error(); |
| 11619 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11620 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11621 | #endif /* HAVE_SETRESUID */ |
| 11622 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11623 | |
| 11624 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11625 | /*[clinic input] |
| 11626 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11627 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11628 | rgid: gid_t |
| 11629 | egid: gid_t |
| 11630 | sgid: gid_t |
| 11631 | / |
| 11632 | |
| 11633 | Set the current process's real, effective, and saved group ids. |
| 11634 | [clinic start generated code]*/ |
| 11635 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11636 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11637 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 11638 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11639 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11640 | if (setresgid(rgid, egid, sgid) < 0) |
| 11641 | return posix_error(); |
| 11642 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11643 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11644 | #endif /* HAVE_SETRESGID */ |
| 11645 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11646 | |
| 11647 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11648 | /*[clinic input] |
| 11649 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11650 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11651 | Return a tuple of the current process's real, effective, and saved user ids. |
| 11652 | [clinic start generated code]*/ |
| 11653 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11654 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11655 | os_getresuid_impl(PyObject *module) |
| 11656 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11657 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11658 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11659 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 11660 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11661 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 11662 | _PyLong_FromUid(euid), |
| 11663 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11664 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11665 | #endif /* HAVE_GETRESUID */ |
| 11666 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11667 | |
| 11668 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11669 | /*[clinic input] |
| 11670 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11671 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11672 | Return a tuple of the current process's real, effective, and saved group ids. |
| 11673 | [clinic start generated code]*/ |
| 11674 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11675 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11676 | os_getresgid_impl(PyObject *module) |
| 11677 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11678 | { |
| 11679 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11680 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 11681 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11682 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 11683 | _PyLong_FromGid(egid), |
| 11684 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11685 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11686 | #endif /* HAVE_GETRESGID */ |
| 11687 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11688 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11689 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11690 | /*[clinic input] |
| 11691 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11692 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11693 | path: path_t(allow_fd=True) |
| 11694 | attribute: path_t |
| 11695 | * |
| 11696 | follow_symlinks: bool = True |
| 11697 | |
| 11698 | Return the value of extended attribute attribute on path. |
| 11699 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11700 | 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] | 11701 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11702 | link, getxattr will examine the symbolic link itself instead of the file |
| 11703 | the link points to. |
| 11704 | |
| 11705 | [clinic start generated code]*/ |
| 11706 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11707 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11708 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11709 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11710 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11711 | { |
| 11712 | Py_ssize_t i; |
| 11713 | PyObject *buffer = NULL; |
| 11714 | |
| 11715 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 11716 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11717 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11718 | for (i = 0; ; i++) { |
| 11719 | void *ptr; |
| 11720 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11721 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11722 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11723 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11724 | path_error(path); |
| 11725 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11726 | } |
| 11727 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 11728 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11729 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11730 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11731 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11732 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11733 | if (path->fd >= 0) |
| 11734 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11735 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11736 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11737 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11738 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11739 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11740 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11741 | if (result < 0) { |
| 11742 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11743 | if (errno == ERANGE) |
| 11744 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11745 | path_error(path); |
| 11746 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11747 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11748 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11749 | if (result != buffer_size) { |
| 11750 | /* Can only shrink. */ |
| 11751 | _PyBytes_Resize(&buffer, result); |
| 11752 | } |
| 11753 | break; |
| 11754 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11755 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11756 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11757 | } |
| 11758 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11759 | |
| 11760 | /*[clinic input] |
| 11761 | os.setxattr |
| 11762 | |
| 11763 | path: path_t(allow_fd=True) |
| 11764 | attribute: path_t |
| 11765 | value: Py_buffer |
| 11766 | flags: int = 0 |
| 11767 | * |
| 11768 | follow_symlinks: bool = True |
| 11769 | |
| 11770 | Set extended attribute attribute on path to value. |
| 11771 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11772 | 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] | 11773 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11774 | link, setxattr will modify the symbolic link itself instead of the file |
| 11775 | the link points to. |
| 11776 | |
| 11777 | [clinic start generated code]*/ |
| 11778 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11779 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11780 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11781 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11782 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11783 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11784 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11785 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11786 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11787 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11788 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11789 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11790 | if (path->fd > -1) |
| 11791 | result = fsetxattr(path->fd, attribute->narrow, |
| 11792 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11793 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11794 | result = setxattr(path->narrow, attribute->narrow, |
| 11795 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11796 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11797 | result = lsetxattr(path->narrow, attribute->narrow, |
| 11798 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11799 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11800 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11801 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11802 | path_error(path); |
| 11803 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11804 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11805 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11806 | Py_RETURN_NONE; |
| 11807 | } |
| 11808 | |
| 11809 | |
| 11810 | /*[clinic input] |
| 11811 | os.removexattr |
| 11812 | |
| 11813 | path: path_t(allow_fd=True) |
| 11814 | attribute: path_t |
| 11815 | * |
| 11816 | follow_symlinks: bool = True |
| 11817 | |
| 11818 | Remove extended attribute attribute on path. |
| 11819 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11820 | 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] | 11821 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11822 | link, removexattr will modify the symbolic link itself instead of the file |
| 11823 | the link points to. |
| 11824 | |
| 11825 | [clinic start generated code]*/ |
| 11826 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11827 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11828 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11829 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11830 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11831 | { |
| 11832 | ssize_t result; |
| 11833 | |
| 11834 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 11835 | return NULL; |
| 11836 | |
| 11837 | Py_BEGIN_ALLOW_THREADS; |
| 11838 | if (path->fd > -1) |
| 11839 | result = fremovexattr(path->fd, attribute->narrow); |
| 11840 | else if (follow_symlinks) |
| 11841 | result = removexattr(path->narrow, attribute->narrow); |
| 11842 | else |
| 11843 | result = lremovexattr(path->narrow, attribute->narrow); |
| 11844 | Py_END_ALLOW_THREADS; |
| 11845 | |
| 11846 | if (result) { |
| 11847 | return path_error(path); |
| 11848 | } |
| 11849 | |
| 11850 | Py_RETURN_NONE; |
| 11851 | } |
| 11852 | |
| 11853 | |
| 11854 | /*[clinic input] |
| 11855 | os.listxattr |
| 11856 | |
| 11857 | path: path_t(allow_fd=True, nullable=True) = None |
| 11858 | * |
| 11859 | follow_symlinks: bool = True |
| 11860 | |
| 11861 | Return a list of extended attributes on path. |
| 11862 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11863 | 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] | 11864 | if path is None, listxattr will examine the current directory. |
| 11865 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11866 | link, listxattr will examine the symbolic link itself instead of the file |
| 11867 | the link points to. |
| 11868 | [clinic start generated code]*/ |
| 11869 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11870 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11871 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11872 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11873 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11874 | Py_ssize_t i; |
| 11875 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11876 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11877 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11878 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11879 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11880 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11881 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11882 | name = path->narrow ? path->narrow : "."; |
| 11883 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11884 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11885 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11886 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11887 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11888 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11889 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 11890 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11891 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11892 | break; |
| 11893 | } |
| 11894 | buffer = PyMem_MALLOC(buffer_size); |
| 11895 | if (!buffer) { |
| 11896 | PyErr_NoMemory(); |
| 11897 | break; |
| 11898 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11899 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11900 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11901 | if (path->fd > -1) |
| 11902 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11903 | else if (follow_symlinks) |
| 11904 | length = listxattr(name, buffer, buffer_size); |
| 11905 | else |
| 11906 | length = llistxattr(name, buffer, buffer_size); |
| 11907 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11908 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11909 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11910 | if (errno == ERANGE) { |
| 11911 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 11912 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11913 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11914 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11915 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11916 | break; |
| 11917 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11918 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11919 | result = PyList_New(0); |
| 11920 | if (!result) { |
| 11921 | goto exit; |
| 11922 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11923 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11924 | end = buffer + length; |
| 11925 | for (trace = start = buffer; trace != end; trace++) { |
| 11926 | if (!*trace) { |
| 11927 | int error; |
| 11928 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 11929 | trace - start); |
| 11930 | if (!attribute) { |
| 11931 | Py_DECREF(result); |
| 11932 | result = NULL; |
| 11933 | goto exit; |
| 11934 | } |
| 11935 | error = PyList_Append(result, attribute); |
| 11936 | Py_DECREF(attribute); |
| 11937 | if (error) { |
| 11938 | Py_DECREF(result); |
| 11939 | result = NULL; |
| 11940 | goto exit; |
| 11941 | } |
| 11942 | start = trace + 1; |
| 11943 | } |
| 11944 | } |
| 11945 | break; |
| 11946 | } |
| 11947 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11948 | if (buffer) |
| 11949 | PyMem_FREE(buffer); |
| 11950 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11951 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11952 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11953 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11954 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11955 | /*[clinic input] |
| 11956 | os.urandom |
| 11957 | |
| 11958 | size: Py_ssize_t |
| 11959 | / |
| 11960 | |
| 11961 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 11962 | [clinic start generated code]*/ |
| 11963 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11964 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11965 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 11966 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11967 | { |
| 11968 | PyObject *bytes; |
| 11969 | int result; |
| 11970 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11971 | if (size < 0) |
| 11972 | return PyErr_Format(PyExc_ValueError, |
| 11973 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11974 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 11975 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11976 | return NULL; |
| 11977 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 11978 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11979 | if (result == -1) { |
| 11980 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11981 | return NULL; |
| 11982 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11983 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11984 | } |
| 11985 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 11986 | #ifdef HAVE_MEMFD_CREATE |
| 11987 | /*[clinic input] |
| 11988 | os.memfd_create |
| 11989 | |
| 11990 | name: FSConverter |
| 11991 | flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC |
| 11992 | |
| 11993 | [clinic start generated code]*/ |
| 11994 | |
| 11995 | static PyObject * |
| 11996 | os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags) |
| 11997 | /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/ |
| 11998 | { |
| 11999 | int fd; |
| 12000 | const char *bytes = PyBytes_AS_STRING(name); |
| 12001 | Py_BEGIN_ALLOW_THREADS |
| 12002 | fd = memfd_create(bytes, flags); |
| 12003 | Py_END_ALLOW_THREADS |
| 12004 | if (fd == -1) { |
| 12005 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12006 | } |
| 12007 | return PyLong_FromLong(fd); |
| 12008 | } |
| 12009 | #endif |
| 12010 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12011 | /* Terminal size querying */ |
| 12012 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 12013 | static PyTypeObject* TerminalSizeType; |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12014 | |
| 12015 | PyDoc_STRVAR(TerminalSize_docstring, |
| 12016 | "A tuple of (columns, lines) for holding terminal window size"); |
| 12017 | |
| 12018 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 12019 | {"columns", "width of the terminal window in characters"}, |
| 12020 | {"lines", "height of the terminal window in characters"}, |
| 12021 | {NULL, NULL} |
| 12022 | }; |
| 12023 | |
| 12024 | static PyStructSequence_Desc TerminalSize_desc = { |
| 12025 | "os.terminal_size", |
| 12026 | TerminalSize_docstring, |
| 12027 | TerminalSize_fields, |
| 12028 | 2, |
| 12029 | }; |
| 12030 | |
| 12031 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12032 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12033 | PyDoc_STRVAR(termsize__doc__, |
| 12034 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 12035 | "\n" \ |
| 12036 | "The optional argument fd (default standard output) specifies\n" \ |
| 12037 | "which file descriptor should be queried.\n" \ |
| 12038 | "\n" \ |
| 12039 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 12040 | "is thrown.\n" \ |
| 12041 | "\n" \ |
| 12042 | "This function will only be defined if an implementation is\n" \ |
| 12043 | "available for this system.\n" \ |
| 12044 | "\n" \ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 12045 | "shutil.get_terminal_size is the high-level function which should\n" \ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12046 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 12047 | |
| 12048 | static PyObject* |
| 12049 | get_terminal_size(PyObject *self, PyObject *args) |
| 12050 | { |
| 12051 | int columns, lines; |
| 12052 | PyObject *termsize; |
| 12053 | |
| 12054 | int fd = fileno(stdout); |
| 12055 | /* Under some conditions stdout may not be connected and |
| 12056 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 12057 | * GUI apps don't have valid standard streams by default. |
| 12058 | * |
| 12059 | * If this happens, and the optional fd argument is not present, |
| 12060 | * the ioctl below will fail returning EBADF. This is what we want. |
| 12061 | */ |
| 12062 | |
| 12063 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 12064 | return NULL; |
| 12065 | |
| 12066 | #ifdef TERMSIZE_USE_IOCTL |
| 12067 | { |
| 12068 | struct winsize w; |
| 12069 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 12070 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12071 | columns = w.ws_col; |
| 12072 | lines = w.ws_row; |
| 12073 | } |
| 12074 | #endif /* TERMSIZE_USE_IOCTL */ |
| 12075 | |
| 12076 | #ifdef TERMSIZE_USE_CONIO |
| 12077 | { |
| 12078 | DWORD nhandle; |
| 12079 | HANDLE handle; |
| 12080 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 12081 | switch (fd) { |
| 12082 | case 0: nhandle = STD_INPUT_HANDLE; |
| 12083 | break; |
| 12084 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 12085 | break; |
| 12086 | case 2: nhandle = STD_ERROR_HANDLE; |
| 12087 | break; |
| 12088 | default: |
| 12089 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 12090 | } |
| 12091 | handle = GetStdHandle(nhandle); |
| 12092 | if (handle == NULL) |
| 12093 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 12094 | if (handle == INVALID_HANDLE_VALUE) |
| 12095 | return PyErr_SetFromWindowsErr(0); |
| 12096 | |
| 12097 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 12098 | return PyErr_SetFromWindowsErr(0); |
| 12099 | |
| 12100 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 12101 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 12102 | } |
| 12103 | #endif /* TERMSIZE_USE_CONIO */ |
| 12104 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 12105 | termsize = PyStructSequence_New(TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12106 | if (termsize == NULL) |
| 12107 | return NULL; |
| 12108 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 12109 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 12110 | if (PyErr_Occurred()) { |
| 12111 | Py_DECREF(termsize); |
| 12112 | return NULL; |
| 12113 | } |
| 12114 | return termsize; |
| 12115 | } |
| 12116 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 12117 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12118 | |
| 12119 | /*[clinic input] |
| 12120 | os.cpu_count |
| 12121 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 12122 | Return the number of CPUs in the system; return None if indeterminable. |
| 12123 | |
| 12124 | This number is not equivalent to the number of CPUs the current process can |
| 12125 | use. The number of usable CPUs can be obtained with |
| 12126 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12127 | [clinic start generated code]*/ |
| 12128 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12129 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12130 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12131 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12132 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12133 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12134 | #ifdef MS_WINDOWS |
Christopher Wilcox | c67bae0 | 2017-08-30 05:01:08 -0400 | [diff] [blame] | 12135 | /* Vista is supported and the GetMaximumProcessorCount API is Win7+ |
| 12136 | Need to fallback to Vista behavior if this call isn't present */ |
| 12137 | HINSTANCE hKernel32; |
Christopher Wilcox | c67bae0 | 2017-08-30 05:01:08 -0400 | [diff] [blame] | 12138 | static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL; |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 12139 | Py_BEGIN_ALLOW_THREADS |
| 12140 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Christopher Wilcox | c67bae0 | 2017-08-30 05:01:08 -0400 | [diff] [blame] | 12141 | *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32, |
| 12142 | "GetMaximumProcessorCount"); |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 12143 | Py_END_ALLOW_THREADS |
Christopher Wilcox | c67bae0 | 2017-08-30 05:01:08 -0400 | [diff] [blame] | 12144 | if (_GetMaximumProcessorCount != NULL) { |
| 12145 | ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS); |
| 12146 | } |
| 12147 | else { |
| 12148 | SYSTEM_INFO sysinfo; |
| 12149 | GetSystemInfo(&sysinfo); |
| 12150 | ncpu = sysinfo.dwNumberOfProcessors; |
| 12151 | } |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12152 | #elif defined(__hpux) |
| 12153 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 12154 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 12155 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12156 | #elif defined(__DragonFly__) || \ |
| 12157 | defined(__OpenBSD__) || \ |
| 12158 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12159 | defined(__NetBSD__) || \ |
| 12160 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 12161 | int mib[2]; |
| 12162 | size_t len = sizeof(ncpu); |
| 12163 | mib[0] = CTL_HW; |
| 12164 | mib[1] = HW_NCPU; |
| 12165 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 12166 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12167 | #endif |
| 12168 | if (ncpu >= 1) |
| 12169 | return PyLong_FromLong(ncpu); |
| 12170 | else |
| 12171 | Py_RETURN_NONE; |
| 12172 | } |
| 12173 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12174 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12175 | /*[clinic input] |
| 12176 | os.get_inheritable -> bool |
| 12177 | |
| 12178 | fd: int |
| 12179 | / |
| 12180 | |
| 12181 | Get the close-on-exe flag of the specified file descriptor. |
| 12182 | [clinic start generated code]*/ |
| 12183 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12184 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12185 | os_get_inheritable_impl(PyObject *module, int fd) |
| 12186 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12187 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12188 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12189 | _Py_BEGIN_SUPPRESS_IPH |
| 12190 | return_value = _Py_get_inheritable(fd); |
| 12191 | _Py_END_SUPPRESS_IPH |
| 12192 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12193 | } |
| 12194 | |
| 12195 | |
| 12196 | /*[clinic input] |
| 12197 | os.set_inheritable |
| 12198 | fd: int |
| 12199 | inheritable: int |
| 12200 | / |
| 12201 | |
| 12202 | Set the inheritable flag of the specified file descriptor. |
| 12203 | [clinic start generated code]*/ |
| 12204 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12205 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12206 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 12207 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12208 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12209 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12210 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12211 | _Py_BEGIN_SUPPRESS_IPH |
| 12212 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 12213 | _Py_END_SUPPRESS_IPH |
| 12214 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12215 | return NULL; |
| 12216 | Py_RETURN_NONE; |
| 12217 | } |
| 12218 | |
| 12219 | |
| 12220 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12221 | /*[clinic input] |
| 12222 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12223 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12224 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12225 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12226 | Get the close-on-exe flag of the specified file descriptor. |
| 12227 | [clinic start generated code]*/ |
| 12228 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12229 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12230 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12231 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12232 | { |
| 12233 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12234 | |
| 12235 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 12236 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12237 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12238 | } |
| 12239 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12240 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12241 | } |
| 12242 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12243 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12244 | /*[clinic input] |
| 12245 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12246 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12247 | inheritable: bool |
| 12248 | / |
| 12249 | |
| 12250 | Set the inheritable flag of the specified handle. |
| 12251 | [clinic start generated code]*/ |
| 12252 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12253 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12254 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12255 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12256 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12257 | { |
| 12258 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12259 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 12260 | PyErr_SetFromWindowsErr(0); |
| 12261 | return NULL; |
| 12262 | } |
| 12263 | Py_RETURN_NONE; |
| 12264 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12265 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12266 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12267 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12268 | /*[clinic input] |
| 12269 | os.get_blocking -> bool |
| 12270 | fd: int |
| 12271 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12272 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12273 | Get the blocking mode of the file descriptor. |
| 12274 | |
| 12275 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 12276 | [clinic start generated code]*/ |
| 12277 | |
| 12278 | static int |
| 12279 | os_get_blocking_impl(PyObject *module, int fd) |
| 12280 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12281 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12282 | int blocking; |
| 12283 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12284 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12285 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12286 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12287 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12288 | } |
| 12289 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12290 | /*[clinic input] |
| 12291 | os.set_blocking |
| 12292 | fd: int |
| 12293 | blocking: bool(accept={int}) |
| 12294 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12295 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12296 | Set the blocking mode of the specified file descriptor. |
| 12297 | |
| 12298 | Set the O_NONBLOCK flag if blocking is False, |
| 12299 | clear the O_NONBLOCK flag otherwise. |
| 12300 | [clinic start generated code]*/ |
| 12301 | |
| 12302 | static PyObject * |
| 12303 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 12304 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12305 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12306 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12307 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12308 | _Py_BEGIN_SUPPRESS_IPH |
| 12309 | result = _Py_set_blocking(fd, blocking); |
| 12310 | _Py_END_SUPPRESS_IPH |
| 12311 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12312 | return NULL; |
| 12313 | Py_RETURN_NONE; |
| 12314 | } |
| 12315 | #endif /* !MS_WINDOWS */ |
| 12316 | |
| 12317 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12318 | /*[clinic input] |
| 12319 | class os.DirEntry "DirEntry *" "&DirEntryType" |
| 12320 | [clinic start generated code]*/ |
| 12321 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12322 | |
| 12323 | typedef struct { |
| 12324 | PyObject_HEAD |
| 12325 | PyObject *name; |
| 12326 | PyObject *path; |
| 12327 | PyObject *stat; |
| 12328 | PyObject *lstat; |
| 12329 | #ifdef MS_WINDOWS |
| 12330 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12331 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12332 | int got_file_index; |
| 12333 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12334 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12335 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12336 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12337 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12338 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12339 | #endif |
| 12340 | } DirEntry; |
| 12341 | |
| 12342 | static void |
| 12343 | DirEntry_dealloc(DirEntry *entry) |
| 12344 | { |
| 12345 | Py_XDECREF(entry->name); |
| 12346 | Py_XDECREF(entry->path); |
| 12347 | Py_XDECREF(entry->stat); |
| 12348 | Py_XDECREF(entry->lstat); |
| 12349 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 12350 | } |
| 12351 | |
| 12352 | /* Forward reference */ |
| 12353 | static int |
| 12354 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 12355 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12356 | /*[clinic input] |
| 12357 | os.DirEntry.is_symlink -> bool |
| 12358 | |
| 12359 | Return True if the entry is a symbolic link; cached per entry. |
| 12360 | [clinic start generated code]*/ |
| 12361 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12362 | static int |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12363 | os_DirEntry_is_symlink_impl(DirEntry *self) |
| 12364 | /*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12365 | { |
| 12366 | #ifdef MS_WINDOWS |
| 12367 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12368 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 12369 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12370 | if (self->d_type != DT_UNKNOWN) |
| 12371 | return self->d_type == DT_LNK; |
| 12372 | else |
| 12373 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12374 | #else |
| 12375 | /* POSIX without d_type */ |
| 12376 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12377 | #endif |
| 12378 | } |
| 12379 | |
| 12380 | static PyObject * |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12381 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 12382 | { |
| 12383 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12384 | STRUCT_STAT st; |
| 12385 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12386 | |
| 12387 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12388 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 12389 | return NULL; |
| 12390 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12391 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12392 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 12393 | return NULL; |
| 12394 | const char *path = PyBytes_AS_STRING(ub); |
| 12395 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 12396 | #ifdef HAVE_FSTATAT |
| 12397 | result = fstatat(self->dir_fd, path, &st, |
| 12398 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 12399 | #else |
| 12400 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 12401 | return NULL; |
| 12402 | #endif /* HAVE_FSTATAT */ |
| 12403 | } |
| 12404 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12405 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12406 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12407 | if (follow_symlinks) |
| 12408 | result = STAT(path, &st); |
| 12409 | else |
| 12410 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12411 | } |
| 12412 | Py_DECREF(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12413 | |
| 12414 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12415 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12416 | |
| 12417 | return _pystat_fromstructstat(&st); |
| 12418 | } |
| 12419 | |
| 12420 | static PyObject * |
| 12421 | DirEntry_get_lstat(DirEntry *self) |
| 12422 | { |
| 12423 | if (!self->lstat) { |
| 12424 | #ifdef MS_WINDOWS |
| 12425 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 12426 | #else /* POSIX */ |
| 12427 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 12428 | #endif |
| 12429 | } |
| 12430 | Py_XINCREF(self->lstat); |
| 12431 | return self->lstat; |
| 12432 | } |
| 12433 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12434 | /*[clinic input] |
| 12435 | os.DirEntry.stat |
| 12436 | * |
| 12437 | follow_symlinks: bool = True |
| 12438 | |
| 12439 | Return stat_result object for the entry; cached per entry. |
| 12440 | [clinic start generated code]*/ |
| 12441 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12442 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12443 | os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks) |
| 12444 | /*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12445 | { |
| 12446 | if (!follow_symlinks) |
| 12447 | return DirEntry_get_lstat(self); |
| 12448 | |
| 12449 | if (!self->stat) { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12450 | int result = os_DirEntry_is_symlink_impl(self); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12451 | if (result == -1) |
| 12452 | return NULL; |
| 12453 | else if (result) |
| 12454 | self->stat = DirEntry_fetch_stat(self, 1); |
| 12455 | else |
| 12456 | self->stat = DirEntry_get_lstat(self); |
| 12457 | } |
| 12458 | |
| 12459 | Py_XINCREF(self->stat); |
| 12460 | return self->stat; |
| 12461 | } |
| 12462 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12463 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 12464 | static int |
| 12465 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 12466 | { |
| 12467 | PyObject *stat = NULL; |
| 12468 | PyObject *st_mode = NULL; |
| 12469 | long mode; |
| 12470 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12471 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12472 | int is_symlink; |
| 12473 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12474 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12475 | #ifdef MS_WINDOWS |
| 12476 | unsigned long dir_bits; |
| 12477 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12478 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12479 | |
| 12480 | #ifdef MS_WINDOWS |
| 12481 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 12482 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12483 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12484 | is_symlink = self->d_type == DT_LNK; |
| 12485 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 12486 | #endif |
| 12487 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12488 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12489 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12490 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12491 | stat = os_DirEntry_stat_impl(self, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12492 | if (!stat) { |
| 12493 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 12494 | /* If file doesn't exist (anymore), then return False |
| 12495 | (i.e., say it's not a file/directory) */ |
| 12496 | PyErr_Clear(); |
| 12497 | return 0; |
| 12498 | } |
| 12499 | goto error; |
| 12500 | } |
| 12501 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 12502 | if (!st_mode) |
| 12503 | goto error; |
| 12504 | |
| 12505 | mode = PyLong_AsLong(st_mode); |
| 12506 | if (mode == -1 && PyErr_Occurred()) |
| 12507 | goto error; |
| 12508 | Py_CLEAR(st_mode); |
| 12509 | Py_CLEAR(stat); |
| 12510 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12511 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12512 | } |
| 12513 | else if (is_symlink) { |
| 12514 | assert(mode_bits != S_IFLNK); |
| 12515 | result = 0; |
| 12516 | } |
| 12517 | else { |
| 12518 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 12519 | #ifdef MS_WINDOWS |
| 12520 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 12521 | if (mode_bits == S_IFDIR) |
| 12522 | result = dir_bits != 0; |
| 12523 | else |
| 12524 | result = dir_bits == 0; |
| 12525 | #else /* POSIX */ |
| 12526 | if (mode_bits == S_IFDIR) |
| 12527 | result = self->d_type == DT_DIR; |
| 12528 | else |
| 12529 | result = self->d_type == DT_REG; |
| 12530 | #endif |
| 12531 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12532 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12533 | |
| 12534 | return result; |
| 12535 | |
| 12536 | error: |
| 12537 | Py_XDECREF(st_mode); |
| 12538 | Py_XDECREF(stat); |
| 12539 | return -1; |
| 12540 | } |
| 12541 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12542 | /*[clinic input] |
| 12543 | os.DirEntry.is_dir -> bool |
| 12544 | * |
| 12545 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12546 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12547 | Return True if the entry is a directory; cached per entry. |
| 12548 | [clinic start generated code]*/ |
| 12549 | |
| 12550 | static int |
| 12551 | os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks) |
| 12552 | /*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/ |
| 12553 | { |
| 12554 | return DirEntry_test_mode(self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12555 | } |
| 12556 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12557 | /*[clinic input] |
| 12558 | os.DirEntry.is_file -> bool |
| 12559 | * |
| 12560 | follow_symlinks: bool = True |
| 12561 | |
| 12562 | Return True if the entry is a file; cached per entry. |
| 12563 | [clinic start generated code]*/ |
| 12564 | |
| 12565 | static int |
| 12566 | os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks) |
| 12567 | /*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12568 | { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12569 | return DirEntry_test_mode(self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12570 | } |
| 12571 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12572 | /*[clinic input] |
| 12573 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12574 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12575 | Return inode of the entry; cached per entry. |
| 12576 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12577 | |
| 12578 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12579 | os_DirEntry_inode_impl(DirEntry *self) |
| 12580 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12581 | { |
| 12582 | #ifdef MS_WINDOWS |
| 12583 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12584 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12585 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12586 | STRUCT_STAT stat; |
| 12587 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12588 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12589 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12590 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12591 | path = PyUnicode_AsUnicode(unicode); |
| 12592 | result = LSTAT(path, &stat); |
| 12593 | Py_DECREF(unicode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12594 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12595 | if (result != 0) |
| 12596 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12597 | |
| 12598 | self->win32_file_index = stat.st_ino; |
| 12599 | self->got_file_index = 1; |
| 12600 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12601 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 12602 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12603 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 12604 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 12605 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12606 | #endif |
| 12607 | } |
| 12608 | |
| 12609 | static PyObject * |
| 12610 | DirEntry_repr(DirEntry *self) |
| 12611 | { |
| 12612 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 12613 | } |
| 12614 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12615 | /*[clinic input] |
| 12616 | os.DirEntry.__fspath__ |
| 12617 | |
| 12618 | Returns the path for the entry. |
| 12619 | [clinic start generated code]*/ |
| 12620 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12621 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12622 | os_DirEntry___fspath___impl(DirEntry *self) |
| 12623 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12624 | { |
| 12625 | Py_INCREF(self->path); |
| 12626 | return self->path; |
| 12627 | } |
| 12628 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12629 | static PyMemberDef DirEntry_members[] = { |
| 12630 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 12631 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 12632 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 12633 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 12634 | {NULL} |
| 12635 | }; |
| 12636 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12637 | #include "clinic/posixmodule.c.h" |
| 12638 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12639 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12640 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 12641 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 12642 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 12643 | OS_DIRENTRY_STAT_METHODDEF |
| 12644 | OS_DIRENTRY_INODE_METHODDEF |
| 12645 | OS_DIRENTRY___FSPATH___METHODDEF |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12646 | {NULL} |
| 12647 | }; |
| 12648 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12649 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12650 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12651 | MODNAME ".DirEntry", /* tp_name */ |
| 12652 | sizeof(DirEntry), /* tp_basicsize */ |
| 12653 | 0, /* tp_itemsize */ |
| 12654 | /* methods */ |
| 12655 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 12656 | 0, /* tp_vectorcall_offset */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12657 | 0, /* tp_getattr */ |
| 12658 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 12659 | 0, /* tp_as_async */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12660 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 12661 | 0, /* tp_as_number */ |
| 12662 | 0, /* tp_as_sequence */ |
| 12663 | 0, /* tp_as_mapping */ |
| 12664 | 0, /* tp_hash */ |
| 12665 | 0, /* tp_call */ |
| 12666 | 0, /* tp_str */ |
| 12667 | 0, /* tp_getattro */ |
| 12668 | 0, /* tp_setattro */ |
| 12669 | 0, /* tp_as_buffer */ |
| 12670 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 12671 | 0, /* tp_doc */ |
| 12672 | 0, /* tp_traverse */ |
| 12673 | 0, /* tp_clear */ |
| 12674 | 0, /* tp_richcompare */ |
| 12675 | 0, /* tp_weaklistoffset */ |
| 12676 | 0, /* tp_iter */ |
| 12677 | 0, /* tp_iternext */ |
| 12678 | DirEntry_methods, /* tp_methods */ |
| 12679 | DirEntry_members, /* tp_members */ |
| 12680 | }; |
| 12681 | |
| 12682 | #ifdef MS_WINDOWS |
| 12683 | |
| 12684 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12685 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12686 | { |
| 12687 | Py_ssize_t path_len; |
| 12688 | Py_ssize_t size; |
| 12689 | wchar_t *result; |
| 12690 | wchar_t ch; |
| 12691 | |
| 12692 | if (!path_wide) { /* Default arg: "." */ |
| 12693 | path_wide = L"."; |
| 12694 | path_len = 1; |
| 12695 | } |
| 12696 | else { |
| 12697 | path_len = wcslen(path_wide); |
| 12698 | } |
| 12699 | |
| 12700 | /* The +1's are for the path separator and the NUL */ |
| 12701 | size = path_len + 1 + wcslen(filename) + 1; |
| 12702 | result = PyMem_New(wchar_t, size); |
| 12703 | if (!result) { |
| 12704 | PyErr_NoMemory(); |
| 12705 | return NULL; |
| 12706 | } |
| 12707 | wcscpy(result, path_wide); |
| 12708 | if (path_len > 0) { |
| 12709 | ch = result[path_len - 1]; |
| 12710 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 12711 | result[path_len++] = SEP; |
| 12712 | wcscpy(result + path_len, filename); |
| 12713 | } |
| 12714 | return result; |
| 12715 | } |
| 12716 | |
| 12717 | static PyObject * |
| 12718 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 12719 | { |
| 12720 | DirEntry *entry; |
| 12721 | BY_HANDLE_FILE_INFORMATION file_info; |
| 12722 | ULONG reparse_tag; |
| 12723 | wchar_t *joined_path; |
| 12724 | |
| 12725 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 12726 | if (!entry) |
| 12727 | return NULL; |
| 12728 | entry->name = NULL; |
| 12729 | entry->path = NULL; |
| 12730 | entry->stat = NULL; |
| 12731 | entry->lstat = NULL; |
| 12732 | entry->got_file_index = 0; |
| 12733 | |
| 12734 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 12735 | if (!entry->name) |
| 12736 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12737 | if (path->narrow) { |
| 12738 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 12739 | if (!entry->name) |
| 12740 | goto error; |
| 12741 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12742 | |
| 12743 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 12744 | if (!joined_path) |
| 12745 | goto error; |
| 12746 | |
| 12747 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 12748 | PyMem_Free(joined_path); |
| 12749 | if (!entry->path) |
| 12750 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12751 | if (path->narrow) { |
| 12752 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 12753 | if (!entry->path) |
| 12754 | goto error; |
| 12755 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12756 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12757 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12758 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 12759 | |
| 12760 | return (PyObject *)entry; |
| 12761 | |
| 12762 | error: |
| 12763 | Py_DECREF(entry); |
| 12764 | return NULL; |
| 12765 | } |
| 12766 | |
| 12767 | #else /* POSIX */ |
| 12768 | |
| 12769 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12770 | 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] | 12771 | { |
| 12772 | Py_ssize_t path_len; |
| 12773 | Py_ssize_t size; |
| 12774 | char *result; |
| 12775 | |
| 12776 | if (!path_narrow) { /* Default arg: "." */ |
| 12777 | path_narrow = "."; |
| 12778 | path_len = 1; |
| 12779 | } |
| 12780 | else { |
| 12781 | path_len = strlen(path_narrow); |
| 12782 | } |
| 12783 | |
| 12784 | if (filename_len == -1) |
| 12785 | filename_len = strlen(filename); |
| 12786 | |
| 12787 | /* The +1's are for the path separator and the NUL */ |
| 12788 | size = path_len + 1 + filename_len + 1; |
| 12789 | result = PyMem_New(char, size); |
| 12790 | if (!result) { |
| 12791 | PyErr_NoMemory(); |
| 12792 | return NULL; |
| 12793 | } |
| 12794 | strcpy(result, path_narrow); |
| 12795 | if (path_len > 0 && result[path_len - 1] != '/') |
| 12796 | result[path_len++] = '/'; |
| 12797 | strcpy(result + path_len, filename); |
| 12798 | return result; |
| 12799 | } |
| 12800 | |
| 12801 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12802 | 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] | 12803 | ino_t d_ino |
| 12804 | #ifdef HAVE_DIRENT_D_TYPE |
| 12805 | , unsigned char d_type |
| 12806 | #endif |
| 12807 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12808 | { |
| 12809 | DirEntry *entry; |
| 12810 | char *joined_path; |
| 12811 | |
| 12812 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 12813 | if (!entry) |
| 12814 | return NULL; |
| 12815 | entry->name = NULL; |
| 12816 | entry->path = NULL; |
| 12817 | entry->stat = NULL; |
| 12818 | entry->lstat = NULL; |
| 12819 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12820 | if (path->fd != -1) { |
| 12821 | entry->dir_fd = path->fd; |
| 12822 | joined_path = NULL; |
| 12823 | } |
| 12824 | else { |
| 12825 | entry->dir_fd = DEFAULT_DIR_FD; |
| 12826 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 12827 | if (!joined_path) |
| 12828 | goto error; |
| 12829 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12830 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 12831 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12832 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12833 | if (joined_path) |
| 12834 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12835 | } |
| 12836 | else { |
| 12837 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12838 | if (joined_path) |
| 12839 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12840 | } |
| 12841 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12842 | if (!entry->name) |
| 12843 | goto error; |
| 12844 | |
| 12845 | if (path->fd != -1) { |
| 12846 | entry->path = entry->name; |
| 12847 | Py_INCREF(entry->path); |
| 12848 | } |
| 12849 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12850 | goto error; |
| 12851 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12852 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12853 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12854 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12855 | entry->d_ino = d_ino; |
| 12856 | |
| 12857 | return (PyObject *)entry; |
| 12858 | |
| 12859 | error: |
| 12860 | Py_XDECREF(entry); |
| 12861 | return NULL; |
| 12862 | } |
| 12863 | |
| 12864 | #endif |
| 12865 | |
| 12866 | |
| 12867 | typedef struct { |
| 12868 | PyObject_HEAD |
| 12869 | path_t path; |
| 12870 | #ifdef MS_WINDOWS |
| 12871 | HANDLE handle; |
| 12872 | WIN32_FIND_DATAW file_data; |
| 12873 | int first_time; |
| 12874 | #else /* POSIX */ |
| 12875 | DIR *dirp; |
| 12876 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12877 | #ifdef HAVE_FDOPENDIR |
| 12878 | int fd; |
| 12879 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12880 | } ScandirIterator; |
| 12881 | |
| 12882 | #ifdef MS_WINDOWS |
| 12883 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12884 | static int |
| 12885 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 12886 | { |
| 12887 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 12888 | } |
| 12889 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12890 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12891 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12892 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12893 | HANDLE handle = iterator->handle; |
| 12894 | |
| 12895 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12896 | return; |
| 12897 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12898 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12899 | Py_BEGIN_ALLOW_THREADS |
| 12900 | FindClose(handle); |
| 12901 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12902 | } |
| 12903 | |
| 12904 | static PyObject * |
| 12905 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12906 | { |
| 12907 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 12908 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12909 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12910 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12911 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12912 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12913 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12914 | |
| 12915 | while (1) { |
| 12916 | if (!iterator->first_time) { |
| 12917 | Py_BEGIN_ALLOW_THREADS |
| 12918 | success = FindNextFileW(iterator->handle, file_data); |
| 12919 | Py_END_ALLOW_THREADS |
| 12920 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12921 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12922 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12923 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12924 | break; |
| 12925 | } |
| 12926 | } |
| 12927 | iterator->first_time = 0; |
| 12928 | |
| 12929 | /* Skip over . and .. */ |
| 12930 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12931 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 12932 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 12933 | if (!entry) |
| 12934 | break; |
| 12935 | return entry; |
| 12936 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12937 | |
| 12938 | /* Loop till we get a non-dot directory or finish iterating */ |
| 12939 | } |
| 12940 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12941 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12942 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12943 | return NULL; |
| 12944 | } |
| 12945 | |
| 12946 | #else /* POSIX */ |
| 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->dirp; |
| 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 | DIR *dirp = iterator->dirp; |
| 12958 | |
| 12959 | if (!dirp) |
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->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12963 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12964 | #ifdef HAVE_FDOPENDIR |
| 12965 | if (iterator->path.fd != -1) |
| 12966 | rewinddir(dirp); |
| 12967 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12968 | closedir(dirp); |
| 12969 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12970 | return; |
| 12971 | } |
| 12972 | |
| 12973 | static PyObject * |
| 12974 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12975 | { |
| 12976 | struct dirent *direntp; |
| 12977 | Py_ssize_t name_len; |
| 12978 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12979 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12980 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12981 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12982 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12983 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12984 | |
| 12985 | while (1) { |
| 12986 | errno = 0; |
| 12987 | Py_BEGIN_ALLOW_THREADS |
| 12988 | direntp = readdir(iterator->dirp); |
| 12989 | Py_END_ALLOW_THREADS |
| 12990 | |
| 12991 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12992 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12993 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12994 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12995 | break; |
| 12996 | } |
| 12997 | |
| 12998 | /* Skip over . and .. */ |
| 12999 | name_len = NAMLEN(direntp); |
| 13000 | is_dot = direntp->d_name[0] == '.' && |
| 13001 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 13002 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13003 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13004 | name_len, direntp->d_ino |
| 13005 | #ifdef HAVE_DIRENT_D_TYPE |
| 13006 | , direntp->d_type |
| 13007 | #endif |
| 13008 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13009 | if (!entry) |
| 13010 | break; |
| 13011 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13012 | } |
| 13013 | |
| 13014 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13015 | } |
| 13016 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13017 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13018 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13019 | return NULL; |
| 13020 | } |
| 13021 | |
| 13022 | #endif |
| 13023 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13024 | static PyObject * |
| 13025 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 13026 | { |
| 13027 | ScandirIterator_closedir(self); |
| 13028 | Py_RETURN_NONE; |
| 13029 | } |
| 13030 | |
| 13031 | static PyObject * |
| 13032 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 13033 | { |
| 13034 | Py_INCREF(self); |
| 13035 | return self; |
| 13036 | } |
| 13037 | |
| 13038 | static PyObject * |
| 13039 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 13040 | { |
| 13041 | ScandirIterator_closedir(self); |
| 13042 | Py_RETURN_NONE; |
| 13043 | } |
| 13044 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13045 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13046 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 13047 | { |
| 13048 | PyObject *error_type, *error_value, *error_traceback; |
| 13049 | |
| 13050 | /* Save the current exception, if any. */ |
| 13051 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 13052 | |
| 13053 | if (!ScandirIterator_is_closed(iterator)) { |
| 13054 | ScandirIterator_closedir(iterator); |
| 13055 | |
| 13056 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 13057 | "unclosed scandir iterator %R", iterator)) { |
| 13058 | /* Spurious errors can appear at shutdown */ |
| 13059 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 13060 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 13061 | } |
| 13062 | } |
| 13063 | } |
| 13064 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13065 | path_cleanup(&iterator->path); |
| 13066 | |
| 13067 | /* Restore the saved exception. */ |
| 13068 | PyErr_Restore(error_type, error_value, error_traceback); |
| 13069 | } |
| 13070 | |
| 13071 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13072 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 13073 | { |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13074 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 13075 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13076 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13077 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 13078 | } |
| 13079 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13080 | static PyMethodDef ScandirIterator_methods[] = { |
| 13081 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 13082 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 13083 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 13084 | {NULL} |
| 13085 | }; |
| 13086 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 13087 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13088 | PyVarObject_HEAD_INIT(NULL, 0) |
| 13089 | MODNAME ".ScandirIterator", /* tp_name */ |
| 13090 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 13091 | 0, /* tp_itemsize */ |
| 13092 | /* methods */ |
| 13093 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 13094 | 0, /* tp_vectorcall_offset */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13095 | 0, /* tp_getattr */ |
| 13096 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 13097 | 0, /* tp_as_async */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13098 | 0, /* tp_repr */ |
| 13099 | 0, /* tp_as_number */ |
| 13100 | 0, /* tp_as_sequence */ |
| 13101 | 0, /* tp_as_mapping */ |
| 13102 | 0, /* tp_hash */ |
| 13103 | 0, /* tp_call */ |
| 13104 | 0, /* tp_str */ |
| 13105 | 0, /* tp_getattro */ |
| 13106 | 0, /* tp_setattro */ |
| 13107 | 0, /* tp_as_buffer */ |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 13108 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13109 | 0, /* tp_doc */ |
| 13110 | 0, /* tp_traverse */ |
| 13111 | 0, /* tp_clear */ |
| 13112 | 0, /* tp_richcompare */ |
| 13113 | 0, /* tp_weaklistoffset */ |
| 13114 | PyObject_SelfIter, /* tp_iter */ |
| 13115 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13116 | ScandirIterator_methods, /* tp_methods */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13117 | 0, /* tp_members */ |
| 13118 | 0, /* tp_getset */ |
| 13119 | 0, /* tp_base */ |
| 13120 | 0, /* tp_dict */ |
| 13121 | 0, /* tp_descr_get */ |
| 13122 | 0, /* tp_descr_set */ |
| 13123 | 0, /* tp_dictoffset */ |
| 13124 | 0, /* tp_init */ |
| 13125 | 0, /* tp_alloc */ |
| 13126 | 0, /* tp_new */ |
| 13127 | 0, /* tp_free */ |
| 13128 | 0, /* tp_is_gc */ |
| 13129 | 0, /* tp_bases */ |
| 13130 | 0, /* tp_mro */ |
| 13131 | 0, /* tp_cache */ |
| 13132 | 0, /* tp_subclasses */ |
| 13133 | 0, /* tp_weaklist */ |
| 13134 | 0, /* tp_del */ |
| 13135 | 0, /* tp_version_tag */ |
| 13136 | (destructor)ScandirIterator_finalize, /* tp_finalize */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13137 | }; |
| 13138 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13139 | /*[clinic input] |
| 13140 | os.scandir |
| 13141 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13142 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13143 | |
| 13144 | Return an iterator of DirEntry objects for given path. |
| 13145 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13146 | 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] | 13147 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 13148 | all other circumstances they will be str. |
| 13149 | |
| 13150 | If path is None, uses the path='.'. |
| 13151 | [clinic start generated code]*/ |
| 13152 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13153 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13154 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13155 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13156 | { |
| 13157 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13158 | #ifdef MS_WINDOWS |
| 13159 | wchar_t *path_strW; |
| 13160 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13161 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13162 | #ifdef HAVE_FDOPENDIR |
| 13163 | int fd = -1; |
| 13164 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13165 | #endif |
| 13166 | |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 13167 | if (PySys_Audit("os.scandir", "O", |
| 13168 | path->object ? path->object : Py_None) < 0) { |
| 13169 | return NULL; |
| 13170 | } |
| 13171 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13172 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 13173 | if (!iterator) |
| 13174 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13175 | |
| 13176 | #ifdef MS_WINDOWS |
| 13177 | iterator->handle = INVALID_HANDLE_VALUE; |
| 13178 | #else |
| 13179 | iterator->dirp = NULL; |
| 13180 | #endif |
| 13181 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13182 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 13183 | /* Move the ownership to iterator->path */ |
| 13184 | path->object = NULL; |
| 13185 | path->cleanup = NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13186 | |
| 13187 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13188 | iterator->first_time = 1; |
| 13189 | |
| 13190 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 13191 | if (!path_strW) |
| 13192 | goto error; |
| 13193 | |
| 13194 | Py_BEGIN_ALLOW_THREADS |
| 13195 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 13196 | Py_END_ALLOW_THREADS |
| 13197 | |
| 13198 | PyMem_Free(path_strW); |
| 13199 | |
| 13200 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 13201 | path_error(&iterator->path); |
| 13202 | goto error; |
| 13203 | } |
| 13204 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13205 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13206 | #ifdef HAVE_FDOPENDIR |
| 13207 | if (path->fd != -1) { |
| 13208 | /* closedir() closes the FD, so we duplicate it */ |
| 13209 | fd = _Py_dup(path->fd); |
| 13210 | if (fd == -1) |
| 13211 | goto error; |
| 13212 | |
| 13213 | Py_BEGIN_ALLOW_THREADS |
| 13214 | iterator->dirp = fdopendir(fd); |
| 13215 | Py_END_ALLOW_THREADS |
| 13216 | } |
| 13217 | else |
| 13218 | #endif |
| 13219 | { |
| 13220 | if (iterator->path.narrow) |
| 13221 | path_str = iterator->path.narrow; |
| 13222 | else |
| 13223 | path_str = "."; |
| 13224 | |
| 13225 | Py_BEGIN_ALLOW_THREADS |
| 13226 | iterator->dirp = opendir(path_str); |
| 13227 | Py_END_ALLOW_THREADS |
| 13228 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13229 | |
| 13230 | if (!iterator->dirp) { |
| 13231 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13232 | #ifdef HAVE_FDOPENDIR |
| 13233 | if (fd != -1) { |
| 13234 | Py_BEGIN_ALLOW_THREADS |
| 13235 | close(fd); |
| 13236 | Py_END_ALLOW_THREADS |
| 13237 | } |
| 13238 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13239 | goto error; |
| 13240 | } |
| 13241 | #endif |
| 13242 | |
| 13243 | return (PyObject *)iterator; |
| 13244 | |
| 13245 | error: |
| 13246 | Py_DECREF(iterator); |
| 13247 | return NULL; |
| 13248 | } |
| 13249 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13250 | /* |
| 13251 | Return the file system path representation of the object. |
| 13252 | |
| 13253 | If the object is str or bytes, then allow it to pass through with |
| 13254 | an incremented refcount. If the object defines __fspath__(), then |
| 13255 | return the result of that method. All other types raise a TypeError. |
| 13256 | */ |
| 13257 | PyObject * |
| 13258 | PyOS_FSPath(PyObject *path) |
| 13259 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 13260 | /* For error message reasons, this function is manually inlined in |
| 13261 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13262 | _Py_IDENTIFIER(__fspath__); |
| 13263 | PyObject *func = NULL; |
| 13264 | PyObject *path_repr = NULL; |
| 13265 | |
| 13266 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 13267 | Py_INCREF(path); |
| 13268 | return path; |
| 13269 | } |
| 13270 | |
| 13271 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 13272 | if (NULL == func) { |
| 13273 | return PyErr_Format(PyExc_TypeError, |
| 13274 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13275 | "not %.200s", |
| 13276 | Py_TYPE(path)->tp_name); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13277 | } |
| 13278 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 13279 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13280 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 13281 | if (NULL == path_repr) { |
| 13282 | return NULL; |
| 13283 | } |
| 13284 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13285 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 13286 | PyErr_Format(PyExc_TypeError, |
| 13287 | "expected %.200s.__fspath__() to return str or bytes, " |
| 13288 | "not %.200s", Py_TYPE(path)->tp_name, |
| 13289 | Py_TYPE(path_repr)->tp_name); |
| 13290 | Py_DECREF(path_repr); |
| 13291 | return NULL; |
| 13292 | } |
| 13293 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13294 | return path_repr; |
| 13295 | } |
| 13296 | |
| 13297 | /*[clinic input] |
| 13298 | os.fspath |
| 13299 | |
| 13300 | path: object |
| 13301 | |
| 13302 | Return the file system path representation of the object. |
| 13303 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 13304 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 13305 | object defines __fspath__(), then return the result of that method. All other |
| 13306 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13307 | [clinic start generated code]*/ |
| 13308 | |
| 13309 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 13310 | os_fspath_impl(PyObject *module, PyObject *path) |
| 13311 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13312 | { |
| 13313 | return PyOS_FSPath(path); |
| 13314 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13315 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13316 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13317 | /*[clinic input] |
| 13318 | os.getrandom |
| 13319 | |
| 13320 | size: Py_ssize_t |
| 13321 | flags: int=0 |
| 13322 | |
| 13323 | Obtain a series of random bytes. |
| 13324 | [clinic start generated code]*/ |
| 13325 | |
| 13326 | static PyObject * |
| 13327 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 13328 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 13329 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13330 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13331 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13332 | |
| 13333 | if (size < 0) { |
| 13334 | errno = EINVAL; |
| 13335 | return posix_error(); |
| 13336 | } |
| 13337 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13338 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 13339 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13340 | PyErr_NoMemory(); |
| 13341 | return NULL; |
| 13342 | } |
| 13343 | |
| 13344 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13345 | n = syscall(SYS_getrandom, |
| 13346 | PyBytes_AS_STRING(bytes), |
| 13347 | PyBytes_GET_SIZE(bytes), |
| 13348 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13349 | if (n < 0 && errno == EINTR) { |
| 13350 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13351 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13352 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13353 | |
| 13354 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13355 | continue; |
| 13356 | } |
| 13357 | break; |
| 13358 | } |
| 13359 | |
| 13360 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13361 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13362 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13363 | } |
| 13364 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13365 | if (n != size) { |
| 13366 | _PyBytes_Resize(&bytes, n); |
| 13367 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13368 | |
| 13369 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13370 | |
| 13371 | error: |
| 13372 | Py_DECREF(bytes); |
| 13373 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13374 | } |
| 13375 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 13376 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13377 | #ifdef MS_WINDOWS |
| 13378 | /* bpo-36085: Helper functions for managing DLL search directories |
| 13379 | * on win32 |
| 13380 | */ |
| 13381 | |
| 13382 | typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory); |
| 13383 | typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie); |
| 13384 | |
| 13385 | /*[clinic input] |
| 13386 | os._add_dll_directory |
| 13387 | |
| 13388 | path: path_t |
| 13389 | |
| 13390 | Add a path to the DLL search path. |
| 13391 | |
| 13392 | This search path is used when resolving dependencies for imported |
| 13393 | extension modules (the module itself is resolved through sys.path), |
| 13394 | and also by ctypes. |
| 13395 | |
| 13396 | Returns an opaque value that may be passed to os.remove_dll_directory |
| 13397 | to remove this directory from the search path. |
| 13398 | [clinic start generated code]*/ |
| 13399 | |
| 13400 | static PyObject * |
| 13401 | os__add_dll_directory_impl(PyObject *module, path_t *path) |
| 13402 | /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ |
| 13403 | { |
| 13404 | HMODULE hKernel32; |
| 13405 | PAddDllDirectory AddDllDirectory; |
| 13406 | DLL_DIRECTORY_COOKIE cookie = 0; |
| 13407 | DWORD err = 0; |
| 13408 | |
| 13409 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13410 | infrequent operation, just do it each time. Kernel32 is always |
| 13411 | loaded. */ |
| 13412 | Py_BEGIN_ALLOW_THREADS |
| 13413 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13414 | !(AddDllDirectory = (PAddDllDirectory)GetProcAddress( |
| 13415 | hKernel32, "AddDllDirectory")) || |
| 13416 | !(cookie = (*AddDllDirectory)(path->wide))) { |
| 13417 | err = GetLastError(); |
| 13418 | } |
| 13419 | Py_END_ALLOW_THREADS |
| 13420 | |
| 13421 | if (err) { |
| 13422 | return win32_error_object_err("add_dll_directory", |
| 13423 | path->object, err); |
| 13424 | } |
| 13425 | |
| 13426 | return PyCapsule_New(cookie, "DLL directory cookie", NULL); |
| 13427 | } |
| 13428 | |
| 13429 | /*[clinic input] |
| 13430 | os._remove_dll_directory |
| 13431 | |
| 13432 | cookie: object |
| 13433 | |
| 13434 | Removes a path from the DLL search path. |
| 13435 | |
| 13436 | The parameter is an opaque value that was returned from |
| 13437 | os.add_dll_directory. You can only remove directories that you added |
| 13438 | yourself. |
| 13439 | [clinic start generated code]*/ |
| 13440 | |
| 13441 | static PyObject * |
| 13442 | os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) |
| 13443 | /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ |
| 13444 | { |
| 13445 | HMODULE hKernel32; |
| 13446 | PRemoveDllDirectory RemoveDllDirectory; |
| 13447 | DLL_DIRECTORY_COOKIE cookieValue; |
| 13448 | DWORD err = 0; |
| 13449 | |
| 13450 | if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) { |
| 13451 | PyErr_SetString(PyExc_TypeError, |
| 13452 | "Provided cookie was not returned from os.add_dll_directory"); |
| 13453 | return NULL; |
| 13454 | } |
| 13455 | |
| 13456 | cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( |
| 13457 | cookie, "DLL directory cookie"); |
| 13458 | |
| 13459 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13460 | infrequent operation, just do it each time. Kernel32 is always |
| 13461 | loaded. */ |
| 13462 | Py_BEGIN_ALLOW_THREADS |
| 13463 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13464 | !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress( |
| 13465 | hKernel32, "RemoveDllDirectory")) || |
| 13466 | !(*RemoveDllDirectory)(cookieValue)) { |
| 13467 | err = GetLastError(); |
| 13468 | } |
| 13469 | Py_END_ALLOW_THREADS |
| 13470 | |
| 13471 | if (err) { |
| 13472 | return win32_error_object_err("remove_dll_directory", |
| 13473 | NULL, err); |
| 13474 | } |
| 13475 | |
| 13476 | if (PyCapsule_SetName(cookie, NULL)) { |
| 13477 | return NULL; |
| 13478 | } |
| 13479 | |
| 13480 | Py_RETURN_NONE; |
| 13481 | } |
| 13482 | |
| 13483 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13484 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13485 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13486 | |
| 13487 | OS_STAT_METHODDEF |
| 13488 | OS_ACCESS_METHODDEF |
| 13489 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13490 | OS_CHDIR_METHODDEF |
| 13491 | OS_CHFLAGS_METHODDEF |
| 13492 | OS_CHMOD_METHODDEF |
| 13493 | OS_FCHMOD_METHODDEF |
| 13494 | OS_LCHMOD_METHODDEF |
| 13495 | OS_CHOWN_METHODDEF |
| 13496 | OS_FCHOWN_METHODDEF |
| 13497 | OS_LCHOWN_METHODDEF |
| 13498 | OS_LCHFLAGS_METHODDEF |
| 13499 | OS_CHROOT_METHODDEF |
| 13500 | OS_CTERMID_METHODDEF |
| 13501 | OS_GETCWD_METHODDEF |
| 13502 | OS_GETCWDB_METHODDEF |
| 13503 | OS_LINK_METHODDEF |
| 13504 | OS_LISTDIR_METHODDEF |
| 13505 | OS_LSTAT_METHODDEF |
| 13506 | OS_MKDIR_METHODDEF |
| 13507 | OS_NICE_METHODDEF |
| 13508 | OS_GETPRIORITY_METHODDEF |
| 13509 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13510 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 13511 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13512 | OS_READLINK_METHODDEF |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 13513 | OS_COPY_FILE_RANGE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13514 | OS_RENAME_METHODDEF |
| 13515 | OS_REPLACE_METHODDEF |
| 13516 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13517 | OS_SYMLINK_METHODDEF |
| 13518 | OS_SYSTEM_METHODDEF |
| 13519 | OS_UMASK_METHODDEF |
| 13520 | OS_UNAME_METHODDEF |
| 13521 | OS_UNLINK_METHODDEF |
| 13522 | OS_REMOVE_METHODDEF |
| 13523 | OS_UTIME_METHODDEF |
| 13524 | OS_TIMES_METHODDEF |
| 13525 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 13526 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13527 | OS_EXECV_METHODDEF |
| 13528 | OS_EXECVE_METHODDEF |
| 13529 | OS_SPAWNV_METHODDEF |
| 13530 | OS_SPAWNVE_METHODDEF |
| 13531 | OS_FORK1_METHODDEF |
| 13532 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 13533 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13534 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 13535 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 13536 | OS_SCHED_GETPARAM_METHODDEF |
| 13537 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 13538 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 13539 | OS_SCHED_SETPARAM_METHODDEF |
| 13540 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 13541 | OS_SCHED_YIELD_METHODDEF |
| 13542 | OS_SCHED_SETAFFINITY_METHODDEF |
| 13543 | OS_SCHED_GETAFFINITY_METHODDEF |
| 13544 | OS_OPENPTY_METHODDEF |
| 13545 | OS_FORKPTY_METHODDEF |
| 13546 | OS_GETEGID_METHODDEF |
| 13547 | OS_GETEUID_METHODDEF |
| 13548 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 13549 | #ifdef HAVE_GETGROUPLIST |
| 13550 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 13551 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13552 | OS_GETGROUPS_METHODDEF |
| 13553 | OS_GETPID_METHODDEF |
| 13554 | OS_GETPGRP_METHODDEF |
| 13555 | OS_GETPPID_METHODDEF |
| 13556 | OS_GETUID_METHODDEF |
| 13557 | OS_GETLOGIN_METHODDEF |
| 13558 | OS_KILL_METHODDEF |
| 13559 | OS_KILLPG_METHODDEF |
| 13560 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13561 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13562 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13563 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13564 | OS_SETUID_METHODDEF |
| 13565 | OS_SETEUID_METHODDEF |
| 13566 | OS_SETREUID_METHODDEF |
| 13567 | OS_SETGID_METHODDEF |
| 13568 | OS_SETEGID_METHODDEF |
| 13569 | OS_SETREGID_METHODDEF |
| 13570 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13571 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13572 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13573 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13574 | OS_GETPGID_METHODDEF |
| 13575 | OS_SETPGRP_METHODDEF |
| 13576 | OS_WAIT_METHODDEF |
| 13577 | OS_WAIT3_METHODDEF |
| 13578 | OS_WAIT4_METHODDEF |
| 13579 | OS_WAITID_METHODDEF |
| 13580 | OS_WAITPID_METHODDEF |
| 13581 | OS_GETSID_METHODDEF |
| 13582 | OS_SETSID_METHODDEF |
| 13583 | OS_SETPGID_METHODDEF |
| 13584 | OS_TCGETPGRP_METHODDEF |
| 13585 | OS_TCSETPGRP_METHODDEF |
| 13586 | OS_OPEN_METHODDEF |
| 13587 | OS_CLOSE_METHODDEF |
| 13588 | OS_CLOSERANGE_METHODDEF |
| 13589 | OS_DEVICE_ENCODING_METHODDEF |
| 13590 | OS_DUP_METHODDEF |
| 13591 | OS_DUP2_METHODDEF |
| 13592 | OS_LOCKF_METHODDEF |
| 13593 | OS_LSEEK_METHODDEF |
| 13594 | OS_READ_METHODDEF |
| 13595 | OS_READV_METHODDEF |
| 13596 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13597 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13598 | OS_WRITE_METHODDEF |
| 13599 | OS_WRITEV_METHODDEF |
| 13600 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13601 | OS_PWRITEV_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13602 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 13603 | {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13604 | posix_sendfile__doc__}, |
| 13605 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13606 | OS_FSTAT_METHODDEF |
| 13607 | OS_ISATTY_METHODDEF |
| 13608 | OS_PIPE_METHODDEF |
| 13609 | OS_PIPE2_METHODDEF |
| 13610 | OS_MKFIFO_METHODDEF |
| 13611 | OS_MKNOD_METHODDEF |
| 13612 | OS_MAJOR_METHODDEF |
| 13613 | OS_MINOR_METHODDEF |
| 13614 | OS_MAKEDEV_METHODDEF |
| 13615 | OS_FTRUNCATE_METHODDEF |
| 13616 | OS_TRUNCATE_METHODDEF |
| 13617 | OS_POSIX_FALLOCATE_METHODDEF |
| 13618 | OS_POSIX_FADVISE_METHODDEF |
| 13619 | OS_PUTENV_METHODDEF |
| 13620 | OS_UNSETENV_METHODDEF |
| 13621 | OS_STRERROR_METHODDEF |
| 13622 | OS_FCHDIR_METHODDEF |
| 13623 | OS_FSYNC_METHODDEF |
| 13624 | OS_SYNC_METHODDEF |
| 13625 | OS_FDATASYNC_METHODDEF |
| 13626 | OS_WCOREDUMP_METHODDEF |
| 13627 | OS_WIFCONTINUED_METHODDEF |
| 13628 | OS_WIFSTOPPED_METHODDEF |
| 13629 | OS_WIFSIGNALED_METHODDEF |
| 13630 | OS_WIFEXITED_METHODDEF |
| 13631 | OS_WEXITSTATUS_METHODDEF |
| 13632 | OS_WTERMSIG_METHODDEF |
| 13633 | OS_WSTOPSIG_METHODDEF |
| 13634 | OS_FSTATVFS_METHODDEF |
| 13635 | OS_STATVFS_METHODDEF |
| 13636 | OS_CONFSTR_METHODDEF |
| 13637 | OS_SYSCONF_METHODDEF |
| 13638 | OS_FPATHCONF_METHODDEF |
| 13639 | OS_PATHCONF_METHODDEF |
| 13640 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 13641 | OS__GETFULLPATHNAME_METHODDEF |
| 13642 | OS__ISDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13643 | OS__GETDISKUSAGE_METHODDEF |
| 13644 | OS__GETFINALPATHNAME_METHODDEF |
| 13645 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 13646 | OS_GETLOADAVG_METHODDEF |
| 13647 | OS_URANDOM_METHODDEF |
| 13648 | OS_SETRESUID_METHODDEF |
| 13649 | OS_SETRESGID_METHODDEF |
| 13650 | OS_GETRESUID_METHODDEF |
| 13651 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 13652 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13653 | OS_GETXATTR_METHODDEF |
| 13654 | OS_SETXATTR_METHODDEF |
| 13655 | OS_REMOVEXATTR_METHODDEF |
| 13656 | OS_LISTXATTR_METHODDEF |
| 13657 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13658 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 13659 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 13660 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13661 | OS_CPU_COUNT_METHODDEF |
| 13662 | OS_GET_INHERITABLE_METHODDEF |
| 13663 | OS_SET_INHERITABLE_METHODDEF |
| 13664 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 13665 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13666 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13667 | OS_GET_BLOCKING_METHODDEF |
| 13668 | OS_SET_BLOCKING_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13669 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13670 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13671 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13672 | OS_GETRANDOM_METHODDEF |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 13673 | OS_MEMFD_CREATE_METHODDEF |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13674 | #ifdef MS_WINDOWS |
| 13675 | OS__ADD_DLL_DIRECTORY_METHODDEF |
| 13676 | OS__REMOVE_DLL_DIRECTORY_METHODDEF |
| 13677 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13678 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13679 | }; |
| 13680 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13681 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13682 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13683 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13684 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13685 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13686 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13687 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13688 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13689 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13690 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13691 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13692 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13693 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13694 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13695 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13696 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13697 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13698 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13699 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13700 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13701 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13702 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13703 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13704 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13705 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13706 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13707 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13708 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13709 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13710 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13711 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13712 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13713 | #endif |
| 13714 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13715 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13716 | #endif |
| 13717 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13718 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13719 | #endif |
| 13720 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13721 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13722 | #endif |
| 13723 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13724 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13725 | #endif |
| 13726 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13727 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13728 | #endif |
| 13729 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13730 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13731 | #endif |
| 13732 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13733 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13734 | #endif |
| 13735 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13736 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13737 | #endif |
| 13738 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13739 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13740 | #endif |
| 13741 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13742 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13743 | #endif |
| 13744 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13745 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13746 | #endif |
| 13747 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13748 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13749 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13750 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13751 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13752 | #endif |
| 13753 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13754 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13755 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13756 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13757 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13758 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13759 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13760 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13761 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13762 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13763 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13764 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13765 | #endif |
| 13766 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13767 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13768 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13769 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13770 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13771 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13772 | #endif |
| 13773 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13774 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13775 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13776 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13777 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13778 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13779 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13780 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13781 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 13782 | #ifdef O_TMPFILE |
| 13783 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 13784 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13785 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13786 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13787 | #endif |
| 13788 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13789 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13790 | #endif |
| 13791 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13792 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13793 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13794 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13795 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13796 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13797 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13798 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13799 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13800 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13801 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13802 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13803 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13804 | #endif |
| 13805 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13806 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13807 | #endif |
| 13808 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13809 | /* MS Windows */ |
| 13810 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13811 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13812 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13813 | #endif |
| 13814 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13815 | /* Optimize for short life (keep in memory). */ |
| 13816 | /* 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] | 13817 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13818 | #endif |
| 13819 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13820 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13821 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13822 | #endif |
| 13823 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13824 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13825 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13826 | #endif |
| 13827 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13828 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13829 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13830 | #endif |
| 13831 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13832 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13833 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13834 | /* Send a SIGIO signal whenever input or output |
| 13835 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13836 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13837 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13838 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13839 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13840 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13841 | #endif |
| 13842 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13843 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13844 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13845 | #endif |
| 13846 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13847 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13848 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13849 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13850 | #ifdef O_NOLINKS |
| 13851 | /* 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] | 13852 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13853 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13854 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13855 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13856 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13857 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 13858 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13859 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13860 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13861 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13862 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13863 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13864 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13865 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13866 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13867 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13868 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13869 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13870 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13871 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13872 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13873 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13874 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13875 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13876 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13877 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13878 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13879 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13880 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13881 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13882 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13883 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13884 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13885 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13886 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13887 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13888 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13889 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13890 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13891 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13892 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13893 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13894 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13895 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13896 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13897 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13898 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13899 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13900 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13901 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13902 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13903 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13904 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13905 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13906 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13907 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13908 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13909 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13910 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13911 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 13912 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13913 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13914 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13915 | #endif /* ST_RDONLY */ |
| 13916 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13917 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13918 | #endif /* ST_NOSUID */ |
| 13919 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 13920 | /* GNU extensions */ |
| 13921 | #ifdef ST_NODEV |
| 13922 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 13923 | #endif /* ST_NODEV */ |
| 13924 | #ifdef ST_NOEXEC |
| 13925 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 13926 | #endif /* ST_NOEXEC */ |
| 13927 | #ifdef ST_SYNCHRONOUS |
| 13928 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 13929 | #endif /* ST_SYNCHRONOUS */ |
| 13930 | #ifdef ST_MANDLOCK |
| 13931 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 13932 | #endif /* ST_MANDLOCK */ |
| 13933 | #ifdef ST_WRITE |
| 13934 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 13935 | #endif /* ST_WRITE */ |
| 13936 | #ifdef ST_APPEND |
| 13937 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 13938 | #endif /* ST_APPEND */ |
| 13939 | #ifdef ST_NOATIME |
| 13940 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 13941 | #endif /* ST_NOATIME */ |
| 13942 | #ifdef ST_NODIRATIME |
| 13943 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 13944 | #endif /* ST_NODIRATIME */ |
| 13945 | #ifdef ST_RELATIME |
| 13946 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 13947 | #endif /* ST_RELATIME */ |
| 13948 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13949 | /* FreeBSD sendfile() constants */ |
| 13950 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13951 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13952 | #endif |
| 13953 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13954 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13955 | #endif |
| 13956 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13957 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13958 | #endif |
| 13959 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13960 | /* constants for posix_fadvise */ |
| 13961 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13962 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13963 | #endif |
| 13964 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13965 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13966 | #endif |
| 13967 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13968 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13969 | #endif |
| 13970 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13971 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13972 | #endif |
| 13973 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13974 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13975 | #endif |
| 13976 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13977 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13978 | #endif |
| 13979 | |
| 13980 | /* constants for waitid */ |
| 13981 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13982 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 13983 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 13984 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13985 | #endif |
| 13986 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13987 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13988 | #endif |
| 13989 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13990 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13991 | #endif |
| 13992 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13993 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13994 | #endif |
| 13995 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13996 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13997 | #endif |
| 13998 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13999 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14000 | #endif |
| 14001 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14002 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14003 | #endif |
| 14004 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14005 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14006 | #endif |
| 14007 | |
| 14008 | /* constants for lockf */ |
| 14009 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14010 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14011 | #endif |
| 14012 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14013 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14014 | #endif |
| 14015 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14016 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14017 | #endif |
| 14018 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14019 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14020 | #endif |
| 14021 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14022 | #ifdef RWF_DSYNC |
| 14023 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 14024 | #endif |
| 14025 | #ifdef RWF_HIPRI |
| 14026 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 14027 | #endif |
| 14028 | #ifdef RWF_SYNC |
| 14029 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 14030 | #endif |
| 14031 | #ifdef RWF_NOWAIT |
| 14032 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 14033 | #endif |
| 14034 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14035 | /* constants for posix_spawn */ |
| 14036 | #ifdef HAVE_POSIX_SPAWN |
| 14037 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 14038 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 14039 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 14040 | #endif |
| 14041 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14042 | #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14043 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 14044 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14045 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14046 | #endif |
| 14047 | #ifdef HAVE_SPAWNV |
| 14048 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14049 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 14050 | #endif |
| 14051 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14052 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14053 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14054 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14055 | #endif |
| 14056 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14057 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14058 | #endif |
| 14059 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14060 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14061 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14062 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 14063 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14064 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14065 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14066 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14067 | #endif |
| 14068 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14069 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14070 | #endif |
| 14071 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14072 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14073 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14074 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14075 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14076 | #endif |
| 14077 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14078 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14079 | #endif |
| 14080 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14081 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14082 | #endif |
| 14083 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14084 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14085 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14086 | #endif |
| 14087 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 14088 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14089 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 14090 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 14091 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 14092 | #endif |
| 14093 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14094 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14095 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14096 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14097 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14098 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14099 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14100 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14101 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14102 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14103 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14104 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14105 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14106 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14107 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14108 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14109 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14110 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14111 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14112 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14113 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14114 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 14115 | #if HAVE_DECL_RTLD_MEMBER |
| 14116 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 14117 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14118 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14119 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 14120 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 14121 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 14122 | #endif |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14123 | #ifdef HAVE_MEMFD_CREATE |
| 14124 | if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1; |
| 14125 | if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1; |
| 14126 | #ifdef MFD_HUGETLB |
| 14127 | if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14128 | #endif |
| 14129 | #ifdef MFD_HUGE_SHIFT |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14130 | if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14131 | #endif |
| 14132 | #ifdef MFD_HUGE_MASK |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14133 | if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14134 | #endif |
| 14135 | #ifdef MFD_HUGE_64KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14136 | if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14137 | #endif |
| 14138 | #ifdef MFD_HUGE_512KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14139 | if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14140 | #endif |
| 14141 | #ifdef MFD_HUGE_1MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14142 | if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14143 | #endif |
| 14144 | #ifdef MFD_HUGE_2MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14145 | if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14146 | #endif |
| 14147 | #ifdef MFD_HUGE_8MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14148 | if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14149 | #endif |
| 14150 | #ifdef MFD_HUGE_16MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14151 | if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14152 | #endif |
| 14153 | #ifdef MFD_HUGE_32MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14154 | if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14155 | #endif |
| 14156 | #ifdef MFD_HUGE_256MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14157 | if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14158 | #endif |
| 14159 | #ifdef MFD_HUGE_512MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14160 | if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14161 | #endif |
| 14162 | #ifdef MFD_HUGE_1GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14163 | if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14164 | #endif |
| 14165 | #ifdef MFD_HUGE_2GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14166 | if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14167 | #endif |
| 14168 | #ifdef MFD_HUGE_16GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14169 | if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1; |
| 14170 | #endif |
| 14171 | #endif |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14172 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14173 | #if defined(__APPLE__) |
| 14174 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 14175 | #endif |
| 14176 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14177 | #ifdef MS_WINDOWS |
| 14178 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1; |
| 14179 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1; |
| 14180 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1; |
| 14181 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1; |
| 14182 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1; |
| 14183 | #endif |
| 14184 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14185 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14186 | } |
| 14187 | |
| 14188 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14189 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14190 | PyModuleDef_HEAD_INIT, |
| 14191 | MODNAME, |
| 14192 | posix__doc__, |
| 14193 | -1, |
| 14194 | posix_methods, |
| 14195 | NULL, |
| 14196 | NULL, |
| 14197 | NULL, |
| 14198 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14199 | }; |
| 14200 | |
| 14201 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14202 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14203 | |
| 14204 | #ifdef HAVE_FACCESSAT |
| 14205 | "HAVE_FACCESSAT", |
| 14206 | #endif |
| 14207 | |
| 14208 | #ifdef HAVE_FCHDIR |
| 14209 | "HAVE_FCHDIR", |
| 14210 | #endif |
| 14211 | |
| 14212 | #ifdef HAVE_FCHMOD |
| 14213 | "HAVE_FCHMOD", |
| 14214 | #endif |
| 14215 | |
| 14216 | #ifdef HAVE_FCHMODAT |
| 14217 | "HAVE_FCHMODAT", |
| 14218 | #endif |
| 14219 | |
| 14220 | #ifdef HAVE_FCHOWN |
| 14221 | "HAVE_FCHOWN", |
| 14222 | #endif |
| 14223 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 14224 | #ifdef HAVE_FCHOWNAT |
| 14225 | "HAVE_FCHOWNAT", |
| 14226 | #endif |
| 14227 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14228 | #ifdef HAVE_FEXECVE |
| 14229 | "HAVE_FEXECVE", |
| 14230 | #endif |
| 14231 | |
| 14232 | #ifdef HAVE_FDOPENDIR |
| 14233 | "HAVE_FDOPENDIR", |
| 14234 | #endif |
| 14235 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14236 | #ifdef HAVE_FPATHCONF |
| 14237 | "HAVE_FPATHCONF", |
| 14238 | #endif |
| 14239 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14240 | #ifdef HAVE_FSTATAT |
| 14241 | "HAVE_FSTATAT", |
| 14242 | #endif |
| 14243 | |
| 14244 | #ifdef HAVE_FSTATVFS |
| 14245 | "HAVE_FSTATVFS", |
| 14246 | #endif |
| 14247 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 14248 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14249 | "HAVE_FTRUNCATE", |
| 14250 | #endif |
| 14251 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14252 | #ifdef HAVE_FUTIMENS |
| 14253 | "HAVE_FUTIMENS", |
| 14254 | #endif |
| 14255 | |
| 14256 | #ifdef HAVE_FUTIMES |
| 14257 | "HAVE_FUTIMES", |
| 14258 | #endif |
| 14259 | |
| 14260 | #ifdef HAVE_FUTIMESAT |
| 14261 | "HAVE_FUTIMESAT", |
| 14262 | #endif |
| 14263 | |
| 14264 | #ifdef HAVE_LINKAT |
| 14265 | "HAVE_LINKAT", |
| 14266 | #endif |
| 14267 | |
| 14268 | #ifdef HAVE_LCHFLAGS |
| 14269 | "HAVE_LCHFLAGS", |
| 14270 | #endif |
| 14271 | |
| 14272 | #ifdef HAVE_LCHMOD |
| 14273 | "HAVE_LCHMOD", |
| 14274 | #endif |
| 14275 | |
| 14276 | #ifdef HAVE_LCHOWN |
| 14277 | "HAVE_LCHOWN", |
| 14278 | #endif |
| 14279 | |
| 14280 | #ifdef HAVE_LSTAT |
| 14281 | "HAVE_LSTAT", |
| 14282 | #endif |
| 14283 | |
| 14284 | #ifdef HAVE_LUTIMES |
| 14285 | "HAVE_LUTIMES", |
| 14286 | #endif |
| 14287 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14288 | #ifdef HAVE_MEMFD_CREATE |
| 14289 | "HAVE_MEMFD_CREATE", |
| 14290 | #endif |
| 14291 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14292 | #ifdef HAVE_MKDIRAT |
| 14293 | "HAVE_MKDIRAT", |
| 14294 | #endif |
| 14295 | |
| 14296 | #ifdef HAVE_MKFIFOAT |
| 14297 | "HAVE_MKFIFOAT", |
| 14298 | #endif |
| 14299 | |
| 14300 | #ifdef HAVE_MKNODAT |
| 14301 | "HAVE_MKNODAT", |
| 14302 | #endif |
| 14303 | |
| 14304 | #ifdef HAVE_OPENAT |
| 14305 | "HAVE_OPENAT", |
| 14306 | #endif |
| 14307 | |
| 14308 | #ifdef HAVE_READLINKAT |
| 14309 | "HAVE_READLINKAT", |
| 14310 | #endif |
| 14311 | |
| 14312 | #ifdef HAVE_RENAMEAT |
| 14313 | "HAVE_RENAMEAT", |
| 14314 | #endif |
| 14315 | |
| 14316 | #ifdef HAVE_SYMLINKAT |
| 14317 | "HAVE_SYMLINKAT", |
| 14318 | #endif |
| 14319 | |
| 14320 | #ifdef HAVE_UNLINKAT |
| 14321 | "HAVE_UNLINKAT", |
| 14322 | #endif |
| 14323 | |
| 14324 | #ifdef HAVE_UTIMENSAT |
| 14325 | "HAVE_UTIMENSAT", |
| 14326 | #endif |
| 14327 | |
| 14328 | #ifdef MS_WINDOWS |
| 14329 | "MS_WINDOWS", |
| 14330 | #endif |
| 14331 | |
| 14332 | NULL |
| 14333 | }; |
| 14334 | |
| 14335 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 14336 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 14337 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14338 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14339 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14340 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14341 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14342 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14343 | m = PyModule_Create(&posixmodule); |
| 14344 | if (m == NULL) |
| 14345 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14346 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14347 | /* Initialize environ dictionary */ |
| 14348 | v = convertenviron(); |
| 14349 | Py_XINCREF(v); |
| 14350 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 14351 | return NULL; |
| 14352 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14353 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14354 | if (all_ins(m)) |
| 14355 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14356 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14357 | if (setup_confname_tables(m)) |
| 14358 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14359 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14360 | Py_INCREF(PyExc_OSError); |
| 14361 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 14362 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 14363 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14364 | if (posix_putenv_garbage == NULL) |
| 14365 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 14366 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 14367 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14368 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14369 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 14370 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14371 | WaitidResultType = PyStructSequence_NewType(&waitid_result_desc); |
| 14372 | if (WaitidResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14373 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14374 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14375 | #endif |
| 14376 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 14377 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14378 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 14379 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 14380 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14381 | StatResultType = PyStructSequence_NewType(&stat_result_desc); |
| 14382 | if (StatResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14383 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14384 | } |
| 14385 | structseq_new = StatResultType->tp_new; |
| 14386 | StatResultType->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14387 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 14388 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14389 | StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc); |
| 14390 | if (StatVFSResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14391 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14392 | } |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14393 | #ifdef NEED_TICKS_PER_SECOND |
| 14394 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14395 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14396 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14397 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14398 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14399 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14400 | # endif |
| 14401 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14402 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 14403 | #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] | 14404 | sched_param_desc.name = MODNAME ".sched_param"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14405 | SchedParamType = PyStructSequence_NewType(&sched_param_desc); |
| 14406 | if (SchedParamType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14407 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14408 | } |
| 14409 | SchedParamType->tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14410 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14411 | |
| 14412 | /* initialize TerminalSize_info */ |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14413 | TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc); |
| 14414 | if (TerminalSizeType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14415 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14416 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14417 | |
| 14418 | /* initialize scandir types */ |
| 14419 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 14420 | return NULL; |
| 14421 | if (PyType_Ready(&DirEntryType) < 0) |
| 14422 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14423 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14424 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14425 | Py_INCREF((PyObject*) WaitidResultType); |
| 14426 | PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14427 | #endif |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14428 | Py_INCREF((PyObject*) StatResultType); |
| 14429 | PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType); |
| 14430 | Py_INCREF((PyObject*) StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14431 | PyModule_AddObject(m, "statvfs_result", |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14432 | (PyObject*) StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14433 | |
| 14434 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14435 | Py_INCREF(SchedParamType); |
| 14436 | PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14437 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14438 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14439 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14440 | TimesResultType = PyStructSequence_NewType(×_result_desc); |
| 14441 | if (TimesResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14442 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14443 | } |
| 14444 | PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14445 | |
| 14446 | uname_result_desc.name = MODNAME ".uname_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14447 | UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
| 14448 | if (UnameResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14449 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14450 | } |
| 14451 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14452 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14453 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14454 | /* |
| 14455 | * Step 2 of weak-linking support on Mac OS X. |
| 14456 | * |
| 14457 | * The code below removes functions that are not available on the |
| 14458 | * currently active platform. |
| 14459 | * |
| 14460 | * 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] | 14461 | * 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] | 14462 | * OSX 10.4. |
| 14463 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14464 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14465 | if (fstatvfs == NULL) { |
| 14466 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 14467 | return NULL; |
| 14468 | } |
| 14469 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14470 | #endif /* HAVE_FSTATVFS */ |
| 14471 | |
| 14472 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14473 | if (statvfs == NULL) { |
| 14474 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 14475 | return NULL; |
| 14476 | } |
| 14477 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14478 | #endif /* HAVE_STATVFS */ |
| 14479 | |
| 14480 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14481 | if (lchown == NULL) { |
| 14482 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 14483 | return NULL; |
| 14484 | } |
| 14485 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14486 | #endif /* HAVE_LCHOWN */ |
| 14487 | |
| 14488 | |
| 14489 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14490 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14491 | Py_INCREF(TerminalSizeType); |
| 14492 | PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14493 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 14494 | billion = PyLong_FromLong(1000000000); |
| 14495 | if (!billion) |
| 14496 | return NULL; |
| 14497 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14498 | /* suppress "function not used" warnings */ |
| 14499 | { |
| 14500 | int ignored; |
| 14501 | fd_specified("", -1); |
| 14502 | follow_symlinks_specified("", 1); |
| 14503 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 14504 | dir_fd_converter(Py_None, &ignored); |
| 14505 | dir_fd_unavailable(Py_None, &ignored); |
| 14506 | } |
| 14507 | |
| 14508 | /* |
| 14509 | * provide list of locally available functions |
| 14510 | * so os.py can populate support_* lists |
| 14511 | */ |
| 14512 | list = PyList_New(0); |
| 14513 | if (!list) |
| 14514 | return NULL; |
| 14515 | for (trace = have_functions; *trace; trace++) { |
| 14516 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 14517 | if (!unicode) |
| 14518 | return NULL; |
| 14519 | if (PyList_Append(list, unicode)) |
| 14520 | return NULL; |
| 14521 | Py_DECREF(unicode); |
| 14522 | } |
| 14523 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 14524 | |
| 14525 | Py_INCREF((PyObject *) &DirEntryType); |
Brett Cannon | a32c4d0 | 2016-06-24 14:14:44 -0700 | [diff] [blame] | 14526 | PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14527 | |
| 14528 | initialized = 1; |
| 14529 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14530 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14531 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14532 | |
| 14533 | #ifdef __cplusplus |
| 14534 | } |
| 14535 | #endif |