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() */ |
| 39 | #include "pycore_pystate.h" /* _PyRuntime */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 40 | #include "pythread.h" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 41 | #include "structmember.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 42 | #ifndef MS_WINDOWS |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 43 | # include "posixmodule.h" |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 44 | #else |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 45 | # include "winreparse.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 46 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 47 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 48 | /* On android API level 21, 'AT_EACCESS' is not declared although |
| 49 | * HAVE_FACCESSAT is defined. */ |
| 50 | #ifdef __ANDROID__ |
| 51 | #undef HAVE_FACCESSAT |
| 52 | #endif |
| 53 | |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | fa76eee | 2016-05-28 21:03:48 +0000 | [diff] [blame] | 54 | #include <stdio.h> /* needed for ctermid() */ |
| 55 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 56 | #ifdef __cplusplus |
| 57 | extern "C" { |
| 58 | #endif |
| 59 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 60 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 61 | "This module provides access to operating system functionality that is\n\ |
| 62 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 63 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 64 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 66 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 67 | #ifdef HAVE_SYS_UIO_H |
| 68 | #include <sys/uio.h> |
| 69 | #endif |
| 70 | |
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 71 | #ifdef HAVE_SYS_SYSMACROS_H |
| 72 | /* GNU C Library: major(), minor(), makedev() */ |
| 73 | #include <sys/sysmacros.h> |
| 74 | #endif |
| 75 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | #endif /* HAVE_SYS_TYPES_H */ |
| 79 | |
| 80 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 81 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 82 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 84 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 85 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 86 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 87 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 88 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 89 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 90 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 92 | #ifdef HAVE_FCNTL_H |
| 93 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 94 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 96 | #ifdef HAVE_GRP_H |
| 97 | #include <grp.h> |
| 98 | #endif |
| 99 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 100 | #ifdef HAVE_SYSEXITS_H |
| 101 | #include <sysexits.h> |
| 102 | #endif /* HAVE_SYSEXITS_H */ |
| 103 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 104 | #ifdef HAVE_SYS_LOADAVG_H |
| 105 | #include <sys/loadavg.h> |
| 106 | #endif |
| 107 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 108 | #ifdef HAVE_SYS_SENDFILE_H |
| 109 | #include <sys/sendfile.h> |
| 110 | #endif |
| 111 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 112 | #if defined(__APPLE__) |
| 113 | #include <copyfile.h> |
| 114 | #endif |
| 115 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 116 | #ifdef HAVE_SCHED_H |
| 117 | #include <sched.h> |
| 118 | #endif |
| 119 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 120 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 121 | #undef HAVE_SCHED_SETAFFINITY |
| 122 | #endif |
| 123 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 124 | #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] | 125 | #define USE_XATTRS |
| 126 | #endif |
| 127 | |
| 128 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 129 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 130 | #endif |
| 131 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 132 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 133 | #ifdef HAVE_SYS_SOCKET_H |
| 134 | #include <sys/socket.h> |
| 135 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 136 | #endif |
| 137 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 138 | #ifdef HAVE_DLFCN_H |
| 139 | #include <dlfcn.h> |
| 140 | #endif |
| 141 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 142 | #ifdef __hpux |
| 143 | #include <sys/mpctl.h> |
| 144 | #endif |
| 145 | |
| 146 | #if defined(__DragonFly__) || \ |
| 147 | defined(__OpenBSD__) || \ |
| 148 | defined(__FreeBSD__) || \ |
| 149 | defined(__NetBSD__) || \ |
| 150 | defined(__APPLE__) |
| 151 | #include <sys/sysctl.h> |
| 152 | #endif |
| 153 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 154 | #ifdef HAVE_LINUX_RANDOM_H |
| 155 | # include <linux/random.h> |
| 156 | #endif |
| 157 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 158 | # include <sys/syscall.h> |
| 159 | #endif |
| 160 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 161 | #if defined(MS_WINDOWS) |
| 162 | # define TERMSIZE_USE_CONIO |
| 163 | #elif defined(HAVE_SYS_IOCTL_H) |
| 164 | # include <sys/ioctl.h> |
| 165 | # if defined(HAVE_TERMIOS_H) |
| 166 | # include <termios.h> |
| 167 | # endif |
| 168 | # if defined(TIOCGWINSZ) |
| 169 | # define TERMSIZE_USE_IOCTL |
| 170 | # endif |
| 171 | #endif /* MS_WINDOWS */ |
| 172 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 174 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 175 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 176 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 177 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 178 | #include <process.h> |
| 179 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 180 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 181 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 182 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 183 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 184 | #define HAVE_EXECV 1 |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 185 | #define HAVE_WSPAWNV 1 |
| 186 | #define HAVE_WEXECV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 187 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 188 | #define HAVE_SYSTEM 1 |
| 189 | #define HAVE_CWAIT 1 |
| 190 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 191 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 192 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 193 | /* Unix functions that the configure script doesn't check for */ |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 194 | #ifndef __VXWORKS__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 195 | #define HAVE_EXECV 1 |
| 196 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 197 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 198 | #define HAVE_FORK1 1 |
| 199 | #endif |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 200 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 201 | #define HAVE_GETEGID 1 |
| 202 | #define HAVE_GETEUID 1 |
| 203 | #define HAVE_GETGID 1 |
| 204 | #define HAVE_GETPPID 1 |
| 205 | #define HAVE_GETUID 1 |
| 206 | #define HAVE_KILL 1 |
| 207 | #define HAVE_OPENDIR 1 |
| 208 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 209 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 210 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 211 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 212 | #endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 213 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 214 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 215 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 216 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 217 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 218 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 219 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 220 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 221 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 222 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 223 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 224 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 225 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 226 | (default) */ |
| 227 | extern char *ctermid_r(char *); |
| 228 | #endif |
| 229 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 230 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 231 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 232 | #if defined(__VXWORKS__) |
| 233 | #include <vxCpuLib.h> |
| 234 | #include <rtpLib.h> |
| 235 | #include <wait.h> |
| 236 | #include <taskLib.h> |
| 237 | #ifndef _P_WAIT |
| 238 | #define _P_WAIT 0 |
| 239 | #define _P_NOWAIT 1 |
| 240 | #define _P_NOWAITO 1 |
| 241 | #endif |
| 242 | #endif /* __VXWORKS__ */ |
| 243 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 244 | #ifdef HAVE_POSIX_SPAWN |
| 245 | #include <spawn.h> |
| 246 | #endif |
| 247 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | #ifdef HAVE_UTIME_H |
| 249 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 250 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 252 | #ifdef HAVE_SYS_UTIME_H |
| 253 | #include <sys/utime.h> |
| 254 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 255 | #endif /* HAVE_SYS_UTIME_H */ |
| 256 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #ifdef HAVE_SYS_TIMES_H |
| 258 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 259 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | |
| 261 | #ifdef HAVE_SYS_PARAM_H |
| 262 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 263 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 264 | |
| 265 | #ifdef HAVE_SYS_UTSNAME_H |
| 266 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 267 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 269 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 271 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 272 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 273 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 274 | #include <direct.h> |
| 275 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 276 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 277 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 278 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 279 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 280 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 281 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 282 | #endif |
| 283 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 284 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 285 | #endif |
| 286 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 287 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 288 | #endif |
| 289 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 290 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 291 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 292 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 293 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 294 | #endif |
| 295 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 296 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 297 | #endif |
| 298 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 299 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 300 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 301 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 302 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 303 | #endif |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 304 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 305 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 306 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 307 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 308 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 309 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 310 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 311 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 312 | #define HAVE_SYMLINK |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 313 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 314 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 315 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 316 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 317 | #define MAXPATHLEN PATH_MAX |
| 318 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 319 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 320 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 321 | #endif /* MAXPATHLEN */ |
| 322 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 323 | #ifdef UNION_WAIT |
| 324 | /* Emulate some macros on systems that have a union instead of macros */ |
| 325 | |
| 326 | #ifndef WIFEXITED |
| 327 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 328 | #endif |
| 329 | |
| 330 | #ifndef WEXITSTATUS |
| 331 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 332 | #endif |
| 333 | |
| 334 | #ifndef WTERMSIG |
| 335 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 336 | #endif |
| 337 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 338 | #define WAIT_TYPE union wait |
| 339 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 340 | |
| 341 | #else /* !UNION_WAIT */ |
| 342 | #define WAIT_TYPE int |
| 343 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 344 | #endif /* UNION_WAIT */ |
| 345 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 346 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 347 | prototype for it, at least on Solaris -- maybe others as well?). */ |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 348 | #if defined(HAVE_CTERMID_R) |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 349 | #define USE_CTERMID_R |
| 350 | #endif |
| 351 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 352 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 353 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 354 | #undef FSTAT |
| 355 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 356 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 357 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 358 | # define LSTAT win32_lstat |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 359 | # define FSTAT _Py_fstat_noraise |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 360 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 361 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 362 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 363 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 364 | # define FSTAT fstat |
| 365 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 366 | #endif |
| 367 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 368 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 369 | #include <sys/mkdev.h> |
| 370 | #else |
| 371 | #if defined(MAJOR_IN_SYSMACROS) |
| 372 | #include <sys/sysmacros.h> |
| 373 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 374 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 375 | #include <sys/mkdev.h> |
| 376 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 377 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 378 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 379 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 380 | #define INITFUNC PyInit_nt |
| 381 | #define MODNAME "nt" |
| 382 | #else |
| 383 | #define INITFUNC PyInit_posix |
| 384 | #define MODNAME "posix" |
| 385 | #endif |
| 386 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 387 | #if defined(__sun) |
| 388 | /* Something to implement in autoconf, not present in autoconf 2.69 */ |
| 389 | #define HAVE_STRUCT_STAT_ST_FSTYPE 1 |
| 390 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 391 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 392 | #ifdef _Py_MEMORY_SANITIZER |
| 393 | # include <sanitizer/msan_interface.h> |
| 394 | #endif |
| 395 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 396 | #ifdef HAVE_FORK |
| 397 | static void |
| 398 | run_at_forkers(PyObject *lst, int reverse) |
| 399 | { |
| 400 | Py_ssize_t i; |
| 401 | PyObject *cpy; |
| 402 | |
| 403 | if (lst != NULL) { |
| 404 | assert(PyList_CheckExact(lst)); |
| 405 | |
| 406 | /* Use a list copy in case register_at_fork() is called from |
| 407 | * one of the callbacks. |
| 408 | */ |
| 409 | cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst)); |
| 410 | if (cpy == NULL) |
| 411 | PyErr_WriteUnraisable(lst); |
| 412 | else { |
| 413 | if (reverse) |
| 414 | PyList_Reverse(cpy); |
| 415 | for (i = 0; i < PyList_GET_SIZE(cpy); i++) { |
| 416 | PyObject *func, *res; |
| 417 | func = PyList_GET_ITEM(cpy, i); |
| 418 | res = PyObject_CallObject(func, NULL); |
| 419 | if (res == NULL) |
| 420 | PyErr_WriteUnraisable(func); |
| 421 | else |
| 422 | Py_DECREF(res); |
| 423 | } |
| 424 | Py_DECREF(cpy); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | void |
| 430 | PyOS_BeforeFork(void) |
| 431 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 432 | run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 433 | |
| 434 | _PyImport_AcquireLock(); |
| 435 | } |
| 436 | |
| 437 | void |
| 438 | PyOS_AfterFork_Parent(void) |
| 439 | { |
| 440 | if (_PyImport_ReleaseLock() <= 0) |
| 441 | Py_FatalError("failed releasing import lock after fork"); |
| 442 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 443 | run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void |
| 447 | PyOS_AfterFork_Child(void) |
| 448 | { |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 449 | _PyRuntimeState *runtime = &_PyRuntime; |
| 450 | _PyGILState_Reinit(runtime); |
| 451 | _PyInterpreterState_DeleteExceptMain(runtime); |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 452 | _PyEval_ReInitThreads(runtime); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 453 | _PyImport_ReInitLock(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 454 | _PySignal_AfterFork(); |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 455 | _PyRuntimeState_ReInitThreads(runtime); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 456 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 457 | run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static int |
| 461 | register_at_forker(PyObject **lst, PyObject *func) |
| 462 | { |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 463 | if (func == NULL) /* nothing to register? do nothing. */ |
| 464 | return 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 465 | if (*lst == NULL) { |
| 466 | *lst = PyList_New(0); |
| 467 | if (*lst == NULL) |
| 468 | return -1; |
| 469 | } |
| 470 | return PyList_Append(*lst, func); |
| 471 | } |
| 472 | #endif |
| 473 | |
| 474 | /* Legacy wrapper */ |
| 475 | void |
| 476 | PyOS_AfterFork(void) |
| 477 | { |
| 478 | #ifdef HAVE_FORK |
| 479 | PyOS_AfterFork_Child(); |
| 480 | #endif |
| 481 | } |
| 482 | |
| 483 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 484 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 485 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 486 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 487 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 488 | ULONG, struct _Py_stat_struct *); |
| 489 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 490 | |
| 491 | #ifdef MS_WINDOWS |
| 492 | static int |
| 493 | win32_warn_bytes_api() |
| 494 | { |
| 495 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 496 | "The Windows bytes API has been deprecated, " |
| 497 | "use Unicode filenames instead", |
| 498 | 1); |
| 499 | } |
| 500 | #endif |
| 501 | |
| 502 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 503 | #ifndef MS_WINDOWS |
| 504 | PyObject * |
| 505 | _PyLong_FromUid(uid_t uid) |
| 506 | { |
| 507 | if (uid == (uid_t)-1) |
| 508 | return PyLong_FromLong(-1); |
| 509 | return PyLong_FromUnsignedLong(uid); |
| 510 | } |
| 511 | |
| 512 | PyObject * |
| 513 | _PyLong_FromGid(gid_t gid) |
| 514 | { |
| 515 | if (gid == (gid_t)-1) |
| 516 | return PyLong_FromLong(-1); |
| 517 | return PyLong_FromUnsignedLong(gid); |
| 518 | } |
| 519 | |
| 520 | int |
| 521 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 522 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 523 | uid_t uid; |
| 524 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 525 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 526 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 527 | unsigned long uresult; |
| 528 | |
| 529 | index = PyNumber_Index(obj); |
| 530 | if (index == NULL) { |
| 531 | PyErr_Format(PyExc_TypeError, |
| 532 | "uid should be integer, not %.200s", |
| 533 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 534 | return 0; |
| 535 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * Handling uid_t is complicated for two reasons: |
| 539 | * * Although uid_t is (always?) unsigned, it still |
| 540 | * accepts -1. |
| 541 | * * We don't know its size in advance--it may be |
| 542 | * bigger than an int, or it may be smaller than |
| 543 | * a long. |
| 544 | * |
| 545 | * So a bit of defensive programming is in order. |
| 546 | * Start with interpreting the value passed |
| 547 | * in as a signed long and see if it works. |
| 548 | */ |
| 549 | |
| 550 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 551 | |
| 552 | if (!overflow) { |
| 553 | uid = (uid_t)result; |
| 554 | |
| 555 | if (result == -1) { |
| 556 | if (PyErr_Occurred()) |
| 557 | goto fail; |
| 558 | /* It's a legitimate -1, we're done. */ |
| 559 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 560 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 561 | |
| 562 | /* Any other negative number is disallowed. */ |
| 563 | if (result < 0) |
| 564 | goto underflow; |
| 565 | |
| 566 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 567 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 568 | (long)uid != result) |
| 569 | goto underflow; |
| 570 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 571 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 572 | |
| 573 | if (overflow < 0) |
| 574 | goto underflow; |
| 575 | |
| 576 | /* |
| 577 | * Okay, the value overflowed a signed long. If it |
| 578 | * fits in an *unsigned* long, it may still be okay, |
| 579 | * as uid_t may be unsigned long on this platform. |
| 580 | */ |
| 581 | uresult = PyLong_AsUnsignedLong(index); |
| 582 | if (PyErr_Occurred()) { |
| 583 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 584 | goto overflow; |
| 585 | goto fail; |
| 586 | } |
| 587 | |
| 588 | uid = (uid_t)uresult; |
| 589 | |
| 590 | /* |
| 591 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 592 | * but this value would get interpreted as (uid_t)-1 by chown |
| 593 | * and its siblings. That's not what the user meant! So we |
| 594 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 595 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 596 | */ |
| 597 | if (uid == (uid_t)-1) |
| 598 | goto overflow; |
| 599 | |
| 600 | /* Ensure the value wasn't truncated. */ |
| 601 | if (sizeof(uid_t) < sizeof(long) && |
| 602 | (unsigned long)uid != uresult) |
| 603 | goto overflow; |
| 604 | /* fallthrough */ |
| 605 | |
| 606 | success: |
| 607 | Py_DECREF(index); |
| 608 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 609 | return 1; |
| 610 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 611 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 612 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 613 | "uid is less than minimum"); |
| 614 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 615 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 616 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 617 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 618 | "uid is greater than maximum"); |
| 619 | /* fallthrough */ |
| 620 | |
| 621 | fail: |
| 622 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | int |
| 627 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 628 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 629 | gid_t gid; |
| 630 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 631 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 632 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 633 | unsigned long uresult; |
| 634 | |
| 635 | index = PyNumber_Index(obj); |
| 636 | if (index == NULL) { |
| 637 | PyErr_Format(PyExc_TypeError, |
| 638 | "gid should be integer, not %.200s", |
| 639 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 640 | return 0; |
| 641 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 642 | |
| 643 | /* |
| 644 | * Handling gid_t is complicated for two reasons: |
| 645 | * * Although gid_t is (always?) unsigned, it still |
| 646 | * accepts -1. |
| 647 | * * We don't know its size in advance--it may be |
| 648 | * bigger than an int, or it may be smaller than |
| 649 | * a long. |
| 650 | * |
| 651 | * So a bit of defensive programming is in order. |
| 652 | * Start with interpreting the value passed |
| 653 | * in as a signed long and see if it works. |
| 654 | */ |
| 655 | |
| 656 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 657 | |
| 658 | if (!overflow) { |
| 659 | gid = (gid_t)result; |
| 660 | |
| 661 | if (result == -1) { |
| 662 | if (PyErr_Occurred()) |
| 663 | goto fail; |
| 664 | /* It's a legitimate -1, we're done. */ |
| 665 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 666 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 667 | |
| 668 | /* Any other negative number is disallowed. */ |
| 669 | if (result < 0) { |
| 670 | goto underflow; |
| 671 | } |
| 672 | |
| 673 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 674 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 675 | (long)gid != result) |
| 676 | goto underflow; |
| 677 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 678 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 679 | |
| 680 | if (overflow < 0) |
| 681 | goto underflow; |
| 682 | |
| 683 | /* |
| 684 | * Okay, the value overflowed a signed long. If it |
| 685 | * fits in an *unsigned* long, it may still be okay, |
| 686 | * as gid_t may be unsigned long on this platform. |
| 687 | */ |
| 688 | uresult = PyLong_AsUnsignedLong(index); |
| 689 | if (PyErr_Occurred()) { |
| 690 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 691 | goto overflow; |
| 692 | goto fail; |
| 693 | } |
| 694 | |
| 695 | gid = (gid_t)uresult; |
| 696 | |
| 697 | /* |
| 698 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 699 | * but this value would get interpreted as (gid_t)-1 by chown |
| 700 | * and its siblings. That's not what the user meant! So we |
| 701 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 702 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 703 | */ |
| 704 | if (gid == (gid_t)-1) |
| 705 | goto overflow; |
| 706 | |
| 707 | /* Ensure the value wasn't truncated. */ |
| 708 | if (sizeof(gid_t) < sizeof(long) && |
| 709 | (unsigned long)gid != uresult) |
| 710 | goto overflow; |
| 711 | /* fallthrough */ |
| 712 | |
| 713 | success: |
| 714 | Py_DECREF(index); |
| 715 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 716 | return 1; |
| 717 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 718 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 719 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 720 | "gid is less than minimum"); |
| 721 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 722 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 723 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 724 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 725 | "gid is greater than maximum"); |
| 726 | /* fallthrough */ |
| 727 | |
| 728 | fail: |
| 729 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 730 | return 0; |
| 731 | } |
| 732 | #endif /* MS_WINDOWS */ |
| 733 | |
| 734 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 735 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 736 | |
| 737 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 738 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 739 | static int |
| 740 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 741 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 742 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 743 | if (PyErr_Occurred()) |
| 744 | return 0; |
| 745 | return 1; |
| 746 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 747 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 748 | |
| 749 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 750 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 751 | /* |
| 752 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 753 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 754 | * which doesn't play nicely with all the initializer lines in this file that |
| 755 | * look like this: |
| 756 | * int dir_fd = DEFAULT_DIR_FD; |
| 757 | */ |
| 758 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 759 | #else |
| 760 | #define DEFAULT_DIR_FD (-100) |
| 761 | #endif |
| 762 | |
| 763 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 764 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 765 | { |
| 766 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 767 | long long_value; |
| 768 | |
| 769 | PyObject *index = PyNumber_Index(o); |
| 770 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 771 | return 0; |
| 772 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 773 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 774 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 775 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 776 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 777 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 778 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 779 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 780 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 781 | return 0; |
| 782 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 783 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 784 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 785 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 786 | return 0; |
| 787 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 788 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 789 | *p = (int)long_value; |
| 790 | return 1; |
| 791 | } |
| 792 | |
| 793 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 794 | dir_fd_converter(PyObject *o, void *p) |
| 795 | { |
| 796 | if (o == Py_None) { |
| 797 | *(int *)p = DEFAULT_DIR_FD; |
| 798 | return 1; |
| 799 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 800 | else if (PyIndex_Check(o)) { |
| 801 | return _fd_converter(o, (int *)p); |
| 802 | } |
| 803 | else { |
| 804 | PyErr_Format(PyExc_TypeError, |
| 805 | "argument should be integer or None, not %.200s", |
| 806 | Py_TYPE(o)->tp_name); |
| 807 | return 0; |
| 808 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 812 | /* |
| 813 | * A PyArg_ParseTuple "converter" function |
| 814 | * that handles filesystem paths in the manner |
| 815 | * preferred by the os module. |
| 816 | * |
| 817 | * path_converter accepts (Unicode) strings and their |
| 818 | * subclasses, and bytes and their subclasses. What |
| 819 | * it does with the argument depends on the platform: |
| 820 | * |
| 821 | * * On Windows, if we get a (Unicode) string we |
| 822 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 823 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 824 | * |
| 825 | * * On all other platforms, strings are encoded |
| 826 | * to bytes using PyUnicode_FSConverter, then we |
| 827 | * extract the char * from the bytes object and |
| 828 | * return that. |
| 829 | * |
| 830 | * path_converter also optionally accepts signed |
| 831 | * integers (representing open file descriptors) instead |
| 832 | * of path strings. |
| 833 | * |
| 834 | * Input fields: |
| 835 | * path.nullable |
| 836 | * If nonzero, the path is permitted to be None. |
| 837 | * path.allow_fd |
| 838 | * If nonzero, the path is permitted to be a file handle |
| 839 | * (a signed int) instead of a string. |
| 840 | * path.function_name |
| 841 | * If non-NULL, path_converter will use that as the name |
| 842 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 843 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 844 | * path.argument_name |
| 845 | * If non-NULL, path_converter will use that as the name |
| 846 | * of the parameter in error messages. |
| 847 | * (If path.argument_name is NULL it uses "path".) |
| 848 | * |
| 849 | * Output fields: |
| 850 | * path.wide |
| 851 | * Points to the path if it was expressed as Unicode |
| 852 | * and was not encoded. (Only used on Windows.) |
| 853 | * path.narrow |
| 854 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 855 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 856 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 857 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 858 | * path.fd |
| 859 | * Contains a file descriptor if path.accept_fd was true |
| 860 | * and the caller provided a signed integer instead of any |
| 861 | * sort of string. |
| 862 | * |
| 863 | * WARNING: if your "path" parameter is optional, and is |
| 864 | * unspecified, path_converter will never get called. |
| 865 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 866 | * yourself! |
| 867 | * path.length |
| 868 | * The length of the path in characters, if specified as |
| 869 | * a string. |
| 870 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 871 | * The original object passed in (if get a PathLike object, |
| 872 | * the result of PyOS_FSPath() is treated as the original object). |
| 873 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 874 | * path.cleanup |
| 875 | * For internal use only. May point to a temporary object. |
| 876 | * (Pay no attention to the man behind the curtain.) |
| 877 | * |
| 878 | * At most one of path.wide or path.narrow will be non-NULL. |
| 879 | * If path was None and path.nullable was set, |
| 880 | * or if path was an integer and path.allow_fd was set, |
| 881 | * both path.wide and path.narrow will be NULL |
| 882 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 883 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 884 | * path_converter takes care to not write to the path_t |
| 885 | * unless it's successful. However it must reset the |
| 886 | * "cleanup" field each time it's called. |
| 887 | * |
| 888 | * Use as follows: |
| 889 | * path_t path; |
| 890 | * memset(&path, 0, sizeof(path)); |
| 891 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 892 | * // ... use values from path ... |
| 893 | * path_cleanup(&path); |
| 894 | * |
| 895 | * (Note that if PyArg_Parse fails you don't need to call |
| 896 | * path_cleanup(). However it is safe to do so.) |
| 897 | */ |
| 898 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 899 | const char *function_name; |
| 900 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 901 | int nullable; |
| 902 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 903 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 904 | #ifdef MS_WINDOWS |
| 905 | BOOL narrow; |
| 906 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 907 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 908 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 909 | int fd; |
| 910 | Py_ssize_t length; |
| 911 | PyObject *object; |
| 912 | PyObject *cleanup; |
| 913 | } path_t; |
| 914 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 915 | #ifdef MS_WINDOWS |
| 916 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 917 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 918 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 919 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 920 | {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] | 921 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 922 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 923 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 924 | path_cleanup(path_t *path) |
| 925 | { |
| 926 | Py_CLEAR(path->object); |
| 927 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 931 | path_converter(PyObject *o, void *p) |
| 932 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 933 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 934 | PyObject *bytes = NULL; |
| 935 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 936 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 937 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 938 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 939 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 940 | const wchar_t *wide; |
| 941 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 942 | |
| 943 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 944 | PyErr_Format(exc, "%s%s" fmt, \ |
| 945 | path->function_name ? path->function_name : "", \ |
| 946 | path->function_name ? ": " : "", \ |
| 947 | path->argument_name ? path->argument_name : "path") |
| 948 | |
| 949 | /* Py_CLEANUP_SUPPORTED support */ |
| 950 | if (o == NULL) { |
| 951 | path_cleanup(path); |
| 952 | return 1; |
| 953 | } |
| 954 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 955 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 956 | path->object = path->cleanup = NULL; |
| 957 | /* path->object owns a reference to the original object */ |
| 958 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 959 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 960 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 961 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 962 | #ifdef MS_WINDOWS |
| 963 | path->narrow = FALSE; |
| 964 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 965 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 966 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 967 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 968 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 971 | /* Only call this here so that we don't treat the return value of |
| 972 | os.fspath() as an fd or buffer. */ |
| 973 | is_index = path->allow_fd && PyIndex_Check(o); |
| 974 | is_buffer = PyObject_CheckBuffer(o); |
| 975 | is_bytes = PyBytes_Check(o); |
| 976 | is_unicode = PyUnicode_Check(o); |
| 977 | |
| 978 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 979 | /* Inline PyOS_FSPath() for better error messages. */ |
| 980 | _Py_IDENTIFIER(__fspath__); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 981 | PyObject *func, *res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 982 | |
| 983 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 984 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 985 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 986 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 987 | res = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 988 | Py_DECREF(func); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 989 | if (NULL == res) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 990 | goto error_exit; |
| 991 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 992 | else if (PyUnicode_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 993 | is_unicode = 1; |
| 994 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 995 | else if (PyBytes_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 996 | is_bytes = 1; |
| 997 | } |
| 998 | else { |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 999 | PyErr_Format(PyExc_TypeError, |
| 1000 | "expected %.200s.__fspath__() to return str or bytes, " |
| 1001 | "not %.200s", Py_TYPE(o)->tp_name, |
| 1002 | Py_TYPE(res)->tp_name); |
| 1003 | Py_DECREF(res); |
| 1004 | goto error_exit; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1005 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1006 | |
| 1007 | /* still owns a reference to the original object */ |
| 1008 | Py_DECREF(o); |
| 1009 | o = res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1013 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1014 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1015 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1016 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1017 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1018 | if (length > 32767) { |
| 1019 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1020 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1021 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1022 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1023 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1024 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1025 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1026 | |
| 1027 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1028 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1029 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1030 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1031 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1032 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1033 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1034 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1035 | #endif |
| 1036 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1037 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1038 | bytes = o; |
| 1039 | Py_INCREF(bytes); |
| 1040 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1041 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1042 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1043 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1044 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1045 | "%s%s%s should be %s, not %.200s", |
| 1046 | path->function_name ? path->function_name : "", |
| 1047 | path->function_name ? ": " : "", |
| 1048 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1049 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1050 | "integer or None" : |
| 1051 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1052 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1053 | "string, bytes or os.PathLike", |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1054 | Py_TYPE(o)->tp_name)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1055 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1056 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1057 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1058 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1059 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1060 | } |
| 1061 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1062 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1063 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1064 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1065 | } |
| 1066 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1067 | #ifdef MS_WINDOWS |
| 1068 | path->narrow = FALSE; |
| 1069 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1070 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1071 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1072 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1073 | } |
| 1074 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1075 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1076 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1077 | path->function_name ? path->function_name : "", |
| 1078 | path->function_name ? ": " : "", |
| 1079 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1080 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1081 | "integer or None" : |
| 1082 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1083 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1084 | "string, bytes or os.PathLike", |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1085 | Py_TYPE(o)->tp_name); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1086 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1089 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1090 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1091 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1092 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1093 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1096 | #ifdef MS_WINDOWS |
| 1097 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1098 | narrow, |
| 1099 | length |
| 1100 | ); |
| 1101 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1102 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1105 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1106 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1107 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1108 | } |
| 1109 | if (length > 32767) { |
| 1110 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1111 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1112 | } |
| 1113 | if (wcslen(wide) != length) { |
| 1114 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1115 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1116 | } |
| 1117 | path->wide = wide; |
| 1118 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1119 | path->cleanup = wo; |
| 1120 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1121 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1122 | path->wide = NULL; |
| 1123 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1124 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1125 | /* Still a reference owned by path->object, don't have to |
| 1126 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1127 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1128 | } |
| 1129 | else { |
| 1130 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1131 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1132 | #endif |
| 1133 | path->fd = -1; |
| 1134 | |
| 1135 | success_exit: |
| 1136 | path->length = length; |
| 1137 | path->object = o; |
| 1138 | return Py_CLEANUP_SUPPORTED; |
| 1139 | |
| 1140 | error_exit: |
| 1141 | Py_XDECREF(o); |
| 1142 | Py_XDECREF(bytes); |
| 1143 | #ifdef MS_WINDOWS |
| 1144 | Py_XDECREF(wo); |
| 1145 | #endif |
| 1146 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1150 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1151 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1152 | PyErr_Format(PyExc_NotImplementedError, |
| 1153 | "%s%s%s unavailable on this platform", |
| 1154 | (function_name != NULL) ? function_name : "", |
| 1155 | (function_name != NULL) ? ": ": "", |
| 1156 | argument_name); |
| 1157 | } |
| 1158 | |
| 1159 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1160 | dir_fd_unavailable(PyObject *o, void *p) |
| 1161 | { |
| 1162 | int dir_fd; |
| 1163 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1164 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1165 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1166 | argument_unavailable_error(NULL, "dir_fd"); |
| 1167 | return 0; |
| 1168 | } |
| 1169 | *(int *)p = dir_fd; |
| 1170 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1174 | fd_specified(const char *function_name, int fd) |
| 1175 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1176 | if (fd == -1) |
| 1177 | return 0; |
| 1178 | |
| 1179 | argument_unavailable_error(function_name, "fd"); |
| 1180 | return 1; |
| 1181 | } |
| 1182 | |
| 1183 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1184 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1185 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1186 | if (follow_symlinks) |
| 1187 | return 0; |
| 1188 | |
| 1189 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1190 | return 1; |
| 1191 | } |
| 1192 | |
| 1193 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1194 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1195 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1196 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1197 | #ifndef MS_WINDOWS |
| 1198 | && !path->narrow |
| 1199 | #endif |
| 1200 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1201 | PyErr_Format(PyExc_ValueError, |
| 1202 | "%s: can't specify dir_fd without matching path", |
| 1203 | function_name); |
| 1204 | return 1; |
| 1205 | } |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1210 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1211 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1212 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1213 | PyErr_Format(PyExc_ValueError, |
| 1214 | "%s: can't specify both dir_fd and fd", |
| 1215 | function_name); |
| 1216 | return 1; |
| 1217 | } |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1222 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1223 | int follow_symlinks) |
| 1224 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1225 | if ((fd > 0) && (!follow_symlinks)) { |
| 1226 | PyErr_Format(PyExc_ValueError, |
| 1227 | "%s: cannot use fd and follow_symlinks together", |
| 1228 | function_name); |
| 1229 | return 1; |
| 1230 | } |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1235 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1236 | int follow_symlinks) |
| 1237 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1238 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1239 | PyErr_Format(PyExc_ValueError, |
| 1240 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1241 | function_name); |
| 1242 | return 1; |
| 1243 | } |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1247 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1248 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1249 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1250 | typedef off_t Py_off_t; |
| 1251 | #endif |
| 1252 | |
| 1253 | static int |
| 1254 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1255 | { |
| 1256 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1257 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1258 | #else |
| 1259 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1260 | #endif |
| 1261 | if (PyErr_Occurred()) |
| 1262 | return 0; |
| 1263 | return 1; |
| 1264 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1265 | |
| 1266 | static PyObject * |
| 1267 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1268 | { |
| 1269 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1270 | return PyLong_FromLongLong(offset); |
| 1271 | #else |
| 1272 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1273 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1274 | } |
| 1275 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1276 | #ifdef HAVE_SIGSET_T |
| 1277 | /* Convert an iterable of integers to a sigset. |
| 1278 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1279 | int |
| 1280 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1281 | { |
| 1282 | sigset_t *mask = (sigset_t *)addr; |
| 1283 | PyObject *iterator, *item; |
| 1284 | long signum; |
| 1285 | int overflow; |
| 1286 | |
Rémi Lapeyre | f090019 | 2019-05-04 01:30:53 +0200 | [diff] [blame] | 1287 | // The extra parens suppress the unreachable-code warning with clang on MacOS |
| 1288 | if (sigemptyset(mask) < (0)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1289 | /* Probably only if mask == NULL. */ |
| 1290 | PyErr_SetFromErrno(PyExc_OSError); |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | iterator = PyObject_GetIter(obj); |
| 1295 | if (iterator == NULL) { |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
| 1299 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1300 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1301 | Py_DECREF(item); |
| 1302 | if (signum <= 0 || signum >= NSIG) { |
| 1303 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1304 | PyErr_Format(PyExc_ValueError, |
| 1305 | "signal number %ld out of range", signum); |
| 1306 | } |
| 1307 | goto error; |
| 1308 | } |
| 1309 | if (sigaddset(mask, (int)signum)) { |
| 1310 | if (errno != EINVAL) { |
| 1311 | /* Probably impossible */ |
| 1312 | PyErr_SetFromErrno(PyExc_OSError); |
| 1313 | goto error; |
| 1314 | } |
| 1315 | /* For backwards compatibility, allow idioms such as |
| 1316 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1317 | */ |
| 1318 | const char msg[] = |
| 1319 | "invalid signal number %ld, please use valid_signals()"; |
| 1320 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1321 | goto error; |
| 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | if (!PyErr_Occurred()) { |
| 1326 | Py_DECREF(iterator); |
| 1327 | return 1; |
| 1328 | } |
| 1329 | |
| 1330 | error: |
| 1331 | Py_DECREF(iterator); |
| 1332 | return 0; |
| 1333 | } |
| 1334 | #endif /* HAVE_SIGSET_T */ |
| 1335 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1336 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1337 | |
| 1338 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1339 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1340 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1341 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1342 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1343 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1344 | |
| 1345 | if (0 == DeviceIoControl( |
| 1346 | reparse_point_handle, |
| 1347 | FSCTL_GET_REPARSE_POINT, |
| 1348 | NULL, 0, /* in buffer */ |
| 1349 | target_buffer, sizeof(target_buffer), |
| 1350 | &n_bytes_returned, |
| 1351 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1352 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1353 | |
| 1354 | if (reparse_tag) |
| 1355 | *reparse_tag = rdb->ReparseTag; |
| 1356 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1357 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1358 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1359 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1360 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1361 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1362 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1363 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1364 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1365 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1366 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1367 | */ |
| 1368 | #include <crt_externs.h> |
| 1369 | static char **environ; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 1370 | #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1371 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1372 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1373 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1374 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1375 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1376 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1377 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1378 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1379 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1380 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1381 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1382 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1383 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1384 | d = PyDict_New(); |
| 1385 | if (d == NULL) |
| 1386 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1387 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1388 | if (environ == NULL) |
| 1389 | environ = *_NSGetEnviron(); |
| 1390 | #endif |
| 1391 | #ifdef MS_WINDOWS |
| 1392 | /* _wenviron must be initialized in this way if the program is started |
| 1393 | through main() instead of wmain(). */ |
| 1394 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1395 | e = _wenviron; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1396 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1397 | e = environ; |
| 1398 | #endif |
| 1399 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1400 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1401 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1402 | PyObject *k; |
| 1403 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1404 | #ifdef MS_WINDOWS |
| 1405 | const wchar_t *p = wcschr(*e, L'='); |
| 1406 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1407 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1408 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1409 | if (p == NULL) |
| 1410 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1411 | #ifdef MS_WINDOWS |
| 1412 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1413 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1414 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1415 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1416 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1417 | Py_DECREF(d); |
| 1418 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1419 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1420 | #ifdef MS_WINDOWS |
| 1421 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1422 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1423 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1424 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1425 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1426 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1427 | Py_DECREF(d); |
| 1428 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1429 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1430 | if (PyDict_GetItemWithError(d, k) == NULL) { |
| 1431 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { |
| 1432 | Py_DECREF(v); |
| 1433 | Py_DECREF(k); |
| 1434 | Py_DECREF(d); |
| 1435 | return NULL; |
| 1436 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1437 | } |
| 1438 | Py_DECREF(k); |
| 1439 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1440 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1441 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1444 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1445 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1446 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1447 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1448 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1449 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1450 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1451 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1452 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1453 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1454 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1455 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1456 | /* XXX We should pass the function name along in the future. |
| 1457 | (winreg.c also wants to pass the function name.) |
| 1458 | This would however require an additional param to the |
| 1459 | Windows error object, which is non-trivial. |
| 1460 | */ |
| 1461 | errno = GetLastError(); |
| 1462 | if (filename) |
| 1463 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1464 | else |
| 1465 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1466 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1467 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1468 | static PyObject * |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1469 | win32_error_object_err(const char* function, PyObject* filename, DWORD err) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1470 | { |
| 1471 | /* XXX - see win32_error for comments on 'function' */ |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1472 | if (filename) |
| 1473 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1474 | PyExc_OSError, |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1475 | err, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1476 | filename); |
| 1477 | else |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1478 | return PyErr_SetFromWindowsErr(err); |
| 1479 | } |
| 1480 | |
| 1481 | static PyObject * |
| 1482 | win32_error_object(const char* function, PyObject* filename) |
| 1483 | { |
| 1484 | errno = GetLastError(); |
| 1485 | return win32_error_object_err(function, filename, errno); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1486 | } |
| 1487 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1488 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1489 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1490 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1491 | posix_path_object_error(PyObject *path) |
| 1492 | { |
| 1493 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1494 | } |
| 1495 | |
| 1496 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1497 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1498 | { |
| 1499 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1500 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1501 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1502 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1503 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1504 | #endif |
| 1505 | } |
| 1506 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1507 | static PyObject * |
| 1508 | path_object_error2(PyObject *path, PyObject *path2) |
| 1509 | { |
| 1510 | #ifdef MS_WINDOWS |
| 1511 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1512 | PyExc_OSError, 0, path, path2); |
| 1513 | #else |
| 1514 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1515 | #endif |
| 1516 | } |
| 1517 | |
| 1518 | static PyObject * |
| 1519 | path_error(path_t *path) |
| 1520 | { |
| 1521 | return path_object_error(path->object); |
| 1522 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1523 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1524 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1525 | posix_path_error(path_t *path) |
| 1526 | { |
| 1527 | return posix_path_object_error(path->object); |
| 1528 | } |
| 1529 | |
| 1530 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1531 | path_error2(path_t *path, path_t *path2) |
| 1532 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1533 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1537 | /* POSIX generic methods */ |
| 1538 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1539 | static int |
| 1540 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1541 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1542 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1543 | int *pointer = (int *)p; |
| 1544 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1545 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1546 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1547 | *pointer = fd; |
| 1548 | return 1; |
| 1549 | } |
| 1550 | |
| 1551 | static PyObject * |
| 1552 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1553 | { |
| 1554 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1555 | int async_err = 0; |
| 1556 | |
| 1557 | do { |
| 1558 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1559 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1560 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1561 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1562 | Py_END_ALLOW_THREADS |
| 1563 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1564 | if (res != 0) |
| 1565 | return (!async_err) ? posix_error() : NULL; |
| 1566 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1567 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1568 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1569 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1570 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1571 | /* This is a reimplementation of the C library's chdir function, |
| 1572 | but one that produces Win32 errors instead of DOS error codes. |
| 1573 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1574 | it also needs to set "magic" environment variables indicating |
| 1575 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1576 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1577 | win32_wchdir(LPCWSTR path) |
| 1578 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1579 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1580 | int result; |
| 1581 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1582 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1583 | if(!SetCurrentDirectoryW(path)) |
| 1584 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1585 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1586 | if (!result) |
| 1587 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1588 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1589 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1590 | if (!new_path) { |
| 1591 | SetLastError(ERROR_OUTOFMEMORY); |
| 1592 | return FALSE; |
| 1593 | } |
| 1594 | result = GetCurrentDirectoryW(result, new_path); |
| 1595 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1596 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1597 | return FALSE; |
| 1598 | } |
| 1599 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1600 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1601 | wcsncmp(new_path, L"//", 2) == 0); |
| 1602 | if (!is_unc_like_path) { |
| 1603 | env[1] = new_path[0]; |
| 1604 | result = SetEnvironmentVariableW(env, new_path); |
| 1605 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1606 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1607 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1608 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1609 | } |
| 1610 | #endif |
| 1611 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1612 | #ifdef MS_WINDOWS |
| 1613 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1614 | - time stamps are restricted to second resolution |
| 1615 | - file modification times suffer from forth-and-back conversions between |
| 1616 | UTC and local time |
| 1617 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1618 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1619 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1620 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1621 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1622 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1623 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1624 | BY_HANDLE_FILE_INFORMATION *info, |
| 1625 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1626 | { |
| 1627 | memset(info, 0, sizeof(*info)); |
| 1628 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1629 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1630 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1631 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1632 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1633 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1634 | /* info->nNumberOfLinks = 1; */ |
| 1635 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1636 | *reparse_tag = pFileData->dwReserved0; |
| 1637 | else |
| 1638 | *reparse_tag = 0; |
| 1639 | } |
| 1640 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1641 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1642 | 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] | 1643 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1644 | HANDLE hFindFile; |
| 1645 | WIN32_FIND_DATAW FileData; |
| 1646 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1647 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1648 | return FALSE; |
| 1649 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1650 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1651 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1654 | static BOOL |
| 1655 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1656 | { |
| 1657 | int buf_size, result_length; |
| 1658 | wchar_t *buf; |
| 1659 | |
| 1660 | /* We have a good handle to the target, use it to determine |
| 1661 | the target path name (then we'll call lstat on it). */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1662 | buf_size = GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1663 | VOLUME_NAME_DOS); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1664 | if(!buf_size) |
| 1665 | return FALSE; |
| 1666 | |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1667 | buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1668 | if (!buf) { |
| 1669 | SetLastError(ERROR_OUTOFMEMORY); |
| 1670 | return FALSE; |
| 1671 | } |
| 1672 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1673 | result_length = GetFinalPathNameByHandleW(hdl, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1674 | buf, buf_size, VOLUME_NAME_DOS); |
| 1675 | |
| 1676 | if(!result_length) { |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1677 | PyMem_RawFree(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1678 | return FALSE; |
| 1679 | } |
| 1680 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1681 | buf[result_length] = 0; |
| 1682 | |
| 1683 | *target_path = buf; |
| 1684 | return TRUE; |
| 1685 | } |
| 1686 | |
| 1687 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1688 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1689 | BOOL traverse) |
| 1690 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1691 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1692 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1693 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1694 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1695 | wchar_t *target_path; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1696 | const wchar_t *dot; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1697 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1698 | hFile = CreateFileW( |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1699 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1700 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1701 | 0, /* share mode */ |
| 1702 | NULL, /* security attributes */ |
| 1703 | OPEN_EXISTING, |
| 1704 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1705 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1706 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1707 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1708 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1709 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1710 | NULL); |
| 1711 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1712 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1713 | /* Either the target doesn't exist, or we don't have access to |
| 1714 | get a handle to it. If the former, we need to return an error. |
| 1715 | If the latter, we can use attributes_from_dir. */ |
Berker Peksag | 0b4dc48 | 2016-09-17 15:49:59 +0300 | [diff] [blame] | 1716 | DWORD lastError = GetLastError(); |
| 1717 | if (lastError != ERROR_ACCESS_DENIED && |
| 1718 | lastError != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1719 | return -1; |
| 1720 | /* Could not get attributes on open file. Fall back to |
| 1721 | reading the directory. */ |
| 1722 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1723 | /* Very strange. This should not fail now */ |
| 1724 | return -1; |
| 1725 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1726 | if (traverse) { |
| 1727 | /* Should traverse, but could not open reparse point handle */ |
Berker Peksag | 0b4dc48 | 2016-09-17 15:49:59 +0300 | [diff] [blame] | 1728 | SetLastError(lastError); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1729 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1730 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1731 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1732 | } else { |
| 1733 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1734 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1735 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1736 | } |
| 1737 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1738 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) { |
| 1739 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1740 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1741 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1742 | /* Close the outer open file handle now that we're about to |
| 1743 | reopen it with different flags. */ |
| 1744 | if (!CloseHandle(hFile)) |
| 1745 | return -1; |
| 1746 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1747 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1748 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1749 | the file without the reparse handling flag set. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1750 | hFile2 = CreateFileW( |
| 1751 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1752 | NULL, OPEN_EXISTING, |
| 1753 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1754 | NULL); |
| 1755 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1756 | return -1; |
| 1757 | |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1758 | if (!get_target_path(hFile2, &target_path)) { |
| 1759 | CloseHandle(hFile2); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1760 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | if (!CloseHandle(hFile2)) { |
| 1764 | return -1; |
| 1765 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1766 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1767 | code = win32_xstat_impl(target_path, result, FALSE); |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1768 | PyMem_RawFree(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1769 | return code; |
| 1770 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1771 | } else |
| 1772 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1773 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1774 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1775 | |
| 1776 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1777 | dot = wcsrchr(path, '.'); |
| 1778 | if (dot) { |
| 1779 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1780 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1781 | result->st_mode |= 0111; |
| 1782 | } |
| 1783 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1787 | 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] | 1788 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1789 | /* Protocol violation: we explicitly clear errno, instead of |
| 1790 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1791 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1792 | errno = 0; |
| 1793 | return code; |
| 1794 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1795 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1796 | |
| 1797 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1798 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1799 | default does not traverse symlinks and instead returns attributes for |
| 1800 | the symlink. |
| 1801 | |
| 1802 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1803 | win32_stat will first explicitly resolve the symlink target and then will |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1804 | call win32_lstat on that result. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1805 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1806 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1807 | 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] | 1808 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1809 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1812 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1813 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1814 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1815 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1818 | #endif /* MS_WINDOWS */ |
| 1819 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1820 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1821 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1822 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1823 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1824 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1825 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1826 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1827 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1828 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1829 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1830 | |
| 1831 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1832 | {"st_mode", "protection bits"}, |
| 1833 | {"st_ino", "inode"}, |
| 1834 | {"st_dev", "device"}, |
| 1835 | {"st_nlink", "number of hard links"}, |
| 1836 | {"st_uid", "user ID of owner"}, |
| 1837 | {"st_gid", "group ID of owner"}, |
| 1838 | {"st_size", "total size, in bytes"}, |
| 1839 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1840 | {NULL, "integer time of last access"}, |
| 1841 | {NULL, "integer time of last modification"}, |
| 1842 | {NULL, "integer time of last change"}, |
| 1843 | {"st_atime", "time of last access"}, |
| 1844 | {"st_mtime", "time of last modification"}, |
| 1845 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1846 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1847 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1848 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1849 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1850 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1851 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1852 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1853 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1854 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1855 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1856 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1857 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1858 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1859 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1860 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1861 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1862 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1863 | #endif |
| 1864 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1865 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1866 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1867 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1868 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1869 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1870 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1871 | {"st_fstype", "Type of filesystem"}, |
| 1872 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1873 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1874 | }; |
| 1875 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1876 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1877 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1878 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1879 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1880 | #endif |
| 1881 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1882 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1883 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1884 | #else |
| 1885 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1886 | #endif |
| 1887 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1888 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1889 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1890 | #else |
| 1891 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1892 | #endif |
| 1893 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1894 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1895 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1896 | #else |
| 1897 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1898 | #endif |
| 1899 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1900 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1901 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1902 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1903 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1904 | #endif |
| 1905 | |
| 1906 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1907 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1908 | #else |
| 1909 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1910 | #endif |
| 1911 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1912 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1913 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1914 | #else |
| 1915 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 1916 | #endif |
| 1917 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1918 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1919 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 1920 | #else |
| 1921 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 1922 | #endif |
| 1923 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1924 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1925 | "stat_result", /* name */ |
| 1926 | stat_result__doc__, /* doc */ |
| 1927 | stat_result_fields, |
| 1928 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1929 | }; |
| 1930 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1931 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1932 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1933 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1934 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1935 | 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] | 1936 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1937 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1938 | |
| 1939 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1940 | {"f_bsize", }, |
| 1941 | {"f_frsize", }, |
| 1942 | {"f_blocks", }, |
| 1943 | {"f_bfree", }, |
| 1944 | {"f_bavail", }, |
| 1945 | {"f_files", }, |
| 1946 | {"f_ffree", }, |
| 1947 | {"f_favail", }, |
| 1948 | {"f_flag", }, |
| 1949 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 1950 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1951 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1952 | }; |
| 1953 | |
| 1954 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1955 | "statvfs_result", /* name */ |
| 1956 | statvfs_result__doc__, /* doc */ |
| 1957 | statvfs_result_fields, |
| 1958 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1959 | }; |
| 1960 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1961 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1962 | PyDoc_STRVAR(waitid_result__doc__, |
| 1963 | "waitid_result: Result from waitid.\n\n\ |
| 1964 | This object may be accessed either as a tuple of\n\ |
| 1965 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1966 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1967 | \n\ |
| 1968 | See os.waitid for more information."); |
| 1969 | |
| 1970 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1971 | {"si_pid", }, |
| 1972 | {"si_uid", }, |
| 1973 | {"si_signo", }, |
| 1974 | {"si_status", }, |
| 1975 | {"si_code", }, |
| 1976 | {0} |
| 1977 | }; |
| 1978 | |
| 1979 | static PyStructSequence_Desc waitid_result_desc = { |
| 1980 | "waitid_result", /* name */ |
| 1981 | waitid_result__doc__, /* doc */ |
| 1982 | waitid_result_fields, |
| 1983 | 5 |
| 1984 | }; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 1985 | static PyTypeObject* WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1986 | #endif |
| 1987 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1988 | static int initialized; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 1989 | static PyTypeObject* StatResultType; |
| 1990 | static PyTypeObject* StatVFSResultType; |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 1991 | #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] | 1992 | static PyTypeObject* SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1993 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1994 | static newfunc structseq_new; |
| 1995 | |
| 1996 | static PyObject * |
| 1997 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1998 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1999 | PyStructSequence *result; |
| 2000 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2001 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2002 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2003 | if (!result) |
| 2004 | return NULL; |
| 2005 | /* If we have been initialized from a tuple, |
| 2006 | st_?time might be set to None. Initialize it |
| 2007 | from the int slots. */ |
| 2008 | for (i = 7; i <= 9; i++) { |
| 2009 | if (result->ob_item[i+3] == Py_None) { |
| 2010 | Py_DECREF(Py_None); |
| 2011 | Py_INCREF(result->ob_item[i]); |
| 2012 | result->ob_item[i+3] = result->ob_item[i]; |
| 2013 | } |
| 2014 | } |
| 2015 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2019 | static PyObject *billion = NULL; |
| 2020 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2021 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2022 | 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] | 2023 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2024 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2025 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2026 | PyObject *s_in_ns = NULL; |
| 2027 | PyObject *ns_total = NULL; |
| 2028 | PyObject *float_s = NULL; |
| 2029 | |
| 2030 | if (!(s && ns_fractional)) |
| 2031 | goto exit; |
| 2032 | |
| 2033 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2034 | if (!s_in_ns) |
| 2035 | goto exit; |
| 2036 | |
| 2037 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2038 | if (!ns_total) |
| 2039 | goto exit; |
| 2040 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2041 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2042 | if (!float_s) { |
| 2043 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | PyStructSequence_SET_ITEM(v, index, s); |
| 2047 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2048 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2049 | s = NULL; |
| 2050 | float_s = NULL; |
| 2051 | ns_total = NULL; |
| 2052 | exit: |
| 2053 | Py_XDECREF(s); |
| 2054 | Py_XDECREF(ns_fractional); |
| 2055 | Py_XDECREF(s_in_ns); |
| 2056 | Py_XDECREF(ns_total); |
| 2057 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2060 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2061 | (used by posix_stat() and posix_fstat()) */ |
| 2062 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2063 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2064 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2065 | unsigned long ansec, mnsec, cnsec; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2066 | PyObject *v = PyStructSequence_New(StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2067 | if (v == NULL) |
| 2068 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2069 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2070 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2071 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2072 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2073 | #ifdef MS_WINDOWS |
| 2074 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2075 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2076 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2077 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2078 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2079 | #if defined(MS_WINDOWS) |
| 2080 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2081 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2082 | #else |
| 2083 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2084 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2085 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2086 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2087 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2088 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2089 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2090 | ansec = st->st_atim.tv_nsec; |
| 2091 | mnsec = st->st_mtim.tv_nsec; |
| 2092 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2093 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2094 | ansec = st->st_atimespec.tv_nsec; |
| 2095 | mnsec = st->st_mtimespec.tv_nsec; |
| 2096 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2097 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2098 | ansec = st->st_atime_nsec; |
| 2099 | mnsec = st->st_mtime_nsec; |
| 2100 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2101 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2102 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2103 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2104 | fill_time(v, 7, st->st_atime, ansec); |
| 2105 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2106 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2107 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2108 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2109 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2110 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2111 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2112 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2113 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2114 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2115 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2116 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2117 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2118 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2119 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2120 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2121 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2122 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2123 | #endif |
| 2124 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2125 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2126 | PyObject *val; |
| 2127 | unsigned long bsec,bnsec; |
| 2128 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2129 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2130 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2131 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2132 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2133 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2134 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2135 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2136 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2137 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2138 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2139 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2140 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2141 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2142 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2143 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2144 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2145 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2146 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2147 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2148 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2149 | PyUnicode_FromString(st->st_fstype)); |
| 2150 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2151 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2152 | if (PyErr_Occurred()) { |
| 2153 | Py_DECREF(v); |
| 2154 | return NULL; |
| 2155 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2156 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2157 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2160 | /* POSIX methods */ |
| 2161 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2162 | |
| 2163 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2164 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2165 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2166 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2167 | STRUCT_STAT st; |
| 2168 | int result; |
| 2169 | |
| 2170 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2171 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2172 | return NULL; |
| 2173 | #endif |
| 2174 | |
| 2175 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2176 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2177 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2178 | return NULL; |
| 2179 | |
| 2180 | Py_BEGIN_ALLOW_THREADS |
| 2181 | if (path->fd != -1) |
| 2182 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2183 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2184 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2185 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2186 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2187 | result = win32_lstat(path->wide, &st); |
| 2188 | #else |
| 2189 | else |
| 2190 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2191 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2192 | result = LSTAT(path->narrow, &st); |
| 2193 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2194 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2195 | #ifdef HAVE_FSTATAT |
| 2196 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2197 | result = fstatat(dir_fd, path->narrow, &st, |
| 2198 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2199 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2200 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2201 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2202 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2203 | Py_END_ALLOW_THREADS |
| 2204 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2205 | if (result != 0) { |
| 2206 | return path_error(path); |
| 2207 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2208 | |
| 2209 | return _pystat_fromstructstat(&st); |
| 2210 | } |
| 2211 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2212 | /*[python input] |
| 2213 | |
| 2214 | for s in """ |
| 2215 | |
| 2216 | FACCESSAT |
| 2217 | FCHMODAT |
| 2218 | FCHOWNAT |
| 2219 | FSTATAT |
| 2220 | LINKAT |
| 2221 | MKDIRAT |
| 2222 | MKFIFOAT |
| 2223 | MKNODAT |
| 2224 | OPENAT |
| 2225 | READLINKAT |
| 2226 | SYMLINKAT |
| 2227 | UNLINKAT |
| 2228 | |
| 2229 | """.strip().split(): |
| 2230 | s = s.strip() |
| 2231 | print(""" |
| 2232 | #ifdef HAVE_{s} |
| 2233 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2234 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2235 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2236 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2237 | """.rstrip().format(s=s)) |
| 2238 | |
| 2239 | for s in """ |
| 2240 | |
| 2241 | FCHDIR |
| 2242 | FCHMOD |
| 2243 | FCHOWN |
| 2244 | FDOPENDIR |
| 2245 | FEXECVE |
| 2246 | FPATHCONF |
| 2247 | FSTATVFS |
| 2248 | FTRUNCATE |
| 2249 | |
| 2250 | """.strip().split(): |
| 2251 | s = s.strip() |
| 2252 | print(""" |
| 2253 | #ifdef HAVE_{s} |
| 2254 | #define PATH_HAVE_{s} 1 |
| 2255 | #else |
| 2256 | #define PATH_HAVE_{s} 0 |
| 2257 | #endif |
| 2258 | |
| 2259 | """.rstrip().format(s=s)) |
| 2260 | [python start generated code]*/ |
| 2261 | |
| 2262 | #ifdef HAVE_FACCESSAT |
| 2263 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2264 | #else |
| 2265 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2266 | #endif |
| 2267 | |
| 2268 | #ifdef HAVE_FCHMODAT |
| 2269 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2270 | #else |
| 2271 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2272 | #endif |
| 2273 | |
| 2274 | #ifdef HAVE_FCHOWNAT |
| 2275 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2276 | #else |
| 2277 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2278 | #endif |
| 2279 | |
| 2280 | #ifdef HAVE_FSTATAT |
| 2281 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2282 | #else |
| 2283 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2284 | #endif |
| 2285 | |
| 2286 | #ifdef HAVE_LINKAT |
| 2287 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2288 | #else |
| 2289 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2290 | #endif |
| 2291 | |
| 2292 | #ifdef HAVE_MKDIRAT |
| 2293 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2294 | #else |
| 2295 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2296 | #endif |
| 2297 | |
| 2298 | #ifdef HAVE_MKFIFOAT |
| 2299 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2300 | #else |
| 2301 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2302 | #endif |
| 2303 | |
| 2304 | #ifdef HAVE_MKNODAT |
| 2305 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2306 | #else |
| 2307 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2308 | #endif |
| 2309 | |
| 2310 | #ifdef HAVE_OPENAT |
| 2311 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2312 | #else |
| 2313 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2314 | #endif |
| 2315 | |
| 2316 | #ifdef HAVE_READLINKAT |
| 2317 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2318 | #else |
| 2319 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2320 | #endif |
| 2321 | |
| 2322 | #ifdef HAVE_SYMLINKAT |
| 2323 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2324 | #else |
| 2325 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2326 | #endif |
| 2327 | |
| 2328 | #ifdef HAVE_UNLINKAT |
| 2329 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2330 | #else |
| 2331 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2332 | #endif |
| 2333 | |
| 2334 | #ifdef HAVE_FCHDIR |
| 2335 | #define PATH_HAVE_FCHDIR 1 |
| 2336 | #else |
| 2337 | #define PATH_HAVE_FCHDIR 0 |
| 2338 | #endif |
| 2339 | |
| 2340 | #ifdef HAVE_FCHMOD |
| 2341 | #define PATH_HAVE_FCHMOD 1 |
| 2342 | #else |
| 2343 | #define PATH_HAVE_FCHMOD 0 |
| 2344 | #endif |
| 2345 | |
| 2346 | #ifdef HAVE_FCHOWN |
| 2347 | #define PATH_HAVE_FCHOWN 1 |
| 2348 | #else |
| 2349 | #define PATH_HAVE_FCHOWN 0 |
| 2350 | #endif |
| 2351 | |
| 2352 | #ifdef HAVE_FDOPENDIR |
| 2353 | #define PATH_HAVE_FDOPENDIR 1 |
| 2354 | #else |
| 2355 | #define PATH_HAVE_FDOPENDIR 0 |
| 2356 | #endif |
| 2357 | |
| 2358 | #ifdef HAVE_FEXECVE |
| 2359 | #define PATH_HAVE_FEXECVE 1 |
| 2360 | #else |
| 2361 | #define PATH_HAVE_FEXECVE 0 |
| 2362 | #endif |
| 2363 | |
| 2364 | #ifdef HAVE_FPATHCONF |
| 2365 | #define PATH_HAVE_FPATHCONF 1 |
| 2366 | #else |
| 2367 | #define PATH_HAVE_FPATHCONF 0 |
| 2368 | #endif |
| 2369 | |
| 2370 | #ifdef HAVE_FSTATVFS |
| 2371 | #define PATH_HAVE_FSTATVFS 1 |
| 2372 | #else |
| 2373 | #define PATH_HAVE_FSTATVFS 0 |
| 2374 | #endif |
| 2375 | |
| 2376 | #ifdef HAVE_FTRUNCATE |
| 2377 | #define PATH_HAVE_FTRUNCATE 1 |
| 2378 | #else |
| 2379 | #define PATH_HAVE_FTRUNCATE 0 |
| 2380 | #endif |
| 2381 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2382 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2383 | #ifdef MS_WINDOWS |
| 2384 | #undef PATH_HAVE_FTRUNCATE |
| 2385 | #define PATH_HAVE_FTRUNCATE 1 |
| 2386 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2387 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2388 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2389 | |
| 2390 | class path_t_converter(CConverter): |
| 2391 | |
| 2392 | type = "path_t" |
| 2393 | impl_by_reference = True |
| 2394 | parse_by_reference = True |
| 2395 | |
| 2396 | converter = 'path_converter' |
| 2397 | |
| 2398 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2399 | # right now path_t doesn't support default values. |
| 2400 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2401 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2402 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2403 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2404 | if self.c_default not in (None, 'Py_None'): |
| 2405 | 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] | 2406 | |
| 2407 | self.nullable = nullable |
| 2408 | self.allow_fd = allow_fd |
| 2409 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2410 | def pre_render(self): |
| 2411 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2412 | if isinstance(value, str): |
| 2413 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2414 | return str(int(bool(value))) |
| 2415 | |
| 2416 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2417 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2418 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2419 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2420 | strify(self.nullable), |
| 2421 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2422 | ) |
| 2423 | |
| 2424 | def cleanup(self): |
| 2425 | return "path_cleanup(&" + self.name + ");\n" |
| 2426 | |
| 2427 | |
| 2428 | class dir_fd_converter(CConverter): |
| 2429 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2430 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2431 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2432 | if self.default in (unspecified, None): |
| 2433 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2434 | if isinstance(requires, str): |
| 2435 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2436 | else: |
| 2437 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2438 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2439 | class fildes_converter(CConverter): |
| 2440 | type = 'int' |
| 2441 | converter = 'fildes_converter' |
| 2442 | |
| 2443 | class uid_t_converter(CConverter): |
| 2444 | type = "uid_t" |
| 2445 | converter = '_Py_Uid_Converter' |
| 2446 | |
| 2447 | class gid_t_converter(CConverter): |
| 2448 | type = "gid_t" |
| 2449 | converter = '_Py_Gid_Converter' |
| 2450 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2451 | class dev_t_converter(CConverter): |
| 2452 | type = 'dev_t' |
| 2453 | converter = '_Py_Dev_Converter' |
| 2454 | |
| 2455 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2456 | type = 'dev_t' |
| 2457 | conversion_fn = '_PyLong_FromDev' |
| 2458 | unsigned_cast = '(dev_t)' |
| 2459 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2460 | class FSConverter_converter(CConverter): |
| 2461 | type = 'PyObject *' |
| 2462 | converter = 'PyUnicode_FSConverter' |
| 2463 | def converter_init(self): |
| 2464 | if self.default is not unspecified: |
| 2465 | fail("FSConverter_converter does not support default values") |
| 2466 | self.c_default = 'NULL' |
| 2467 | |
| 2468 | def cleanup(self): |
| 2469 | return "Py_XDECREF(" + self.name + ");\n" |
| 2470 | |
| 2471 | class pid_t_converter(CConverter): |
| 2472 | type = 'pid_t' |
| 2473 | format_unit = '" _Py_PARSE_PID "' |
| 2474 | |
| 2475 | class idtype_t_converter(int_converter): |
| 2476 | type = 'idtype_t' |
| 2477 | |
| 2478 | class id_t_converter(CConverter): |
| 2479 | type = 'id_t' |
| 2480 | format_unit = '" _Py_PARSE_PID "' |
| 2481 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2482 | class intptr_t_converter(CConverter): |
| 2483 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2484 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2485 | |
| 2486 | class Py_off_t_converter(CConverter): |
| 2487 | type = 'Py_off_t' |
| 2488 | converter = 'Py_off_t_converter' |
| 2489 | |
| 2490 | class Py_off_t_return_converter(long_return_converter): |
| 2491 | type = 'Py_off_t' |
| 2492 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2493 | |
| 2494 | class path_confname_converter(CConverter): |
| 2495 | type="int" |
| 2496 | converter="conv_path_confname" |
| 2497 | |
| 2498 | class confstr_confname_converter(path_confname_converter): |
| 2499 | converter='conv_confstr_confname' |
| 2500 | |
| 2501 | class sysconf_confname_converter(path_confname_converter): |
| 2502 | converter="conv_sysconf_confname" |
| 2503 | |
| 2504 | class sched_param_converter(CConverter): |
| 2505 | type = 'struct sched_param' |
| 2506 | converter = 'convert_sched_param' |
| 2507 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2508 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2509 | [python start generated code]*/ |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 2510 | /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2511 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2512 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2513 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2514 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2515 | |
| 2516 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2517 | 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] | 2518 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2519 | |
| 2520 | * |
| 2521 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2522 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2523 | If not None, it should be a file descriptor open to a directory, |
| 2524 | and path should be a relative string; path will then be relative to |
| 2525 | that directory. |
| 2526 | |
| 2527 | follow_symlinks: bool = True |
| 2528 | If False, and the last element of the path is a symbolic link, |
| 2529 | stat will examine the symbolic link itself instead of the file |
| 2530 | the link points to. |
| 2531 | |
| 2532 | Perform a stat system call on the given path. |
| 2533 | |
| 2534 | dir_fd and follow_symlinks may not be implemented |
| 2535 | on your platform. If they are unavailable, using them will raise a |
| 2536 | NotImplementedError. |
| 2537 | |
| 2538 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2539 | an open file descriptor. |
| 2540 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2541 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2542 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2543 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2544 | 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] | 2545 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2546 | { |
| 2547 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2548 | } |
| 2549 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2550 | |
| 2551 | /*[clinic input] |
| 2552 | os.lstat |
| 2553 | |
| 2554 | path : path_t |
| 2555 | |
| 2556 | * |
| 2557 | |
| 2558 | dir_fd : dir_fd(requires='fstatat') = None |
| 2559 | |
| 2560 | Perform a stat system call on the given path, without following symbolic links. |
| 2561 | |
| 2562 | Like stat(), but do not follow symbolic links. |
| 2563 | Equivalent to stat(path, follow_symlinks=False). |
| 2564 | [clinic start generated code]*/ |
| 2565 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2566 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2567 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2568 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2569 | { |
| 2570 | int follow_symlinks = 0; |
| 2571 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2572 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2573 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2574 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2575 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2576 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2577 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2578 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2579 | 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] | 2580 | |
| 2581 | mode: int |
| 2582 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2583 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2584 | |
| 2585 | * |
| 2586 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2587 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2588 | If not None, it should be a file descriptor open to a directory, |
| 2589 | and path should be relative; path will then be relative to that |
| 2590 | directory. |
| 2591 | |
| 2592 | effective_ids: bool = False |
| 2593 | If True, access will use the effective uid/gid instead of |
| 2594 | the real uid/gid. |
| 2595 | |
| 2596 | follow_symlinks: bool = True |
| 2597 | If False, and the last element of the path is a symbolic link, |
| 2598 | access will examine the symbolic link itself instead of the file |
| 2599 | the link points to. |
| 2600 | |
| 2601 | Use the real uid/gid to test for access to a path. |
| 2602 | |
| 2603 | {parameters} |
| 2604 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2605 | on your platform. If they are unavailable, using them will raise a |
| 2606 | NotImplementedError. |
| 2607 | |
| 2608 | Note that most operations will use the effective uid/gid, therefore this |
| 2609 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2610 | has the specified access to the path. |
| 2611 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2612 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2613 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2614 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2615 | 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] | 2616 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2617 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2618 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2619 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2620 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2621 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2622 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2623 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2624 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2625 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2626 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2627 | #ifndef HAVE_FACCESSAT |
| 2628 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2629 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2630 | |
| 2631 | if (effective_ids) { |
| 2632 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2633 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2634 | } |
| 2635 | #endif |
| 2636 | |
| 2637 | #ifdef MS_WINDOWS |
| 2638 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2639 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2640 | Py_END_ALLOW_THREADS |
| 2641 | |
| 2642 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2643 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2644 | * * we didn't get a -1, and |
| 2645 | * * write access wasn't requested, |
| 2646 | * * or the file isn't read-only, |
| 2647 | * * or it's a directory. |
| 2648 | * (Directories cannot be read-only on Windows.) |
| 2649 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2650 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2651 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2652 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2653 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2654 | #else |
| 2655 | |
| 2656 | Py_BEGIN_ALLOW_THREADS |
| 2657 | #ifdef HAVE_FACCESSAT |
| 2658 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2659 | effective_ids || |
| 2660 | !follow_symlinks) { |
| 2661 | int flags = 0; |
| 2662 | if (!follow_symlinks) |
| 2663 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2664 | if (effective_ids) |
| 2665 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2666 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2667 | } |
| 2668 | else |
| 2669 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2670 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2671 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2672 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2673 | #endif |
| 2674 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2675 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2678 | #ifndef F_OK |
| 2679 | #define F_OK 0 |
| 2680 | #endif |
| 2681 | #ifndef R_OK |
| 2682 | #define R_OK 4 |
| 2683 | #endif |
| 2684 | #ifndef W_OK |
| 2685 | #define W_OK 2 |
| 2686 | #endif |
| 2687 | #ifndef X_OK |
| 2688 | #define X_OK 1 |
| 2689 | #endif |
| 2690 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2691 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2692 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2693 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2694 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2695 | |
| 2696 | fd: int |
| 2697 | Integer file descriptor handle. |
| 2698 | |
| 2699 | / |
| 2700 | |
| 2701 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2702 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2703 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2704 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2705 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2706 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2707 | { |
| 2708 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2709 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2710 | ret = ttyname(fd); |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2711 | if (ret == NULL) { |
| 2712 | return posix_error(); |
| 2713 | } |
| 2714 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2715 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2716 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2717 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2718 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2719 | /*[clinic input] |
| 2720 | os.ctermid |
| 2721 | |
| 2722 | Return the name of the controlling terminal for this process. |
| 2723 | [clinic start generated code]*/ |
| 2724 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2725 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2726 | os_ctermid_impl(PyObject *module) |
| 2727 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2728 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2729 | char *ret; |
| 2730 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2731 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2732 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2733 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2734 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2735 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2736 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2737 | if (ret == NULL) |
| 2738 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2739 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2740 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2741 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2742 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2743 | |
| 2744 | /*[clinic input] |
| 2745 | os.chdir |
| 2746 | |
| 2747 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2748 | |
| 2749 | Change the current working directory to the specified path. |
| 2750 | |
| 2751 | path may always be specified as a string. |
| 2752 | On some platforms, path may also be specified as an open file descriptor. |
| 2753 | If this functionality is unavailable, using it raises an exception. |
| 2754 | [clinic start generated code]*/ |
| 2755 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2756 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2757 | os_chdir_impl(PyObject *module, path_t *path) |
| 2758 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2759 | { |
| 2760 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2761 | |
| 2762 | Py_BEGIN_ALLOW_THREADS |
| 2763 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2764 | /* on unix, success = 0, on windows, success = !0 */ |
| 2765 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2766 | #else |
| 2767 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2768 | if (path->fd != -1) |
| 2769 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2770 | else |
| 2771 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2772 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2773 | #endif |
| 2774 | Py_END_ALLOW_THREADS |
| 2775 | |
| 2776 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2777 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2778 | } |
| 2779 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2780 | Py_RETURN_NONE; |
| 2781 | } |
| 2782 | |
| 2783 | |
| 2784 | #ifdef HAVE_FCHDIR |
| 2785 | /*[clinic input] |
| 2786 | os.fchdir |
| 2787 | |
| 2788 | fd: fildes |
| 2789 | |
| 2790 | Change to the directory of the given file descriptor. |
| 2791 | |
| 2792 | fd must be opened on a directory, not a file. |
| 2793 | Equivalent to os.chdir(fd). |
| 2794 | |
| 2795 | [clinic start generated code]*/ |
| 2796 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2797 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2798 | os_fchdir_impl(PyObject *module, int fd) |
| 2799 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2800 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2801 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2802 | } |
| 2803 | #endif /* HAVE_FCHDIR */ |
| 2804 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2805 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2806 | /*[clinic input] |
| 2807 | os.chmod |
| 2808 | |
| 2809 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2810 | 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] | 2811 | On some platforms, path may also be specified as an open file descriptor. |
| 2812 | If this functionality is unavailable, using it raises an exception. |
| 2813 | |
| 2814 | mode: int |
| 2815 | Operating-system mode bitfield. |
| 2816 | |
| 2817 | * |
| 2818 | |
| 2819 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2820 | If not None, it should be a file descriptor open to a directory, |
| 2821 | and path should be relative; path will then be relative to that |
| 2822 | directory. |
| 2823 | |
| 2824 | follow_symlinks: bool = True |
| 2825 | If False, and the last element of the path is a symbolic link, |
| 2826 | chmod will modify the symbolic link itself instead of the file |
| 2827 | the link points to. |
| 2828 | |
| 2829 | Change the access permissions of a file. |
| 2830 | |
| 2831 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2832 | an open file descriptor. |
| 2833 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2834 | If they are unavailable, using them will raise a NotImplementedError. |
| 2835 | |
| 2836 | [clinic start generated code]*/ |
| 2837 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2838 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2839 | 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] | 2840 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2841 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2842 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2843 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2844 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2845 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2846 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2847 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2848 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2849 | #ifdef HAVE_FCHMODAT |
| 2850 | int fchmodat_nofollow_unsupported = 0; |
| 2851 | #endif |
| 2852 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2853 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2854 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2855 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2856 | #endif |
| 2857 | |
| 2858 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2859 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2860 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2861 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2862 | result = 0; |
| 2863 | else { |
| 2864 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2865 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2866 | else |
| 2867 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2868 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2869 | } |
| 2870 | Py_END_ALLOW_THREADS |
| 2871 | |
| 2872 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2873 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2874 | } |
| 2875 | #else /* MS_WINDOWS */ |
| 2876 | Py_BEGIN_ALLOW_THREADS |
| 2877 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2878 | if (path->fd != -1) |
| 2879 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2880 | else |
| 2881 | #endif |
| 2882 | #ifdef HAVE_LCHMOD |
| 2883 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2884 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2885 | else |
| 2886 | #endif |
| 2887 | #ifdef HAVE_FCHMODAT |
| 2888 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2889 | /* |
| 2890 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2891 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2892 | * and then says it isn't implemented yet. |
| 2893 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2894 | * |
| 2895 | * Once it is supported, os.chmod will automatically |
| 2896 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2897 | * Until then, we need to be careful what exception we raise. |
| 2898 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2899 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2900 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2901 | /* |
| 2902 | * But wait! We can't throw the exception without allowing threads, |
| 2903 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2904 | */ |
| 2905 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2906 | result && |
| 2907 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2908 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2909 | } |
| 2910 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2911 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2912 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2913 | Py_END_ALLOW_THREADS |
| 2914 | |
| 2915 | if (result) { |
| 2916 | #ifdef HAVE_FCHMODAT |
| 2917 | if (fchmodat_nofollow_unsupported) { |
| 2918 | if (dir_fd != DEFAULT_DIR_FD) |
| 2919 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2920 | dir_fd, follow_symlinks); |
| 2921 | else |
| 2922 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 2923 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2924 | } |
| 2925 | else |
| 2926 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2927 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2928 | } |
| 2929 | #endif |
| 2930 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2931 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2934 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2935 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2936 | /*[clinic input] |
| 2937 | os.fchmod |
| 2938 | |
| 2939 | fd: int |
| 2940 | mode: int |
| 2941 | |
| 2942 | Change the access permissions of the file given by file descriptor fd. |
| 2943 | |
| 2944 | Equivalent to os.chmod(fd, mode). |
| 2945 | [clinic start generated code]*/ |
| 2946 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2947 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2948 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 2949 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2950 | { |
| 2951 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2952 | int async_err = 0; |
| 2953 | |
| 2954 | do { |
| 2955 | Py_BEGIN_ALLOW_THREADS |
| 2956 | res = fchmod(fd, mode); |
| 2957 | Py_END_ALLOW_THREADS |
| 2958 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 2959 | if (res != 0) |
| 2960 | return (!async_err) ? posix_error() : NULL; |
| 2961 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2962 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2963 | } |
| 2964 | #endif /* HAVE_FCHMOD */ |
| 2965 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2966 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2967 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2968 | /*[clinic input] |
| 2969 | os.lchmod |
| 2970 | |
| 2971 | path: path_t |
| 2972 | mode: int |
| 2973 | |
| 2974 | Change the access permissions of a file, without following symbolic links. |
| 2975 | |
| 2976 | If path is a symlink, this affects the link itself rather than the target. |
| 2977 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 2978 | [clinic start generated code]*/ |
| 2979 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2980 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2981 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 2982 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2983 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2984 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2985 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2986 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2987 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2988 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2989 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2990 | return NULL; |
| 2991 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2992 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2993 | } |
| 2994 | #endif /* HAVE_LCHMOD */ |
| 2995 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2996 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2997 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2998 | /*[clinic input] |
| 2999 | os.chflags |
| 3000 | |
| 3001 | path: path_t |
| 3002 | flags: unsigned_long(bitwise=True) |
| 3003 | follow_symlinks: bool=True |
| 3004 | |
| 3005 | Set file flags. |
| 3006 | |
| 3007 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3008 | link, chflags will change flags on the symbolic link itself instead of the |
| 3009 | file the link points to. |
| 3010 | follow_symlinks may not be implemented on your platform. If it is |
| 3011 | unavailable, using it will raise a NotImplementedError. |
| 3012 | |
| 3013 | [clinic start generated code]*/ |
| 3014 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3015 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3016 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3017 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3018 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3019 | { |
| 3020 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3021 | |
| 3022 | #ifndef HAVE_LCHFLAGS |
| 3023 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3024 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3025 | #endif |
| 3026 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3027 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3028 | #ifdef HAVE_LCHFLAGS |
| 3029 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3030 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3031 | else |
| 3032 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3033 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3034 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3035 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3036 | if (result) |
| 3037 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3038 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3039 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3040 | } |
| 3041 | #endif /* HAVE_CHFLAGS */ |
| 3042 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3043 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3044 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3045 | /*[clinic input] |
| 3046 | os.lchflags |
| 3047 | |
| 3048 | path: path_t |
| 3049 | flags: unsigned_long(bitwise=True) |
| 3050 | |
| 3051 | Set file flags. |
| 3052 | |
| 3053 | This function will not follow symbolic links. |
| 3054 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3055 | [clinic start generated code]*/ |
| 3056 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3057 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3058 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3059 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3060 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3061 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3062 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3063 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3064 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3065 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3066 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3067 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3068 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3069 | } |
| 3070 | #endif /* HAVE_LCHFLAGS */ |
| 3071 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3072 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3073 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3074 | /*[clinic input] |
| 3075 | os.chroot |
| 3076 | path: path_t |
| 3077 | |
| 3078 | Change root directory to path. |
| 3079 | |
| 3080 | [clinic start generated code]*/ |
| 3081 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3082 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3083 | os_chroot_impl(PyObject *module, path_t *path) |
| 3084 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3085 | { |
| 3086 | int res; |
| 3087 | Py_BEGIN_ALLOW_THREADS |
| 3088 | res = chroot(path->narrow); |
| 3089 | Py_END_ALLOW_THREADS |
| 3090 | if (res < 0) |
| 3091 | return path_error(path); |
| 3092 | Py_RETURN_NONE; |
| 3093 | } |
| 3094 | #endif /* HAVE_CHROOT */ |
| 3095 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3096 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3097 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3098 | /*[clinic input] |
| 3099 | os.fsync |
| 3100 | |
| 3101 | fd: fildes |
| 3102 | |
| 3103 | Force write of fd to disk. |
| 3104 | [clinic start generated code]*/ |
| 3105 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3106 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3107 | os_fsync_impl(PyObject *module, int fd) |
| 3108 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3109 | { |
| 3110 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3111 | } |
| 3112 | #endif /* HAVE_FSYNC */ |
| 3113 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3114 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3115 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3116 | /*[clinic input] |
| 3117 | os.sync |
| 3118 | |
| 3119 | Force write of everything to disk. |
| 3120 | [clinic start generated code]*/ |
| 3121 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3122 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3123 | os_sync_impl(PyObject *module) |
| 3124 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3125 | { |
| 3126 | Py_BEGIN_ALLOW_THREADS |
| 3127 | sync(); |
| 3128 | Py_END_ALLOW_THREADS |
| 3129 | Py_RETURN_NONE; |
| 3130 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3131 | #endif /* HAVE_SYNC */ |
| 3132 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3133 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3134 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3135 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3136 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3137 | #endif |
| 3138 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3139 | /*[clinic input] |
| 3140 | os.fdatasync |
| 3141 | |
| 3142 | fd: fildes |
| 3143 | |
| 3144 | Force write of fd to disk without forcing update of metadata. |
| 3145 | [clinic start generated code]*/ |
| 3146 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3147 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3148 | os_fdatasync_impl(PyObject *module, int fd) |
| 3149 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3150 | { |
| 3151 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3152 | } |
| 3153 | #endif /* HAVE_FDATASYNC */ |
| 3154 | |
| 3155 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3156 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3157 | /*[clinic input] |
| 3158 | os.chown |
| 3159 | |
| 3160 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3161 | 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] | 3162 | |
| 3163 | uid: uid_t |
| 3164 | |
| 3165 | gid: gid_t |
| 3166 | |
| 3167 | * |
| 3168 | |
| 3169 | dir_fd : dir_fd(requires='fchownat') = None |
| 3170 | If not None, it should be a file descriptor open to a directory, |
| 3171 | and path should be relative; path will then be relative to that |
| 3172 | directory. |
| 3173 | |
| 3174 | follow_symlinks: bool = True |
| 3175 | If False, and the last element of the path is a symbolic link, |
| 3176 | stat will examine the symbolic link itself instead of the file |
| 3177 | the link points to. |
| 3178 | |
| 3179 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3180 | |
| 3181 | path may always be specified as a string. |
| 3182 | On some platforms, path may also be specified as an open file descriptor. |
| 3183 | If this functionality is unavailable, using it raises an exception. |
| 3184 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3185 | and path should be relative; path will then be relative to that directory. |
| 3186 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3187 | link, chown will modify the symbolic link itself instead of the file the |
| 3188 | link points to. |
| 3189 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3190 | an open file descriptor. |
| 3191 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3192 | If they are unavailable, using them will raise a NotImplementedError. |
| 3193 | |
| 3194 | [clinic start generated code]*/ |
| 3195 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3196 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3197 | 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] | 3198 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3199 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3200 | { |
| 3201 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3202 | |
| 3203 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3204 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3205 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3206 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3207 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3208 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3209 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3210 | |
| 3211 | #ifdef __APPLE__ |
| 3212 | /* |
| 3213 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3214 | * (But we still have an lchown symbol because of weak-linking.) |
| 3215 | * It doesn't have fchownat either. So there's no possibility |
| 3216 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3217 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3218 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3219 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3220 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3221 | } |
| 3222 | #endif |
| 3223 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3224 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3225 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3226 | if (path->fd != -1) |
| 3227 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3228 | else |
| 3229 | #endif |
| 3230 | #ifdef HAVE_LCHOWN |
| 3231 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3232 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3233 | else |
| 3234 | #endif |
| 3235 | #ifdef HAVE_FCHOWNAT |
| 3236 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3237 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3238 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3239 | else |
| 3240 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3241 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3242 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3243 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3244 | if (result) |
| 3245 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3246 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3247 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3248 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3249 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3250 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3251 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3252 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3253 | /*[clinic input] |
| 3254 | os.fchown |
| 3255 | |
| 3256 | fd: int |
| 3257 | uid: uid_t |
| 3258 | gid: gid_t |
| 3259 | |
| 3260 | Change the owner and group id of the file specified by file descriptor. |
| 3261 | |
| 3262 | Equivalent to os.chown(fd, uid, gid). |
| 3263 | |
| 3264 | [clinic start generated code]*/ |
| 3265 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3266 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3267 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3268 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3269 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3270 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3271 | int async_err = 0; |
| 3272 | |
| 3273 | do { |
| 3274 | Py_BEGIN_ALLOW_THREADS |
| 3275 | res = fchown(fd, uid, gid); |
| 3276 | Py_END_ALLOW_THREADS |
| 3277 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3278 | if (res != 0) |
| 3279 | return (!async_err) ? posix_error() : NULL; |
| 3280 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3281 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3282 | } |
| 3283 | #endif /* HAVE_FCHOWN */ |
| 3284 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3285 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3286 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3287 | /*[clinic input] |
| 3288 | os.lchown |
| 3289 | |
| 3290 | path : path_t |
| 3291 | uid: uid_t |
| 3292 | gid: gid_t |
| 3293 | |
| 3294 | Change the owner and group id of path to the numeric uid and gid. |
| 3295 | |
| 3296 | This function will not follow symbolic links. |
| 3297 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3298 | [clinic start generated code]*/ |
| 3299 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3300 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3301 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3302 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3303 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3304 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3305 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3306 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3307 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3308 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3309 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3310 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3311 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3312 | } |
| 3313 | #endif /* HAVE_LCHOWN */ |
| 3314 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3315 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3316 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3317 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3318 | { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3319 | char *buf, *tmpbuf; |
| 3320 | char *cwd; |
| 3321 | const size_t chunk = 1024; |
| 3322 | size_t buflen = 0; |
| 3323 | PyObject *obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3324 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3325 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3326 | if (!use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3327 | wchar_t wbuf[MAXPATHLEN]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3328 | wchar_t *wbuf2 = wbuf; |
| 3329 | PyObject *resobj; |
| 3330 | DWORD len; |
| 3331 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3332 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3333 | /* If the buffer is large enough, len does not include the |
| 3334 | terminating \0. If the buffer is too small, len includes |
| 3335 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3336 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3337 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3338 | if (wbuf2) |
| 3339 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3340 | } |
| 3341 | Py_END_ALLOW_THREADS |
| 3342 | if (!wbuf2) { |
| 3343 | PyErr_NoMemory(); |
| 3344 | return NULL; |
| 3345 | } |
| 3346 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3347 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3348 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3349 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3350 | } |
| 3351 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3352 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3353 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3354 | return resobj; |
| 3355 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3356 | |
| 3357 | if (win32_warn_bytes_api()) |
| 3358 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3359 | #endif |
| 3360 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3361 | buf = cwd = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3362 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3363 | do { |
| 3364 | buflen += chunk; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3365 | #ifdef MS_WINDOWS |
| 3366 | if (buflen > INT_MAX) { |
| 3367 | PyErr_NoMemory(); |
| 3368 | break; |
| 3369 | } |
| 3370 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3371 | tmpbuf = PyMem_RawRealloc(buf, buflen); |
| 3372 | if (tmpbuf == NULL) |
| 3373 | break; |
| 3374 | |
| 3375 | buf = tmpbuf; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3376 | #ifdef MS_WINDOWS |
| 3377 | cwd = getcwd(buf, (int)buflen); |
| 3378 | #else |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3379 | cwd = getcwd(buf, buflen); |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3380 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3381 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3382 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3383 | |
| 3384 | if (cwd == NULL) { |
| 3385 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3386 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3387 | } |
| 3388 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3389 | if (use_bytes) |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3390 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 3391 | else |
| 3392 | obj = PyUnicode_DecodeFSDefault(buf); |
| 3393 | PyMem_RawFree(buf); |
| 3394 | |
| 3395 | return obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3396 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3397 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3398 | |
| 3399 | /*[clinic input] |
| 3400 | os.getcwd |
| 3401 | |
| 3402 | Return a unicode string representing the current working directory. |
| 3403 | [clinic start generated code]*/ |
| 3404 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3405 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3406 | os_getcwd_impl(PyObject *module) |
| 3407 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3408 | { |
| 3409 | return posix_getcwd(0); |
| 3410 | } |
| 3411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3412 | |
| 3413 | /*[clinic input] |
| 3414 | os.getcwdb |
| 3415 | |
| 3416 | Return a bytes string representing the current working directory. |
| 3417 | [clinic start generated code]*/ |
| 3418 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3419 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3420 | os_getcwdb_impl(PyObject *module) |
| 3421 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3422 | { |
| 3423 | return posix_getcwd(1); |
| 3424 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3425 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3426 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3427 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3428 | #define HAVE_LINK 1 |
| 3429 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3430 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3431 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3432 | /*[clinic input] |
| 3433 | |
| 3434 | os.link |
| 3435 | |
| 3436 | src : path_t |
| 3437 | dst : path_t |
| 3438 | * |
| 3439 | src_dir_fd : dir_fd = None |
| 3440 | dst_dir_fd : dir_fd = None |
| 3441 | follow_symlinks: bool = True |
| 3442 | |
| 3443 | Create a hard link to a file. |
| 3444 | |
| 3445 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3446 | descriptor open to a directory, and the respective path string (src or dst) |
| 3447 | should be relative; the path will then be relative to that directory. |
| 3448 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3449 | link, link will create a link to the symbolic link itself instead of the |
| 3450 | file the link points to. |
| 3451 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3452 | platform. If they are unavailable, using them will raise a |
| 3453 | NotImplementedError. |
| 3454 | [clinic start generated code]*/ |
| 3455 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3456 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3457 | 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] | 3458 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3459 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3460 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3461 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3462 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3463 | #else |
| 3464 | int result; |
| 3465 | #endif |
| 3466 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3467 | #ifndef HAVE_LINKAT |
| 3468 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3469 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3470 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3471 | } |
| 3472 | #endif |
| 3473 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3474 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3475 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3476 | PyErr_SetString(PyExc_NotImplementedError, |
| 3477 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3478 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3479 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3480 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3481 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3482 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3483 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3484 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3485 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3486 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3487 | if (!result) |
| 3488 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3489 | #else |
| 3490 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3491 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3492 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3493 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3494 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3495 | result = linkat(src_dir_fd, src->narrow, |
| 3496 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3497 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3498 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3499 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3500 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3501 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3502 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3503 | if (result) |
| 3504 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3505 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3506 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3507 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3508 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3509 | #endif |
| 3510 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3511 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3512 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3513 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3514 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3515 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3516 | PyObject *v; |
| 3517 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3518 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3519 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3520 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3521 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3522 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3523 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3524 | WIN32_FIND_DATAW wFileData; |
| 3525 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3526 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3527 | if (!path->wide) { /* Default arg: "." */ |
| 3528 | po_wchars = L"."; |
| 3529 | len = 1; |
| 3530 | } else { |
| 3531 | po_wchars = path->wide; |
| 3532 | len = wcslen(path->wide); |
| 3533 | } |
| 3534 | /* The +5 is so we can append "\\*.*\0" */ |
| 3535 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3536 | if (!wnamebuf) { |
| 3537 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3538 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3539 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3540 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3541 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3542 | wchar_t wch = wnamebuf[len-1]; |
| 3543 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3544 | wnamebuf[len++] = SEP; |
| 3545 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3546 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3547 | if ((list = PyList_New(0)) == NULL) { |
| 3548 | goto exit; |
| 3549 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3550 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3551 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3552 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3553 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3554 | int error = GetLastError(); |
| 3555 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3556 | goto exit; |
| 3557 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3558 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3559 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3560 | } |
| 3561 | do { |
| 3562 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3563 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3564 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3565 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3566 | wcslen(wFileData.cFileName)); |
| 3567 | if (path->narrow && v) { |
| 3568 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3569 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3570 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3571 | Py_DECREF(list); |
| 3572 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3573 | break; |
| 3574 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3575 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3576 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3577 | Py_DECREF(list); |
| 3578 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3579 | break; |
| 3580 | } |
| 3581 | Py_DECREF(v); |
| 3582 | } |
| 3583 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3584 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3585 | Py_END_ALLOW_THREADS |
| 3586 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3587 | it got to the end of the directory. */ |
| 3588 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3589 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3590 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3591 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3592 | } |
| 3593 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3594 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3595 | exit: |
| 3596 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3597 | if (FindClose(hFindFile) == FALSE) { |
| 3598 | if (list != NULL) { |
| 3599 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3600 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3601 | } |
| 3602 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3603 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3604 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3605 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3606 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3607 | } /* end of _listdir_windows_no_opendir */ |
| 3608 | |
| 3609 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3610 | |
| 3611 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3612 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3613 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3614 | PyObject *v; |
| 3615 | DIR *dirp = NULL; |
| 3616 | struct dirent *ep; |
| 3617 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3618 | #ifdef HAVE_FDOPENDIR |
| 3619 | int fd = -1; |
| 3620 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3621 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3622 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3623 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3624 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3625 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3626 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3627 | if (fd == -1) |
| 3628 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3629 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3630 | return_str = 1; |
| 3631 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3632 | Py_BEGIN_ALLOW_THREADS |
| 3633 | dirp = fdopendir(fd); |
| 3634 | Py_END_ALLOW_THREADS |
| 3635 | } |
| 3636 | else |
| 3637 | #endif |
| 3638 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3639 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3640 | if (path->narrow) { |
| 3641 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3642 | /* only return bytes if they specified a bytes-like object */ |
| 3643 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3644 | } |
| 3645 | else { |
| 3646 | name = "."; |
| 3647 | return_str = 1; |
| 3648 | } |
| 3649 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3650 | Py_BEGIN_ALLOW_THREADS |
| 3651 | dirp = opendir(name); |
| 3652 | Py_END_ALLOW_THREADS |
| 3653 | } |
| 3654 | |
| 3655 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3656 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3657 | #ifdef HAVE_FDOPENDIR |
| 3658 | if (fd != -1) { |
| 3659 | Py_BEGIN_ALLOW_THREADS |
| 3660 | close(fd); |
| 3661 | Py_END_ALLOW_THREADS |
| 3662 | } |
| 3663 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3664 | goto exit; |
| 3665 | } |
| 3666 | if ((list = PyList_New(0)) == NULL) { |
| 3667 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3668 | } |
| 3669 | for (;;) { |
| 3670 | errno = 0; |
| 3671 | Py_BEGIN_ALLOW_THREADS |
| 3672 | ep = readdir(dirp); |
| 3673 | Py_END_ALLOW_THREADS |
| 3674 | if (ep == NULL) { |
| 3675 | if (errno == 0) { |
| 3676 | break; |
| 3677 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3678 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3679 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3680 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3681 | } |
| 3682 | } |
| 3683 | if (ep->d_name[0] == '.' && |
| 3684 | (NAMLEN(ep) == 1 || |
| 3685 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3686 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3687 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3688 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3689 | else |
| 3690 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3691 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3692 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3693 | break; |
| 3694 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3695 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3696 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3697 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3698 | break; |
| 3699 | } |
| 3700 | Py_DECREF(v); |
| 3701 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3702 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3703 | exit: |
| 3704 | if (dirp != NULL) { |
| 3705 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3706 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3707 | if (fd > -1) |
| 3708 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3709 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3710 | closedir(dirp); |
| 3711 | Py_END_ALLOW_THREADS |
| 3712 | } |
| 3713 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3714 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3715 | } /* end of _posix_listdir */ |
| 3716 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3717 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3718 | |
| 3719 | /*[clinic input] |
| 3720 | os.listdir |
| 3721 | |
| 3722 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3723 | |
| 3724 | Return a list containing the names of the files in the directory. |
| 3725 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3726 | 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] | 3727 | the filenames returned will also be bytes; in all other circumstances |
| 3728 | the filenames returned will be str. |
| 3729 | If path is None, uses the path='.'. |
| 3730 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3731 | the file descriptor must refer to a directory. |
| 3732 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3733 | |
| 3734 | The list is in arbitrary order. It does not include the special |
| 3735 | entries '.' and '..' even if they are present in the directory. |
| 3736 | |
| 3737 | |
| 3738 | [clinic start generated code]*/ |
| 3739 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3740 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3741 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3742 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3743 | { |
| 3744 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3745 | return _listdir_windows_no_opendir(path, NULL); |
| 3746 | #else |
| 3747 | return _posix_listdir(path, NULL); |
| 3748 | #endif |
| 3749 | } |
| 3750 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3751 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3752 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3753 | /*[clinic input] |
| 3754 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3755 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3756 | path: path_t |
| 3757 | / |
| 3758 | |
| 3759 | [clinic start generated code]*/ |
| 3760 | |
| 3761 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3762 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 3763 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3764 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3765 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
| 3766 | wchar_t *wtemp; |
| 3767 | DWORD result; |
| 3768 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3769 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3770 | result = GetFullPathNameW(path->wide, |
| 3771 | Py_ARRAY_LENGTH(woutbuf), |
| 3772 | woutbuf, &wtemp); |
| 3773 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
| 3774 | woutbufp = PyMem_New(wchar_t, result); |
| 3775 | if (!woutbufp) |
| 3776 | return PyErr_NoMemory(); |
| 3777 | result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3778 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3779 | if (result) { |
| 3780 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
| 3781 | if (path->narrow) |
| 3782 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3783 | } else |
| 3784 | v = win32_error_object("GetFullPathNameW", path->object); |
| 3785 | if (woutbufp != woutbuf) |
| 3786 | PyMem_Free(woutbufp); |
| 3787 | return v; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3788 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3789 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3790 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3791 | /*[clinic input] |
| 3792 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3793 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3794 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3795 | / |
| 3796 | |
| 3797 | A helper function for samepath on windows. |
| 3798 | [clinic start generated code]*/ |
| 3799 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3800 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3801 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 3802 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3803 | { |
| 3804 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3805 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 3806 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3807 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3808 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3809 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3810 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3811 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3812 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3813 | 0, /* desired access */ |
| 3814 | 0, /* share mode */ |
| 3815 | NULL, /* security attributes */ |
| 3816 | OPEN_EXISTING, |
| 3817 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3818 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3819 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3820 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3821 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3822 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3823 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3824 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3825 | |
| 3826 | /* We have a good handle to the target, use it to determine the |
| 3827 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3828 | while (1) { |
| 3829 | Py_BEGIN_ALLOW_THREADS |
| 3830 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 3831 | buf_size, VOLUME_NAME_DOS); |
| 3832 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3833 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3834 | if (!result_length) { |
| 3835 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 3836 | path->object); |
| 3837 | goto cleanup; |
| 3838 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3839 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3840 | if (result_length < buf_size) { |
| 3841 | break; |
| 3842 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3843 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3844 | wchar_t *tmp; |
| 3845 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 3846 | result_length * sizeof(*tmp)); |
| 3847 | if (!tmp) { |
| 3848 | result = PyErr_NoMemory(); |
| 3849 | goto cleanup; |
| 3850 | } |
| 3851 | |
| 3852 | buf_size = result_length; |
| 3853 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3854 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3855 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3856 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3857 | if (path->narrow) |
| 3858 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3859 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3860 | cleanup: |
| 3861 | if (target_path != buf) { |
| 3862 | PyMem_Free(target_path); |
| 3863 | } |
| 3864 | CloseHandle(hFile); |
| 3865 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3866 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3867 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3868 | /*[clinic input] |
| 3869 | os._isdir |
| 3870 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3871 | path as arg: object |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3872 | / |
| 3873 | |
Serhiy Storchaka | 579f038 | 2016-11-08 20:21:22 +0200 | [diff] [blame] | 3874 | Return true if the pathname refers to an existing directory. |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3875 | [clinic start generated code]*/ |
| 3876 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3877 | static PyObject * |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3878 | os__isdir(PyObject *module, PyObject *arg) |
| 3879 | /*[clinic end generated code: output=404f334d85d4bf25 input=36cb6785874d479e]*/ |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3880 | { |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3881 | DWORD attributes; |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3882 | path_t path = PATH_T_INITIALIZE("_isdir", "path", 0, 0); |
| 3883 | |
| 3884 | if (!path_converter(arg, &path)) { |
| 3885 | if (PyErr_ExceptionMatches(PyExc_ValueError)) { |
| 3886 | PyErr_Clear(); |
| 3887 | Py_RETURN_FALSE; |
| 3888 | } |
| 3889 | return NULL; |
| 3890 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3891 | |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3892 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3893 | attributes = GetFileAttributesW(path.wide); |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3894 | Py_END_ALLOW_THREADS |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3895 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3896 | path_cleanup(&path); |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3897 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3898 | Py_RETURN_FALSE; |
| 3899 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3900 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3901 | Py_RETURN_TRUE; |
| 3902 | else |
| 3903 | Py_RETURN_FALSE; |
| 3904 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3905 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3906 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3907 | /*[clinic input] |
| 3908 | os._getvolumepathname |
| 3909 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3910 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3911 | |
| 3912 | A helper function for ismount on Win32. |
| 3913 | [clinic start generated code]*/ |
| 3914 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3915 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3916 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 3917 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3918 | { |
| 3919 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3920 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3921 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3922 | BOOL ret; |
| 3923 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3924 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3925 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3926 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 3927 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3928 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 3929 | return NULL; |
| 3930 | } |
| 3931 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3932 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3933 | if (mountpath == NULL) |
| 3934 | return PyErr_NoMemory(); |
| 3935 | |
| 3936 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3937 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3938 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3939 | Py_END_ALLOW_THREADS |
| 3940 | |
| 3941 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3942 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3943 | goto exit; |
| 3944 | } |
| 3945 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3946 | if (path->narrow) |
| 3947 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3948 | |
| 3949 | exit: |
| 3950 | PyMem_Free(mountpath); |
| 3951 | return result; |
| 3952 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3953 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3954 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3955 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3956 | |
| 3957 | /*[clinic input] |
| 3958 | os.mkdir |
| 3959 | |
| 3960 | path : path_t |
| 3961 | |
| 3962 | mode: int = 0o777 |
| 3963 | |
| 3964 | * |
| 3965 | |
| 3966 | dir_fd : dir_fd(requires='mkdirat') = None |
| 3967 | |
| 3968 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3969 | |
| 3970 | Create a directory. |
| 3971 | |
| 3972 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3973 | and path should be relative; path will then be relative to that directory. |
| 3974 | dir_fd may not be implemented on your platform. |
| 3975 | If it is unavailable, using it will raise a NotImplementedError. |
| 3976 | |
| 3977 | The mode argument is ignored on Windows. |
| 3978 | [clinic start generated code]*/ |
| 3979 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3980 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3981 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 3982 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3983 | { |
| 3984 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3985 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3986 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3987 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3988 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3989 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3990 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3991 | if (!result) |
| 3992 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3993 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3994 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3995 | #if HAVE_MKDIRAT |
| 3996 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3997 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3998 | else |
| 3999 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 4000 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4001 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4002 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4003 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4004 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4005 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4006 | if (result < 0) |
| 4007 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4008 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4009 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4012 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4013 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4014 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4015 | #include <sys/resource.h> |
| 4016 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4017 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4018 | |
| 4019 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4020 | /*[clinic input] |
| 4021 | os.nice |
| 4022 | |
| 4023 | increment: int |
| 4024 | / |
| 4025 | |
| 4026 | Add increment to the priority of process and return the new priority. |
| 4027 | [clinic start generated code]*/ |
| 4028 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4029 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4030 | os_nice_impl(PyObject *module, int increment) |
| 4031 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4032 | { |
| 4033 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4034 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4035 | /* There are two flavours of 'nice': one that returns the new |
| 4036 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 4037 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4038 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4039 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4040 | If we are of the nice family that returns the new priority, we |
| 4041 | need to clear errno before the call, and check if errno is filled |
| 4042 | before calling posix_error() on a returnvalue of -1, because the |
| 4043 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4044 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4045 | errno = 0; |
| 4046 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4047 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4048 | if (value == 0) |
| 4049 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4050 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4051 | if (value == -1 && errno != 0) |
| 4052 | /* either nice() or getpriority() returned an error */ |
| 4053 | return posix_error(); |
| 4054 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4055 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4056 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4057 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4058 | |
| 4059 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4060 | /*[clinic input] |
| 4061 | os.getpriority |
| 4062 | |
| 4063 | which: int |
| 4064 | who: int |
| 4065 | |
| 4066 | Return program scheduling priority. |
| 4067 | [clinic start generated code]*/ |
| 4068 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4069 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4070 | os_getpriority_impl(PyObject *module, int which, int who) |
| 4071 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4072 | { |
| 4073 | int retval; |
| 4074 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4075 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4076 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4077 | if (errno != 0) |
| 4078 | return posix_error(); |
| 4079 | return PyLong_FromLong((long)retval); |
| 4080 | } |
| 4081 | #endif /* HAVE_GETPRIORITY */ |
| 4082 | |
| 4083 | |
| 4084 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4085 | /*[clinic input] |
| 4086 | os.setpriority |
| 4087 | |
| 4088 | which: int |
| 4089 | who: int |
| 4090 | priority: int |
| 4091 | |
| 4092 | Set program scheduling priority. |
| 4093 | [clinic start generated code]*/ |
| 4094 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4095 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4096 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4097 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4098 | { |
| 4099 | int retval; |
| 4100 | |
| 4101 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4102 | if (retval == -1) |
| 4103 | return posix_error(); |
| 4104 | Py_RETURN_NONE; |
| 4105 | } |
| 4106 | #endif /* HAVE_SETPRIORITY */ |
| 4107 | |
| 4108 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4109 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4110 | 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] | 4111 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4112 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4113 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4114 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4115 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4116 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4117 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4118 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4119 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4120 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4121 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4122 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4123 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4124 | #ifndef HAVE_RENAMEAT |
| 4125 | if (dir_fd_specified) { |
| 4126 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4127 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4128 | } |
| 4129 | #endif |
| 4130 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4131 | #ifdef MS_WINDOWS |
| 4132 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4133 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4134 | Py_END_ALLOW_THREADS |
| 4135 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4136 | if (!result) |
| 4137 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4138 | |
| 4139 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4140 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4141 | PyErr_Format(PyExc_ValueError, |
| 4142 | "%s: src and dst must be the same type", function_name); |
| 4143 | return NULL; |
| 4144 | } |
| 4145 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4146 | Py_BEGIN_ALLOW_THREADS |
| 4147 | #ifdef HAVE_RENAMEAT |
| 4148 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4149 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4150 | else |
| 4151 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4152 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4153 | Py_END_ALLOW_THREADS |
| 4154 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4155 | if (result) |
| 4156 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4157 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4158 | Py_RETURN_NONE; |
| 4159 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4160 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4161 | |
| 4162 | /*[clinic input] |
| 4163 | os.rename |
| 4164 | |
| 4165 | src : path_t |
| 4166 | dst : path_t |
| 4167 | * |
| 4168 | src_dir_fd : dir_fd = None |
| 4169 | dst_dir_fd : dir_fd = None |
| 4170 | |
| 4171 | Rename a file or directory. |
| 4172 | |
| 4173 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4174 | descriptor open to a directory, and the respective path string (src or dst) |
| 4175 | should be relative; the path will then be relative to that directory. |
| 4176 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4177 | If they are unavailable, using them will raise a NotImplementedError. |
| 4178 | [clinic start generated code]*/ |
| 4179 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4180 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4181 | 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] | 4182 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4183 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4184 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4185 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4186 | } |
| 4187 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4188 | |
| 4189 | /*[clinic input] |
| 4190 | os.replace = os.rename |
| 4191 | |
| 4192 | Rename a file or directory, overwriting the destination. |
| 4193 | |
| 4194 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4195 | descriptor open to a directory, and the respective path string (src or dst) |
| 4196 | should be relative; the path will then be relative to that directory. |
| 4197 | 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] | 4198 | If they are unavailable, using them will raise a NotImplementedError. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4199 | [clinic start generated code]*/ |
| 4200 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4201 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4202 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4203 | int dst_dir_fd) |
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4204 | /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4205 | { |
| 4206 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4207 | } |
| 4208 | |
| 4209 | |
| 4210 | /*[clinic input] |
| 4211 | os.rmdir |
| 4212 | |
| 4213 | path: path_t |
| 4214 | * |
| 4215 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4216 | |
| 4217 | Remove a directory. |
| 4218 | |
| 4219 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4220 | and path should be relative; path will then be relative to that directory. |
| 4221 | dir_fd may not be implemented on your platform. |
| 4222 | If it is unavailable, using it will raise a NotImplementedError. |
| 4223 | [clinic start generated code]*/ |
| 4224 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4225 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4226 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4227 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4228 | { |
| 4229 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4230 | |
| 4231 | Py_BEGIN_ALLOW_THREADS |
| 4232 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4233 | /* Windows, success=1, UNIX, success=0 */ |
| 4234 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4235 | #else |
| 4236 | #ifdef HAVE_UNLINKAT |
| 4237 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4238 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4239 | else |
| 4240 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4241 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4242 | #endif |
| 4243 | Py_END_ALLOW_THREADS |
| 4244 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4245 | if (result) |
| 4246 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4247 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4248 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4251 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4252 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4253 | #ifdef MS_WINDOWS |
| 4254 | /*[clinic input] |
| 4255 | os.system -> long |
| 4256 | |
| 4257 | command: Py_UNICODE |
| 4258 | |
| 4259 | Execute the command in a subshell. |
| 4260 | [clinic start generated code]*/ |
| 4261 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4262 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4263 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4264 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4265 | { |
| 4266 | long result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4267 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4268 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4269 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4270 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4271 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4272 | return result; |
| 4273 | } |
| 4274 | #else /* MS_WINDOWS */ |
| 4275 | /*[clinic input] |
| 4276 | os.system -> long |
| 4277 | |
| 4278 | command: FSConverter |
| 4279 | |
| 4280 | Execute the command in a subshell. |
| 4281 | [clinic start generated code]*/ |
| 4282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4283 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4284 | os_system_impl(PyObject *module, PyObject *command) |
| 4285 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4286 | { |
| 4287 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4288 | const char *bytes = PyBytes_AsString(command); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4289 | Py_BEGIN_ALLOW_THREADS |
| 4290 | result = system(bytes); |
| 4291 | Py_END_ALLOW_THREADS |
| 4292 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4293 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4294 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4295 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4296 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4297 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4298 | /*[clinic input] |
| 4299 | os.umask |
| 4300 | |
| 4301 | mask: int |
| 4302 | / |
| 4303 | |
| 4304 | Set the current numeric umask and return the previous umask. |
| 4305 | [clinic start generated code]*/ |
| 4306 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4307 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4308 | os_umask_impl(PyObject *module, int mask) |
| 4309 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4310 | { |
| 4311 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4312 | if (i < 0) |
| 4313 | return posix_error(); |
| 4314 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4317 | #ifdef MS_WINDOWS |
| 4318 | |
| 4319 | /* override the default DeleteFileW behavior so that directory |
| 4320 | symlinks can be removed with this function, the same as with |
| 4321 | Unix symlinks */ |
| 4322 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4323 | { |
| 4324 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4325 | WIN32_FIND_DATAW find_data; |
| 4326 | HANDLE find_data_handle; |
| 4327 | int is_directory = 0; |
| 4328 | int is_link = 0; |
| 4329 | |
| 4330 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4331 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4332 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4333 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4334 | it is a symlink */ |
| 4335 | if(is_directory && |
| 4336 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4337 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4338 | |
| 4339 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4340 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4341 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4342 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4343 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4344 | FindClose(find_data_handle); |
| 4345 | } |
| 4346 | } |
| 4347 | } |
| 4348 | |
| 4349 | if (is_directory && is_link) |
| 4350 | return RemoveDirectoryW(lpFileName); |
| 4351 | |
| 4352 | return DeleteFileW(lpFileName); |
| 4353 | } |
| 4354 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4355 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4356 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4357 | /*[clinic input] |
| 4358 | os.unlink |
| 4359 | |
| 4360 | path: path_t |
| 4361 | * |
| 4362 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4363 | |
| 4364 | Remove a file (same as remove()). |
| 4365 | |
| 4366 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4367 | and path should be relative; path will then be relative to that directory. |
| 4368 | dir_fd may not be implemented on your platform. |
| 4369 | If it is unavailable, using it will raise a NotImplementedError. |
| 4370 | |
| 4371 | [clinic start generated code]*/ |
| 4372 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4373 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4374 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4375 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4376 | { |
| 4377 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4378 | |
| 4379 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4380 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4381 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4382 | /* Windows, success=1, UNIX, success=0 */ |
| 4383 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4384 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4385 | #ifdef HAVE_UNLINKAT |
| 4386 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4387 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4388 | else |
| 4389 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4390 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4391 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4392 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4393 | Py_END_ALLOW_THREADS |
| 4394 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4395 | if (result) |
| 4396 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4397 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4398 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4401 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4402 | /*[clinic input] |
| 4403 | os.remove = os.unlink |
| 4404 | |
| 4405 | Remove a file (same as unlink()). |
| 4406 | |
| 4407 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4408 | and path should be relative; path will then be relative to that directory. |
| 4409 | dir_fd may not be implemented on your platform. |
| 4410 | If it is unavailable, using it will raise a NotImplementedError. |
| 4411 | [clinic start generated code]*/ |
| 4412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4413 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4414 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4415 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4416 | { |
| 4417 | return os_unlink_impl(module, path, dir_fd); |
| 4418 | } |
| 4419 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4420 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4421 | static PyStructSequence_Field uname_result_fields[] = { |
| 4422 | {"sysname", "operating system name"}, |
| 4423 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4424 | {"release", "operating system release"}, |
| 4425 | {"version", "operating system version"}, |
| 4426 | {"machine", "hardware identifier"}, |
| 4427 | {NULL} |
| 4428 | }; |
| 4429 | |
| 4430 | PyDoc_STRVAR(uname_result__doc__, |
| 4431 | "uname_result: Result from os.uname().\n\n\ |
| 4432 | This object may be accessed either as a tuple of\n\ |
| 4433 | (sysname, nodename, release, version, machine),\n\ |
| 4434 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4435 | \n\ |
| 4436 | See os.uname for more information."); |
| 4437 | |
| 4438 | static PyStructSequence_Desc uname_result_desc = { |
| 4439 | "uname_result", /* name */ |
| 4440 | uname_result__doc__, /* doc */ |
| 4441 | uname_result_fields, |
| 4442 | 5 |
| 4443 | }; |
| 4444 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 4445 | static PyTypeObject* UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4446 | |
| 4447 | |
| 4448 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4449 | /*[clinic input] |
| 4450 | os.uname |
| 4451 | |
| 4452 | Return an object identifying the current operating system. |
| 4453 | |
| 4454 | The object behaves like a named tuple with the following fields: |
| 4455 | (sysname, nodename, release, version, machine) |
| 4456 | |
| 4457 | [clinic start generated code]*/ |
| 4458 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4459 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4460 | os_uname_impl(PyObject *module) |
| 4461 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4462 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4463 | struct utsname u; |
| 4464 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4465 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4466 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4467 | Py_BEGIN_ALLOW_THREADS |
| 4468 | res = uname(&u); |
| 4469 | Py_END_ALLOW_THREADS |
| 4470 | if (res < 0) |
| 4471 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4472 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 4473 | value = PyStructSequence_New(UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4474 | if (value == NULL) |
| 4475 | return NULL; |
| 4476 | |
| 4477 | #define SET(i, field) \ |
| 4478 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4479 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4480 | if (!o) { \ |
| 4481 | Py_DECREF(value); \ |
| 4482 | return NULL; \ |
| 4483 | } \ |
| 4484 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4485 | } \ |
| 4486 | |
| 4487 | SET(0, u.sysname); |
| 4488 | SET(1, u.nodename); |
| 4489 | SET(2, u.release); |
| 4490 | SET(3, u.version); |
| 4491 | SET(4, u.machine); |
| 4492 | |
| 4493 | #undef SET |
| 4494 | |
| 4495 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4496 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4497 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4498 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4499 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4500 | |
| 4501 | typedef struct { |
| 4502 | int now; |
| 4503 | time_t atime_s; |
| 4504 | long atime_ns; |
| 4505 | time_t mtime_s; |
| 4506 | long mtime_ns; |
| 4507 | } utime_t; |
| 4508 | |
| 4509 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4510 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4511 | * they also intentionally leak the declaration of a pointer named "time" |
| 4512 | */ |
| 4513 | #define UTIME_TO_TIMESPEC \ |
| 4514 | struct timespec ts[2]; \ |
| 4515 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4516 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4517 | time = NULL; \ |
| 4518 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4519 | ts[0].tv_sec = ut->atime_s; \ |
| 4520 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4521 | ts[1].tv_sec = ut->mtime_s; \ |
| 4522 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4523 | time = ts; \ |
| 4524 | } \ |
| 4525 | |
| 4526 | #define UTIME_TO_TIMEVAL \ |
| 4527 | struct timeval tv[2]; \ |
| 4528 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4529 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4530 | time = NULL; \ |
| 4531 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4532 | tv[0].tv_sec = ut->atime_s; \ |
| 4533 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4534 | tv[1].tv_sec = ut->mtime_s; \ |
| 4535 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4536 | time = tv; \ |
| 4537 | } \ |
| 4538 | |
| 4539 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4540 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4541 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4542 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4543 | time = NULL; \ |
| 4544 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4545 | u.actime = ut->atime_s; \ |
| 4546 | u.modtime = ut->mtime_s; \ |
| 4547 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4548 | } |
| 4549 | |
| 4550 | #define UTIME_TO_TIME_T \ |
| 4551 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4552 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4553 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4554 | time = NULL; \ |
| 4555 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4556 | timet[0] = ut->atime_s; \ |
| 4557 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4558 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4559 | } \ |
| 4560 | |
| 4561 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4562 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4563 | |
| 4564 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4565 | 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] | 4566 | { |
| 4567 | #ifdef HAVE_UTIMENSAT |
| 4568 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4569 | UTIME_TO_TIMESPEC; |
| 4570 | return utimensat(dir_fd, path, time, flags); |
| 4571 | #elif defined(HAVE_FUTIMESAT) |
| 4572 | UTIME_TO_TIMEVAL; |
| 4573 | /* |
| 4574 | * follow_symlinks will never be false here; |
| 4575 | * we only allow !follow_symlinks and dir_fd together |
| 4576 | * if we have utimensat() |
| 4577 | */ |
| 4578 | assert(follow_symlinks); |
| 4579 | return futimesat(dir_fd, path, time); |
| 4580 | #endif |
| 4581 | } |
| 4582 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4583 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4584 | #else |
| 4585 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4586 | #endif |
| 4587 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4588 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4589 | |
| 4590 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4591 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4592 | { |
| 4593 | #ifdef HAVE_FUTIMENS |
| 4594 | UTIME_TO_TIMESPEC; |
| 4595 | return futimens(fd, time); |
| 4596 | #else |
| 4597 | UTIME_TO_TIMEVAL; |
| 4598 | return futimes(fd, time); |
| 4599 | #endif |
| 4600 | } |
| 4601 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4602 | #define PATH_UTIME_HAVE_FD 1 |
| 4603 | #else |
| 4604 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4605 | #endif |
| 4606 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4607 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4608 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4609 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4610 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4611 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4612 | |
| 4613 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4614 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4615 | { |
| 4616 | #ifdef HAVE_UTIMENSAT |
| 4617 | UTIME_TO_TIMESPEC; |
| 4618 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4619 | #else |
| 4620 | UTIME_TO_TIMEVAL; |
| 4621 | return lutimes(path, time); |
| 4622 | #endif |
| 4623 | } |
| 4624 | |
| 4625 | #endif |
| 4626 | |
| 4627 | #ifndef MS_WINDOWS |
| 4628 | |
| 4629 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4630 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4631 | { |
| 4632 | #ifdef HAVE_UTIMENSAT |
| 4633 | UTIME_TO_TIMESPEC; |
| 4634 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4635 | #elif defined(HAVE_UTIMES) |
| 4636 | UTIME_TO_TIMEVAL; |
| 4637 | return utimes(path, time); |
| 4638 | #elif defined(HAVE_UTIME_H) |
| 4639 | UTIME_TO_UTIMBUF; |
| 4640 | return utime(path, time); |
| 4641 | #else |
| 4642 | UTIME_TO_TIME_T; |
| 4643 | return utime(path, time); |
| 4644 | #endif |
| 4645 | } |
| 4646 | |
| 4647 | #endif |
| 4648 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4649 | static int |
| 4650 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4651 | { |
| 4652 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4653 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4654 | divmod = PyNumber_Divmod(py_long, billion); |
| 4655 | if (!divmod) |
| 4656 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4657 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 4658 | PyErr_Format(PyExc_TypeError, |
| 4659 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
| 4660 | Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name); |
| 4661 | goto exit; |
| 4662 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4663 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4664 | if ((*s == -1) && PyErr_Occurred()) |
| 4665 | goto exit; |
| 4666 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4667 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4668 | goto exit; |
| 4669 | |
| 4670 | result = 1; |
| 4671 | exit: |
| 4672 | Py_XDECREF(divmod); |
| 4673 | return result; |
| 4674 | } |
| 4675 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4676 | |
| 4677 | /*[clinic input] |
| 4678 | os.utime |
| 4679 | |
| 4680 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
| 4681 | times: object = NULL |
| 4682 | * |
| 4683 | ns: object = NULL |
| 4684 | dir_fd: dir_fd(requires='futimensat') = None |
| 4685 | follow_symlinks: bool=True |
| 4686 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4687 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4688 | |
| 4689 | Set the access and modified time of path. |
| 4690 | |
| 4691 | path may always be specified as a string. |
| 4692 | On some platforms, path may also be specified as an open file descriptor. |
| 4693 | If this functionality is unavailable, using it raises an exception. |
| 4694 | |
| 4695 | If times is not None, it must be a tuple (atime, mtime); |
| 4696 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4697 | 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] | 4698 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4699 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4700 | 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] | 4701 | Specifying tuples for both times and ns is an error. |
| 4702 | |
| 4703 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4704 | and path should be relative; path will then be relative to that directory. |
| 4705 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4706 | link, utime will modify the symbolic link itself instead of the file the |
| 4707 | link points to. |
| 4708 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4709 | as an open file descriptor. |
| 4710 | dir_fd and follow_symlinks may not be available on your platform. |
| 4711 | If they are unavailable, using them will raise a NotImplementedError. |
| 4712 | |
| 4713 | [clinic start generated code]*/ |
| 4714 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4715 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4716 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4717 | int dir_fd, int follow_symlinks) |
| 4718 | /*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4719 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4720 | #ifdef MS_WINDOWS |
| 4721 | HANDLE hFile; |
| 4722 | FILETIME atime, mtime; |
| 4723 | #else |
| 4724 | int result; |
| 4725 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4726 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4727 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4728 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4729 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4730 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4731 | if (times && (times != Py_None) && ns) { |
| 4732 | PyErr_SetString(PyExc_ValueError, |
| 4733 | "utime: you may specify either 'times'" |
| 4734 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4735 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4739 | time_t a_sec, m_sec; |
| 4740 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4741 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4742 | PyErr_SetString(PyExc_TypeError, |
| 4743 | "utime: 'times' must be either" |
| 4744 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4745 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4746 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4747 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4748 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4749 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4750 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4751 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4752 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4753 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4754 | utime.atime_s = a_sec; |
| 4755 | utime.atime_ns = a_nsec; |
| 4756 | utime.mtime_s = m_sec; |
| 4757 | utime.mtime_ns = m_nsec; |
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 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4760 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4761 | PyErr_SetString(PyExc_TypeError, |
| 4762 | "utime: 'ns' must be a tuple of two ints"); |
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 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4765 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4766 | 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] | 4767 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4768 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4769 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4770 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4771 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4772 | } |
| 4773 | else { |
| 4774 | /* times and ns are both None/unspecified. use "now". */ |
| 4775 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4776 | } |
| 4777 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4778 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4779 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4780 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4781 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4782 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4783 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4784 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4785 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4786 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4787 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4788 | #if !defined(HAVE_UTIMENSAT) |
| 4789 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4790 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4791 | "utime: cannot use dir_fd and follow_symlinks " |
| 4792 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4793 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4794 | } |
| 4795 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4796 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4797 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4798 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4799 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 4800 | NULL, OPEN_EXISTING, |
| 4801 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4802 | Py_END_ALLOW_THREADS |
| 4803 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4804 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4805 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4806 | } |
| 4807 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4808 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4809 | GetSystemTimeAsFileTime(&mtime); |
| 4810 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4811 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4812 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4813 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4814 | _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] | 4815 | } |
| 4816 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4817 | /* Avoid putting the file name into the error here, |
| 4818 | as that may confuse the user into believing that |
| 4819 | something is wrong with the file, when it also |
| 4820 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4821 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4822 | CloseHandle(hFile); |
| 4823 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4824 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4825 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4826 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4827 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4828 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4829 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4830 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4831 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4832 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4833 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4834 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4835 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4836 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4837 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4838 | else |
| 4839 | #endif |
| 4840 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4841 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4842 | if (path->fd != -1) |
| 4843 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4844 | else |
| 4845 | #endif |
| 4846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4847 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4848 | |
| 4849 | Py_END_ALLOW_THREADS |
| 4850 | |
| 4851 | if (result < 0) { |
| 4852 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4853 | posix_error(); |
| 4854 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4855 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4856 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4857 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4858 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4859 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4860 | } |
| 4861 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4862 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4863 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4864 | |
| 4865 | /*[clinic input] |
| 4866 | os._exit |
| 4867 | |
| 4868 | status: int |
| 4869 | |
| 4870 | Exit to the system with specified status, without normal exit processing. |
| 4871 | [clinic start generated code]*/ |
| 4872 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4873 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4874 | os__exit_impl(PyObject *module, int status) |
| 4875 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4876 | { |
| 4877 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4878 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4879 | } |
| 4880 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4881 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4882 | #define EXECV_CHAR wchar_t |
| 4883 | #else |
| 4884 | #define EXECV_CHAR char |
| 4885 | #endif |
| 4886 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 4887 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4888 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4889 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4890 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4891 | Py_ssize_t i; |
| 4892 | for (i = 0; i < count; i++) |
| 4893 | PyMem_Free(array[i]); |
| 4894 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4895 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4896 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4897 | static int |
| 4898 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4899 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4900 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4901 | PyObject *ub; |
| 4902 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4903 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4904 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4905 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4906 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 4907 | if (*out) |
| 4908 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4909 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4910 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4911 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4912 | size = PyBytes_GET_SIZE(ub); |
| 4913 | *out = PyMem_Malloc(size + 1); |
| 4914 | if (*out) { |
| 4915 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 4916 | result = 1; |
| 4917 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4918 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4919 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4920 | Py_DECREF(ub); |
| 4921 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4922 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4923 | #endif |
| 4924 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 4925 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4926 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4927 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4928 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4929 | Py_ssize_t i, pos, envc; |
| 4930 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4931 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4932 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4933 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4934 | i = PyMapping_Size(env); |
| 4935 | if (i < 0) |
| 4936 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4937 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4938 | if (envlist == NULL) { |
| 4939 | PyErr_NoMemory(); |
| 4940 | return NULL; |
| 4941 | } |
| 4942 | envc = 0; |
| 4943 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4944 | if (!keys) |
| 4945 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4946 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4947 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4948 | goto error; |
| 4949 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4950 | PyErr_Format(PyExc_TypeError, |
| 4951 | "env.keys() or env.values() is not a list"); |
| 4952 | goto error; |
| 4953 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4954 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4955 | for (pos = 0; pos < i; pos++) { |
| 4956 | key = PyList_GetItem(keys, pos); |
| 4957 | val = PyList_GetItem(vals, pos); |
| 4958 | if (!key || !val) |
| 4959 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4960 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4961 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4962 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 4963 | goto error; |
| 4964 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 4965 | Py_DECREF(key2); |
| 4966 | goto error; |
| 4967 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4968 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 4969 | defining hidden environment variables. */ |
| 4970 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 4971 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 4972 | { |
| 4973 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 4974 | Py_DECREF(key2); |
| 4975 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4976 | goto error; |
| 4977 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4978 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 4979 | #else |
| 4980 | if (!PyUnicode_FSConverter(key, &key2)) |
| 4981 | goto error; |
| 4982 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 4983 | Py_DECREF(key2); |
| 4984 | goto error; |
| 4985 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4986 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 4987 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 4988 | { |
| 4989 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 4990 | Py_DECREF(key2); |
| 4991 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4992 | goto error; |
| 4993 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4994 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 4995 | PyBytes_AS_STRING(val2)); |
| 4996 | #endif |
| 4997 | Py_DECREF(key2); |
| 4998 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4999 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5000 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5001 | |
| 5002 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 5003 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5004 | goto error; |
| 5005 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5006 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5007 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5008 | } |
| 5009 | Py_DECREF(vals); |
| 5010 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5011 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5012 | envlist[envc] = 0; |
| 5013 | *envc_ptr = envc; |
| 5014 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5015 | |
| 5016 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5017 | Py_XDECREF(keys); |
| 5018 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5019 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5020 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5021 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5022 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5023 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5024 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5025 | { |
| 5026 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5027 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5028 | if (argvlist == NULL) { |
| 5029 | PyErr_NoMemory(); |
| 5030 | return NULL; |
| 5031 | } |
| 5032 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5033 | PyObject* item = PySequence_ITEM(argv, i); |
| 5034 | if (item == NULL) |
| 5035 | goto fail; |
| 5036 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5037 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5038 | goto fail; |
| 5039 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5040 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5041 | } |
| 5042 | argvlist[*argc] = NULL; |
| 5043 | return argvlist; |
| 5044 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5045 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5046 | free_string_array(argvlist, *argc); |
| 5047 | return NULL; |
| 5048 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5049 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5050 | #endif |
| 5051 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5052 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5053 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5054 | /*[clinic input] |
| 5055 | os.execv |
| 5056 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5057 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5058 | Path of executable file. |
| 5059 | argv: object |
| 5060 | Tuple or list of strings. |
| 5061 | / |
| 5062 | |
| 5063 | Execute an executable path with arguments, replacing current process. |
| 5064 | [clinic start generated code]*/ |
| 5065 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5066 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5067 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5068 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5069 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5070 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5071 | Py_ssize_t argc; |
| 5072 | |
| 5073 | /* execv has two arguments: (path, argv), where |
| 5074 | argv is a list or tuple of strings. */ |
| 5075 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5076 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5077 | PyErr_SetString(PyExc_TypeError, |
| 5078 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5079 | return NULL; |
| 5080 | } |
| 5081 | argc = PySequence_Size(argv); |
| 5082 | if (argc < 1) { |
| 5083 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5084 | return NULL; |
| 5085 | } |
| 5086 | |
| 5087 | argvlist = parse_arglist(argv, &argc); |
| 5088 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5089 | return NULL; |
| 5090 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5091 | if (!argvlist[0][0]) { |
| 5092 | PyErr_SetString(PyExc_ValueError, |
| 5093 | "execv() arg 2 first element cannot be empty"); |
| 5094 | free_string_array(argvlist, argc); |
| 5095 | return NULL; |
| 5096 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5097 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5098 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5099 | #ifdef HAVE_WEXECV |
| 5100 | _wexecv(path->wide, argvlist); |
| 5101 | #else |
| 5102 | execv(path->narrow, argvlist); |
| 5103 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5104 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5105 | |
| 5106 | /* If we get here it's definitely an error */ |
| 5107 | |
| 5108 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5109 | return posix_error(); |
| 5110 | } |
| 5111 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5112 | |
| 5113 | /*[clinic input] |
| 5114 | os.execve |
| 5115 | |
| 5116 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5117 | Path of executable file. |
| 5118 | argv: object |
| 5119 | Tuple or list of strings. |
| 5120 | env: object |
| 5121 | Dictionary of strings mapping to strings. |
| 5122 | |
| 5123 | Execute an executable path with arguments, replacing current process. |
| 5124 | [clinic start generated code]*/ |
| 5125 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5126 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5127 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5128 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5129 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5130 | EXECV_CHAR **argvlist = NULL; |
| 5131 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5132 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5133 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5134 | /* execve has three arguments: (path, argv, env), where |
| 5135 | argv is a list or tuple of strings and env is a dictionary |
| 5136 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5137 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5138 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5139 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5140 | "execve: argv must be a tuple or list"); |
| 5141 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5142 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5143 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5144 | if (argc < 1) { |
| 5145 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5146 | return NULL; |
| 5147 | } |
| 5148 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5149 | if (!PyMapping_Check(env)) { |
| 5150 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5151 | "execve: environment must be a mapping object"); |
| 5152 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5153 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5154 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5155 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5156 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5157 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5158 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5159 | if (!argvlist[0][0]) { |
| 5160 | PyErr_SetString(PyExc_ValueError, |
| 5161 | "execve: argv first element cannot be empty"); |
| 5162 | goto fail; |
| 5163 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5164 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5165 | envlist = parse_envlist(env, &envc); |
| 5166 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5167 | goto fail; |
| 5168 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5169 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5170 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5171 | if (path->fd > -1) |
| 5172 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5173 | else |
| 5174 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5175 | #ifdef HAVE_WEXECV |
| 5176 | _wexecve(path->wide, argvlist, envlist); |
| 5177 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5178 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5179 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5180 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5181 | |
| 5182 | /* If we get here it's definitely an error */ |
| 5183 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5184 | posix_path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5185 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5186 | free_string_array(envlist, envc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5187 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5188 | if (argvlist) |
| 5189 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5190 | return NULL; |
| 5191 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5192 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5193 | #endif /* HAVE_EXECV */ |
| 5194 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5195 | #ifdef HAVE_POSIX_SPAWN |
| 5196 | |
| 5197 | enum posix_spawn_file_actions_identifier { |
| 5198 | POSIX_SPAWN_OPEN, |
| 5199 | POSIX_SPAWN_CLOSE, |
| 5200 | POSIX_SPAWN_DUP2 |
| 5201 | }; |
| 5202 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5203 | #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] | 5204 | static int |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5205 | convert_sched_param(PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5206 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5207 | |
| 5208 | static int |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5209 | parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup, |
| 5210 | int resetids, int setsid, PyObject *setsigmask, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5211 | PyObject *setsigdef, PyObject *scheduler, |
| 5212 | posix_spawnattr_t *attrp) |
| 5213 | { |
| 5214 | long all_flags = 0; |
| 5215 | |
| 5216 | errno = posix_spawnattr_init(attrp); |
| 5217 | if (errno) { |
| 5218 | posix_error(); |
| 5219 | return -1; |
| 5220 | } |
| 5221 | |
| 5222 | if (setpgroup) { |
| 5223 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5224 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5225 | goto fail; |
| 5226 | } |
| 5227 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5228 | if (errno) { |
| 5229 | posix_error(); |
| 5230 | goto fail; |
| 5231 | } |
| 5232 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5233 | } |
| 5234 | |
| 5235 | if (resetids) { |
| 5236 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5237 | } |
| 5238 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5239 | if (setsid) { |
| 5240 | #ifdef POSIX_SPAWN_SETSID |
| 5241 | all_flags |= POSIX_SPAWN_SETSID; |
| 5242 | #elif defined(POSIX_SPAWN_SETSID_NP) |
| 5243 | all_flags |= POSIX_SPAWN_SETSID_NP; |
| 5244 | #else |
| 5245 | argument_unavailable_error(func_name, "setsid"); |
| 5246 | return -1; |
| 5247 | #endif |
| 5248 | } |
| 5249 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5250 | if (setsigmask) { |
| 5251 | sigset_t set; |
| 5252 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5253 | goto fail; |
| 5254 | } |
| 5255 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5256 | if (errno) { |
| 5257 | posix_error(); |
| 5258 | goto fail; |
| 5259 | } |
| 5260 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5261 | } |
| 5262 | |
| 5263 | if (setsigdef) { |
| 5264 | sigset_t set; |
| 5265 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5266 | goto fail; |
| 5267 | } |
| 5268 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5269 | if (errno) { |
| 5270 | posix_error(); |
| 5271 | goto fail; |
| 5272 | } |
| 5273 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5274 | } |
| 5275 | |
| 5276 | if (scheduler) { |
| 5277 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5278 | PyObject *py_schedpolicy; |
| 5279 | struct sched_param schedparam; |
| 5280 | |
| 5281 | if (!PyArg_ParseTuple(scheduler, "OO&" |
| 5282 | ";A scheduler tuple must have two elements", |
| 5283 | &py_schedpolicy, convert_sched_param, &schedparam)) { |
| 5284 | goto fail; |
| 5285 | } |
| 5286 | if (py_schedpolicy != Py_None) { |
| 5287 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5288 | |
| 5289 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5290 | goto fail; |
| 5291 | } |
| 5292 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5293 | if (errno) { |
| 5294 | posix_error(); |
| 5295 | goto fail; |
| 5296 | } |
| 5297 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5298 | } |
| 5299 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5300 | if (errno) { |
| 5301 | posix_error(); |
| 5302 | goto fail; |
| 5303 | } |
| 5304 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5305 | #else |
| 5306 | PyErr_SetString(PyExc_NotImplementedError, |
| 5307 | "The scheduler option is not supported in this system."); |
| 5308 | goto fail; |
| 5309 | #endif |
| 5310 | } |
| 5311 | |
| 5312 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5313 | if (errno) { |
| 5314 | posix_error(); |
| 5315 | goto fail; |
| 5316 | } |
| 5317 | |
| 5318 | return 0; |
| 5319 | |
| 5320 | fail: |
| 5321 | (void)posix_spawnattr_destroy(attrp); |
| 5322 | return -1; |
| 5323 | } |
| 5324 | |
| 5325 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5326 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5327 | posix_spawn_file_actions_t *file_actionsp, |
| 5328 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5329 | { |
| 5330 | PyObject *seq; |
| 5331 | PyObject *file_action = NULL; |
| 5332 | PyObject *tag_obj; |
| 5333 | |
| 5334 | seq = PySequence_Fast(file_actions, |
| 5335 | "file_actions must be a sequence or None"); |
| 5336 | if (seq == NULL) { |
| 5337 | return -1; |
| 5338 | } |
| 5339 | |
| 5340 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5341 | if (errno) { |
| 5342 | posix_error(); |
| 5343 | Py_DECREF(seq); |
| 5344 | return -1; |
| 5345 | } |
| 5346 | |
| 5347 | for (int i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) { |
| 5348 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5349 | Py_INCREF(file_action); |
| 5350 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5351 | PyErr_SetString(PyExc_TypeError, |
| 5352 | "Each file_actions element must be a non-empty tuple"); |
| 5353 | goto fail; |
| 5354 | } |
| 5355 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5356 | if (tag == -1 && PyErr_Occurred()) { |
| 5357 | goto fail; |
| 5358 | } |
| 5359 | |
| 5360 | /* Populate the file_actions object */ |
| 5361 | switch (tag) { |
| 5362 | case POSIX_SPAWN_OPEN: { |
| 5363 | int fd, oflag; |
| 5364 | PyObject *path; |
| 5365 | unsigned long mode; |
| 5366 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5367 | ";A open file_action tuple must have 5 elements", |
| 5368 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5369 | &oflag, &mode)) |
| 5370 | { |
| 5371 | goto fail; |
| 5372 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5373 | if (PyList_Append(temp_buffer, path)) { |
| 5374 | Py_DECREF(path); |
| 5375 | goto fail; |
| 5376 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5377 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5378 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5379 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5380 | if (errno) { |
| 5381 | posix_error(); |
| 5382 | goto fail; |
| 5383 | } |
| 5384 | break; |
| 5385 | } |
| 5386 | case POSIX_SPAWN_CLOSE: { |
| 5387 | int fd; |
| 5388 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5389 | ";A close file_action tuple must have 2 elements", |
| 5390 | &tag_obj, &fd)) |
| 5391 | { |
| 5392 | goto fail; |
| 5393 | } |
| 5394 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 5395 | if (errno) { |
| 5396 | posix_error(); |
| 5397 | goto fail; |
| 5398 | } |
| 5399 | break; |
| 5400 | } |
| 5401 | case POSIX_SPAWN_DUP2: { |
| 5402 | int fd1, fd2; |
| 5403 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 5404 | ";A dup2 file_action tuple must have 3 elements", |
| 5405 | &tag_obj, &fd1, &fd2)) |
| 5406 | { |
| 5407 | goto fail; |
| 5408 | } |
| 5409 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 5410 | fd1, fd2); |
| 5411 | if (errno) { |
| 5412 | posix_error(); |
| 5413 | goto fail; |
| 5414 | } |
| 5415 | break; |
| 5416 | } |
| 5417 | default: { |
| 5418 | PyErr_SetString(PyExc_TypeError, |
| 5419 | "Unknown file_actions identifier"); |
| 5420 | goto fail; |
| 5421 | } |
| 5422 | } |
| 5423 | Py_DECREF(file_action); |
| 5424 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5425 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5426 | Py_DECREF(seq); |
| 5427 | return 0; |
| 5428 | |
| 5429 | fail: |
| 5430 | Py_DECREF(seq); |
| 5431 | Py_DECREF(file_action); |
| 5432 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 5433 | return -1; |
| 5434 | } |
| 5435 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5436 | |
| 5437 | static PyObject * |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5438 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
| 5439 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5440 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5441 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5442 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5443 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5444 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5445 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5446 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5447 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5448 | posix_spawnattr_t attr; |
| 5449 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5450 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5451 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5452 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5453 | pid_t pid; |
| 5454 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5455 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5456 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5457 | 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] | 5458 | like posix.environ. */ |
| 5459 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5460 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5461 | PyErr_Format(PyExc_TypeError, |
| 5462 | "%s: argv must be a tuple or list", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5463 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5464 | } |
| 5465 | argc = PySequence_Size(argv); |
| 5466 | if (argc < 1) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5467 | PyErr_Format(PyExc_ValueError, |
| 5468 | "%s: argv must not be empty", func_name); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5469 | return NULL; |
| 5470 | } |
| 5471 | |
| 5472 | if (!PyMapping_Check(env)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5473 | PyErr_Format(PyExc_TypeError, |
| 5474 | "%s: environment must be a mapping object", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5475 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | argvlist = parse_arglist(argv, &argc); |
| 5479 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5480 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5481 | } |
| 5482 | if (!argvlist[0][0]) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5483 | PyErr_Format(PyExc_ValueError, |
| 5484 | "%s: argv first element cannot be empty", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5485 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5486 | } |
| 5487 | |
| 5488 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5489 | if (envlist == NULL) { |
| 5490 | goto exit; |
| 5491 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5492 | |
Anthony Shaw | 948ed8c | 2019-05-10 12:00:06 +1000 | [diff] [blame] | 5493 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5494 | /* There is a bug in old versions of glibc that makes some of the |
| 5495 | * helper functions for manipulating file actions not copy the provided |
| 5496 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 5497 | * copy the value of path for some old versions of glibc (<2.20). |
| 5498 | * The use of temp_buffer here is a workaround that keeps the |
| 5499 | * python objects that own the buffers alive until posix_spawn gets called. |
| 5500 | * Check https://bugs.python.org/issue33630 and |
| 5501 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 5502 | temp_buffer = PyList_New(0); |
| 5503 | if (!temp_buffer) { |
| 5504 | goto exit; |
| 5505 | } |
| 5506 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5507 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5508 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5509 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5510 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5511 | |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5512 | if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid, |
| 5513 | setsigmask, setsigdef, scheduler, &attr)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5514 | goto exit; |
| 5515 | } |
| 5516 | attrp = &attr; |
| 5517 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5518 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5519 | #ifdef HAVE_POSIX_SPAWNP |
| 5520 | if (use_posix_spawnp) { |
| 5521 | err_code = posix_spawnp(&pid, path->narrow, |
| 5522 | file_actionsp, attrp, argvlist, envlist); |
| 5523 | } |
| 5524 | else |
| 5525 | #endif /* HAVE_POSIX_SPAWNP */ |
| 5526 | { |
| 5527 | err_code = posix_spawn(&pid, path->narrow, |
| 5528 | file_actionsp, attrp, argvlist, envlist); |
| 5529 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5530 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5531 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5532 | if (err_code) { |
| 5533 | errno = err_code; |
| 5534 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5535 | goto exit; |
| 5536 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 5537 | #ifdef _Py_MEMORY_SANITIZER |
| 5538 | __msan_unpoison(&pid, sizeof(pid)); |
| 5539 | #endif |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5540 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5541 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5542 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5543 | if (file_actionsp) { |
| 5544 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5545 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5546 | if (attrp) { |
| 5547 | (void)posix_spawnattr_destroy(attrp); |
| 5548 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5549 | if (envlist) { |
| 5550 | free_string_array(envlist, envc); |
| 5551 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5552 | if (argvlist) { |
| 5553 | free_string_array(argvlist, argc); |
| 5554 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5555 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5556 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5557 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5558 | |
| 5559 | |
| 5560 | /*[clinic input] |
| 5561 | |
| 5562 | os.posix_spawn |
| 5563 | path: path_t |
| 5564 | Path of executable file. |
| 5565 | argv: object |
| 5566 | Tuple or list of strings. |
| 5567 | env: object |
| 5568 | Dictionary of strings mapping to strings. |
| 5569 | / |
| 5570 | * |
| 5571 | file_actions: object(c_default='NULL') = () |
| 5572 | A sequence of file action tuples. |
| 5573 | setpgroup: object = NULL |
| 5574 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5575 | resetids: bool(accept={int}) = False |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5576 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
| 5577 | setsid: bool(accept={int}) = False |
| 5578 | 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] | 5579 | setsigmask: object(c_default='NULL') = () |
| 5580 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5581 | setsigdef: object(c_default='NULL') = () |
| 5582 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5583 | scheduler: object = NULL |
| 5584 | A tuple with the scheduler policy (optional) and parameters. |
| 5585 | |
| 5586 | Execute the program specified by path in a new process. |
| 5587 | [clinic start generated code]*/ |
| 5588 | |
| 5589 | static PyObject * |
| 5590 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5591 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5592 | PyObject *setpgroup, int resetids, int setsid, |
| 5593 | PyObject *setsigmask, PyObject *setsigdef, |
| 5594 | PyObject *scheduler) |
| 5595 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5596 | { |
| 5597 | return py_posix_spawn(0, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5598 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5599 | scheduler); |
| 5600 | } |
| 5601 | #endif /* HAVE_POSIX_SPAWN */ |
| 5602 | |
| 5603 | |
| 5604 | |
| 5605 | #ifdef HAVE_POSIX_SPAWNP |
| 5606 | /*[clinic input] |
| 5607 | |
| 5608 | os.posix_spawnp |
| 5609 | path: path_t |
| 5610 | Path of executable file. |
| 5611 | argv: object |
| 5612 | Tuple or list of strings. |
| 5613 | env: object |
| 5614 | Dictionary of strings mapping to strings. |
| 5615 | / |
| 5616 | * |
| 5617 | file_actions: object(c_default='NULL') = () |
| 5618 | A sequence of file action tuples. |
| 5619 | setpgroup: object = NULL |
| 5620 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5621 | resetids: bool(accept={int}) = False |
| 5622 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5623 | setsid: bool(accept={int}) = False |
| 5624 | 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] | 5625 | setsigmask: object(c_default='NULL') = () |
| 5626 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5627 | setsigdef: object(c_default='NULL') = () |
| 5628 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5629 | scheduler: object = NULL |
| 5630 | A tuple with the scheduler policy (optional) and parameters. |
| 5631 | |
| 5632 | Execute the program specified by path in a new process. |
| 5633 | [clinic start generated code]*/ |
| 5634 | |
| 5635 | static PyObject * |
| 5636 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5637 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5638 | PyObject *setpgroup, int resetids, int setsid, |
| 5639 | PyObject *setsigmask, PyObject *setsigdef, |
| 5640 | PyObject *scheduler) |
| 5641 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5642 | { |
| 5643 | return py_posix_spawn(1, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5644 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5645 | scheduler); |
| 5646 | } |
| 5647 | #endif /* HAVE_POSIX_SPAWNP */ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5648 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5649 | #ifdef HAVE_RTPSPAWN |
| 5650 | static intptr_t |
| 5651 | _rtp_spawn(int mode, const char *rtpFileName, const char *argv[], |
| 5652 | const char *envp[]) |
| 5653 | { |
| 5654 | RTP_ID rtpid; |
| 5655 | int status; |
| 5656 | pid_t res; |
| 5657 | int async_err = 0; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5658 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5659 | /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes. |
| 5660 | uStackSize=0 cannot be used, the default stack size is too small for |
| 5661 | Python. */ |
| 5662 | if (envp) { |
| 5663 | rtpid = rtpSpawn(rtpFileName, argv, envp, |
| 5664 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5665 | } |
| 5666 | else { |
| 5667 | rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ, |
| 5668 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5669 | } |
| 5670 | if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) { |
| 5671 | do { |
| 5672 | res = waitpid((pid_t)rtpid, &status, 0); |
| 5673 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 5674 | |
| 5675 | if (res < 0) |
| 5676 | return RTP_ID_ERROR; |
| 5677 | return ((intptr_t)status); |
| 5678 | } |
| 5679 | return ((intptr_t)rtpid); |
| 5680 | } |
| 5681 | #endif |
| 5682 | |
| 5683 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5684 | /*[clinic input] |
| 5685 | os.spawnv |
| 5686 | |
| 5687 | mode: int |
| 5688 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5689 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5690 | Path of executable file. |
| 5691 | argv: object |
| 5692 | Tuple or list of strings. |
| 5693 | / |
| 5694 | |
| 5695 | Execute the program specified by path in a new process. |
| 5696 | [clinic start generated code]*/ |
| 5697 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5698 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5699 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5700 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5701 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5702 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5703 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5704 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5705 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5706 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5707 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5708 | /* spawnv has three arguments: (mode, path, argv), where |
| 5709 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5710 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5711 | if (PyList_Check(argv)) { |
| 5712 | argc = PyList_Size(argv); |
| 5713 | getitem = PyList_GetItem; |
| 5714 | } |
| 5715 | else if (PyTuple_Check(argv)) { |
| 5716 | argc = PyTuple_Size(argv); |
| 5717 | getitem = PyTuple_GetItem; |
| 5718 | } |
| 5719 | else { |
| 5720 | PyErr_SetString(PyExc_TypeError, |
| 5721 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5722 | return NULL; |
| 5723 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5724 | if (argc == 0) { |
| 5725 | PyErr_SetString(PyExc_ValueError, |
| 5726 | "spawnv() arg 2 cannot be empty"); |
| 5727 | return NULL; |
| 5728 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5729 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5730 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5731 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5732 | return PyErr_NoMemory(); |
| 5733 | } |
| 5734 | for (i = 0; i < argc; i++) { |
| 5735 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5736 | &argvlist[i])) { |
| 5737 | free_string_array(argvlist, i); |
| 5738 | PyErr_SetString( |
| 5739 | PyExc_TypeError, |
| 5740 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5741 | return NULL; |
| 5742 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5743 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5744 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5745 | PyErr_SetString( |
| 5746 | PyExc_ValueError, |
| 5747 | "spawnv() arg 2 first element cannot be empty"); |
| 5748 | return NULL; |
| 5749 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5750 | } |
| 5751 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5752 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5753 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5754 | if (mode == _OLD_P_OVERLAY) |
| 5755 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5756 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5757 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5758 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5759 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5760 | #ifdef HAVE_WSPAWNV |
| 5761 | spawnval = _wspawnv(mode, path->wide, argvlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5762 | #elif defined(HAVE_RTPSPAWN) |
| 5763 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5764 | #else |
| 5765 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 5766 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5767 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5768 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5769 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5770 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5771 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5772 | if (spawnval == -1) |
| 5773 | return posix_error(); |
| 5774 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5775 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5776 | } |
| 5777 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5778 | /*[clinic input] |
| 5779 | os.spawnve |
| 5780 | |
| 5781 | mode: int |
| 5782 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5783 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5784 | Path of executable file. |
| 5785 | argv: object |
| 5786 | Tuple or list of strings. |
| 5787 | env: object |
| 5788 | Dictionary of strings mapping to strings. |
| 5789 | / |
| 5790 | |
| 5791 | Execute the program specified by path in a new process. |
| 5792 | [clinic start generated code]*/ |
| 5793 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5794 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5795 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5796 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5797 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5798 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5799 | EXECV_CHAR **argvlist; |
| 5800 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5801 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5802 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5803 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5804 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5805 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5806 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5807 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5808 | argv is a list or tuple of strings and env is a dictionary |
| 5809 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5810 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5811 | if (PyList_Check(argv)) { |
| 5812 | argc = PyList_Size(argv); |
| 5813 | getitem = PyList_GetItem; |
| 5814 | } |
| 5815 | else if (PyTuple_Check(argv)) { |
| 5816 | argc = PyTuple_Size(argv); |
| 5817 | getitem = PyTuple_GetItem; |
| 5818 | } |
| 5819 | else { |
| 5820 | PyErr_SetString(PyExc_TypeError, |
| 5821 | "spawnve() arg 2 must be a tuple or list"); |
| 5822 | goto fail_0; |
| 5823 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5824 | if (argc == 0) { |
| 5825 | PyErr_SetString(PyExc_ValueError, |
| 5826 | "spawnve() arg 2 cannot be empty"); |
| 5827 | goto fail_0; |
| 5828 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5829 | if (!PyMapping_Check(env)) { |
| 5830 | PyErr_SetString(PyExc_TypeError, |
| 5831 | "spawnve() arg 3 must be a mapping object"); |
| 5832 | goto fail_0; |
| 5833 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5834 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5835 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5836 | if (argvlist == NULL) { |
| 5837 | PyErr_NoMemory(); |
| 5838 | goto fail_0; |
| 5839 | } |
| 5840 | for (i = 0; i < argc; i++) { |
| 5841 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5842 | &argvlist[i])) |
| 5843 | { |
| 5844 | lastarg = i; |
| 5845 | goto fail_1; |
| 5846 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5847 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5848 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5849 | PyErr_SetString( |
| 5850 | PyExc_ValueError, |
| 5851 | "spawnv() arg 2 first element cannot be empty"); |
| 5852 | goto fail_1; |
| 5853 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5854 | } |
| 5855 | lastarg = argc; |
| 5856 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5857 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5858 | envlist = parse_envlist(env, &envc); |
| 5859 | if (envlist == NULL) |
| 5860 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5861 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5862 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5863 | if (mode == _OLD_P_OVERLAY) |
| 5864 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5865 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5866 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5867 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5868 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5869 | #ifdef HAVE_WSPAWNV |
| 5870 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5871 | #elif defined(HAVE_RTPSPAWN) |
| 5872 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, |
| 5873 | (const char **)envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5874 | #else |
| 5875 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 5876 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5877 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5878 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5879 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5880 | if (spawnval == -1) |
| 5881 | (void) posix_error(); |
| 5882 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5883 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5884 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5885 | while (--envc >= 0) |
| 5886 | PyMem_DEL(envlist[envc]); |
| 5887 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5888 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5889 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5890 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5891 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5892 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5893 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5894 | #endif /* HAVE_SPAWNV */ |
| 5895 | |
| 5896 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5897 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5898 | |
| 5899 | /* Helper function to validate arguments. |
| 5900 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 5901 | If obj is non-NULL it must be callable. */ |
| 5902 | static int |
| 5903 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 5904 | { |
| 5905 | if (obj && !PyCallable_Check(obj)) { |
| 5906 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
| 5907 | obj_name, Py_TYPE(obj)->tp_name); |
| 5908 | return -1; |
| 5909 | } |
| 5910 | return 0; |
| 5911 | } |
| 5912 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5913 | /*[clinic input] |
| 5914 | os.register_at_fork |
| 5915 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5916 | * |
| 5917 | before: object=NULL |
| 5918 | A callable to be called in the parent before the fork() syscall. |
| 5919 | after_in_child: object=NULL |
| 5920 | A callable to be called in the child after fork(). |
| 5921 | after_in_parent: object=NULL |
| 5922 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5923 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5924 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5925 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5926 | 'before' callbacks are called in reverse order. |
| 5927 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5928 | |
| 5929 | [clinic start generated code]*/ |
| 5930 | |
| 5931 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5932 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 5933 | PyObject *after_in_child, PyObject *after_in_parent) |
| 5934 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5935 | { |
| 5936 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5937 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5938 | if (!before && !after_in_child && !after_in_parent) { |
| 5939 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 5940 | return NULL; |
| 5941 | } |
| 5942 | if (check_null_or_callable(before, "before") || |
| 5943 | check_null_or_callable(after_in_child, "after_in_child") || |
| 5944 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5945 | return NULL; |
| 5946 | } |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 5947 | interp = _PyInterpreterState_Get(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5948 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5949 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5950 | return NULL; |
| 5951 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5952 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5953 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5954 | } |
| 5955 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 5956 | return NULL; |
| 5957 | } |
| 5958 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5959 | } |
| 5960 | #endif /* HAVE_FORK */ |
| 5961 | |
| 5962 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5963 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5964 | /*[clinic input] |
| 5965 | os.fork1 |
| 5966 | |
| 5967 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 5968 | |
| 5969 | Return 0 to child process and PID of child to parent process. |
| 5970 | [clinic start generated code]*/ |
| 5971 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5972 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5973 | os_fork1_impl(PyObject *module) |
| 5974 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5975 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5976 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5977 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 5978 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 5979 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 5980 | return NULL; |
| 5981 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5982 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5983 | pid = fork1(); |
| 5984 | if (pid == 0) { |
| 5985 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5986 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5987 | } else { |
| 5988 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5989 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5990 | } |
| 5991 | if (pid == -1) |
| 5992 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5993 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5994 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5995 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5996 | |
| 5997 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5998 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5999 | /*[clinic input] |
| 6000 | os.fork |
| 6001 | |
| 6002 | Fork a child process. |
| 6003 | |
| 6004 | Return 0 to child process and PID of child to parent process. |
| 6005 | [clinic start generated code]*/ |
| 6006 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6007 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6008 | os_fork_impl(PyObject *module) |
| 6009 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6010 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6011 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6012 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6013 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6014 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6015 | return NULL; |
| 6016 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6017 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6018 | pid = fork(); |
| 6019 | if (pid == 0) { |
| 6020 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6021 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6022 | } else { |
| 6023 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6024 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6025 | } |
| 6026 | if (pid == -1) |
| 6027 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6028 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6029 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6030 | #endif /* HAVE_FORK */ |
| 6031 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6032 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6033 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6034 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6035 | /*[clinic input] |
| 6036 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6037 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6038 | policy: int |
| 6039 | |
| 6040 | Get the maximum scheduling priority for policy. |
| 6041 | [clinic start generated code]*/ |
| 6042 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6043 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6044 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 6045 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6046 | { |
| 6047 | int max; |
| 6048 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6049 | max = sched_get_priority_max(policy); |
| 6050 | if (max < 0) |
| 6051 | return posix_error(); |
| 6052 | return PyLong_FromLong(max); |
| 6053 | } |
| 6054 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6055 | |
| 6056 | /*[clinic input] |
| 6057 | os.sched_get_priority_min |
| 6058 | |
| 6059 | policy: int |
| 6060 | |
| 6061 | Get the minimum scheduling priority for policy. |
| 6062 | [clinic start generated code]*/ |
| 6063 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6064 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6065 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 6066 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6067 | { |
| 6068 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6069 | if (min < 0) |
| 6070 | return posix_error(); |
| 6071 | return PyLong_FromLong(min); |
| 6072 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6073 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 6074 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6075 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6076 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 6077 | /*[clinic input] |
| 6078 | os.sched_getscheduler |
| 6079 | pid: pid_t |
| 6080 | / |
| 6081 | |
| 6082 | Get the scheduling policy for the process identifiedy by pid. |
| 6083 | |
| 6084 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 6085 | [clinic start generated code]*/ |
| 6086 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6087 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6088 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
| 6089 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6090 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6091 | int policy; |
| 6092 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6093 | policy = sched_getscheduler(pid); |
| 6094 | if (policy < 0) |
| 6095 | return posix_error(); |
| 6096 | return PyLong_FromLong(policy); |
| 6097 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6098 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6099 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6100 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6101 | #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] | 6102 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6103 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6104 | |
| 6105 | @classmethod |
| 6106 | os.sched_param.__new__ |
| 6107 | |
| 6108 | sched_priority: object |
| 6109 | A scheduling parameter. |
| 6110 | |
| 6111 | Current has only one field: sched_priority"); |
| 6112 | [clinic start generated code]*/ |
| 6113 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6114 | static PyObject * |
| 6115 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6116 | /*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6117 | { |
| 6118 | PyObject *res; |
| 6119 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6120 | res = PyStructSequence_New(type); |
| 6121 | if (!res) |
| 6122 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6123 | Py_INCREF(sched_priority); |
| 6124 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6125 | return res; |
| 6126 | } |
| 6127 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6128 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6129 | PyDoc_VAR(os_sched_param__doc__); |
| 6130 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6131 | static PyStructSequence_Field sched_param_fields[] = { |
| 6132 | {"sched_priority", "the scheduling priority"}, |
| 6133 | {0} |
| 6134 | }; |
| 6135 | |
| 6136 | static PyStructSequence_Desc sched_param_desc = { |
| 6137 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6138 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6139 | sched_param_fields, |
| 6140 | 1 |
| 6141 | }; |
| 6142 | |
| 6143 | static int |
| 6144 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 6145 | { |
| 6146 | long priority; |
| 6147 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6148 | if (Py_TYPE(param) != SchedParamType) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6149 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 6150 | return 0; |
| 6151 | } |
| 6152 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 6153 | if (priority == -1 && PyErr_Occurred()) |
| 6154 | return 0; |
| 6155 | if (priority > INT_MAX || priority < INT_MIN) { |
| 6156 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 6157 | return 0; |
| 6158 | } |
| 6159 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 6160 | return 1; |
| 6161 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6162 | #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] | 6163 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6164 | |
| 6165 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6166 | /*[clinic input] |
| 6167 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6168 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6169 | pid: pid_t |
| 6170 | policy: int |
| 6171 | param: sched_param |
| 6172 | / |
| 6173 | |
| 6174 | Set the scheduling policy for the process identified by pid. |
| 6175 | |
| 6176 | If pid is 0, the calling process is changed. |
| 6177 | param is an instance of sched_param. |
| 6178 | [clinic start generated code]*/ |
| 6179 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6180 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6181 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6182 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6183 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6184 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6185 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6186 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6187 | ** scheduling policy under Solaris/Illumos, and others. |
| 6188 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6189 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6190 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6191 | return posix_error(); |
| 6192 | Py_RETURN_NONE; |
| 6193 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6194 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6195 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6196 | |
| 6197 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6198 | /*[clinic input] |
| 6199 | os.sched_getparam |
| 6200 | pid: pid_t |
| 6201 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6202 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6203 | Returns scheduling parameters for the process identified by pid. |
| 6204 | |
| 6205 | If pid is 0, returns parameters for the calling process. |
| 6206 | Return value is an instance of sched_param. |
| 6207 | [clinic start generated code]*/ |
| 6208 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6209 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6210 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6211 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6212 | { |
| 6213 | struct sched_param param; |
| 6214 | PyObject *result; |
| 6215 | PyObject *priority; |
| 6216 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6217 | if (sched_getparam(pid, ¶m)) |
| 6218 | return posix_error(); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6219 | result = PyStructSequence_New(SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6220 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6221 | return NULL; |
| 6222 | priority = PyLong_FromLong(param.sched_priority); |
| 6223 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6224 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6225 | return NULL; |
| 6226 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6227 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6228 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6229 | } |
| 6230 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6231 | |
| 6232 | /*[clinic input] |
| 6233 | os.sched_setparam |
| 6234 | pid: pid_t |
| 6235 | param: sched_param |
| 6236 | / |
| 6237 | |
| 6238 | Set scheduling parameters for the process identified by pid. |
| 6239 | |
| 6240 | If pid is 0, sets parameters for the calling process. |
| 6241 | param should be an instance of sched_param. |
| 6242 | [clinic start generated code]*/ |
| 6243 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6244 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6245 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6246 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6247 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6248 | { |
| 6249 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6250 | return posix_error(); |
| 6251 | Py_RETURN_NONE; |
| 6252 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6253 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6254 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6255 | |
| 6256 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6257 | /*[clinic input] |
| 6258 | os.sched_rr_get_interval -> double |
| 6259 | pid: pid_t |
| 6260 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6261 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6262 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6263 | |
| 6264 | Value returned is a float. |
| 6265 | [clinic start generated code]*/ |
| 6266 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6267 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6268 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6269 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6270 | { |
| 6271 | struct timespec interval; |
| 6272 | if (sched_rr_get_interval(pid, &interval)) { |
| 6273 | posix_error(); |
| 6274 | return -1.0; |
| 6275 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6276 | #ifdef _Py_MEMORY_SANITIZER |
| 6277 | __msan_unpoison(&interval, sizeof(interval)); |
| 6278 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6279 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6280 | } |
| 6281 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6283 | |
| 6284 | /*[clinic input] |
| 6285 | os.sched_yield |
| 6286 | |
| 6287 | Voluntarily relinquish the CPU. |
| 6288 | [clinic start generated code]*/ |
| 6289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6290 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6291 | os_sched_yield_impl(PyObject *module) |
| 6292 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6293 | { |
| 6294 | if (sched_yield()) |
| 6295 | return posix_error(); |
| 6296 | Py_RETURN_NONE; |
| 6297 | } |
| 6298 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6299 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6300 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6301 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6302 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6303 | /*[clinic input] |
| 6304 | os.sched_setaffinity |
| 6305 | pid: pid_t |
| 6306 | mask : object |
| 6307 | / |
| 6308 | |
| 6309 | Set the CPU affinity of the process identified by pid to mask. |
| 6310 | |
| 6311 | mask should be an iterable of integers identifying CPUs. |
| 6312 | [clinic start generated code]*/ |
| 6313 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6314 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6315 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6316 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6317 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6318 | int ncpus; |
| 6319 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6320 | cpu_set_t *cpu_set = NULL; |
| 6321 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6322 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6323 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6324 | if (iterator == NULL) |
| 6325 | return NULL; |
| 6326 | |
| 6327 | ncpus = NCPUS_START; |
| 6328 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6329 | cpu_set = CPU_ALLOC(ncpus); |
| 6330 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6331 | PyErr_NoMemory(); |
| 6332 | goto error; |
| 6333 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6334 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6335 | |
| 6336 | while ((item = PyIter_Next(iterator))) { |
| 6337 | long cpu; |
| 6338 | if (!PyLong_Check(item)) { |
| 6339 | PyErr_Format(PyExc_TypeError, |
| 6340 | "expected an iterator of ints, " |
| 6341 | "but iterator yielded %R", |
| 6342 | Py_TYPE(item)); |
| 6343 | Py_DECREF(item); |
| 6344 | goto error; |
| 6345 | } |
| 6346 | cpu = PyLong_AsLong(item); |
| 6347 | Py_DECREF(item); |
| 6348 | if (cpu < 0) { |
| 6349 | if (!PyErr_Occurred()) |
| 6350 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6351 | goto error; |
| 6352 | } |
| 6353 | if (cpu > INT_MAX - 1) { |
| 6354 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6355 | goto error; |
| 6356 | } |
| 6357 | if (cpu >= ncpus) { |
| 6358 | /* Grow CPU mask to fit the CPU number */ |
| 6359 | int newncpus = ncpus; |
| 6360 | cpu_set_t *newmask; |
| 6361 | size_t newsetsize; |
| 6362 | while (newncpus <= cpu) { |
| 6363 | if (newncpus > INT_MAX / 2) |
| 6364 | newncpus = cpu + 1; |
| 6365 | else |
| 6366 | newncpus = newncpus * 2; |
| 6367 | } |
| 6368 | newmask = CPU_ALLOC(newncpus); |
| 6369 | if (newmask == NULL) { |
| 6370 | PyErr_NoMemory(); |
| 6371 | goto error; |
| 6372 | } |
| 6373 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 6374 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6375 | memcpy(newmask, cpu_set, setsize); |
| 6376 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6377 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6378 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6379 | ncpus = newncpus; |
| 6380 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6381 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6382 | } |
| 6383 | Py_CLEAR(iterator); |
| 6384 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6385 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6386 | posix_error(); |
| 6387 | goto error; |
| 6388 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6389 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6390 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6391 | |
| 6392 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6393 | if (cpu_set) |
| 6394 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6395 | Py_XDECREF(iterator); |
| 6396 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6397 | } |
| 6398 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6399 | |
| 6400 | /*[clinic input] |
| 6401 | os.sched_getaffinity |
| 6402 | pid: pid_t |
| 6403 | / |
| 6404 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6405 | 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] | 6406 | |
| 6407 | The affinity is returned as a set of CPU identifiers. |
| 6408 | [clinic start generated code]*/ |
| 6409 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6410 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6411 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6412 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6413 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6414 | int cpu, ncpus, count; |
| 6415 | size_t setsize; |
| 6416 | cpu_set_t *mask = NULL; |
| 6417 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6418 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6419 | ncpus = NCPUS_START; |
| 6420 | while (1) { |
| 6421 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6422 | mask = CPU_ALLOC(ncpus); |
| 6423 | if (mask == NULL) |
| 6424 | return PyErr_NoMemory(); |
| 6425 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6426 | break; |
| 6427 | CPU_FREE(mask); |
| 6428 | if (errno != EINVAL) |
| 6429 | return posix_error(); |
| 6430 | if (ncpus > INT_MAX / 2) { |
| 6431 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6432 | "a large enough CPU set"); |
| 6433 | return NULL; |
| 6434 | } |
| 6435 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6436 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6437 | |
| 6438 | res = PySet_New(NULL); |
| 6439 | if (res == NULL) |
| 6440 | goto error; |
| 6441 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6442 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6443 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6444 | --count; |
| 6445 | if (cpu_num == NULL) |
| 6446 | goto error; |
| 6447 | if (PySet_Add(res, cpu_num)) { |
| 6448 | Py_DECREF(cpu_num); |
| 6449 | goto error; |
| 6450 | } |
| 6451 | Py_DECREF(cpu_num); |
| 6452 | } |
| 6453 | } |
| 6454 | CPU_FREE(mask); |
| 6455 | return res; |
| 6456 | |
| 6457 | error: |
| 6458 | if (mask) |
| 6459 | CPU_FREE(mask); |
| 6460 | Py_XDECREF(res); |
| 6461 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6462 | } |
| 6463 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6464 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6465 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6466 | #endif /* HAVE_SCHED_H */ |
| 6467 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6468 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6469 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6470 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6471 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6472 | #define DEV_PTY_FILE "/dev/ptc" |
| 6473 | #define HAVE_DEV_PTMX |
| 6474 | #else |
| 6475 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6476 | #endif |
| 6477 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6478 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6479 | #ifdef HAVE_PTY_H |
| 6480 | #include <pty.h> |
| 6481 | #else |
| 6482 | #ifdef HAVE_LIBUTIL_H |
| 6483 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6484 | #else |
| 6485 | #ifdef HAVE_UTIL_H |
| 6486 | #include <util.h> |
| 6487 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6488 | #endif /* HAVE_LIBUTIL_H */ |
| 6489 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6490 | #ifdef HAVE_STROPTS_H |
| 6491 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6492 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6493 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6494 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6495 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6496 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6497 | /*[clinic input] |
| 6498 | os.openpty |
| 6499 | |
| 6500 | Open a pseudo-terminal. |
| 6501 | |
| 6502 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6503 | for both the master and slave ends. |
| 6504 | [clinic start generated code]*/ |
| 6505 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6506 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6507 | os_openpty_impl(PyObject *module) |
| 6508 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6509 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6510 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6511 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6512 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6513 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6514 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6515 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 6516 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6517 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6518 | #endif |
| 6519 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6520 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6521 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6522 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6523 | goto posix_error; |
| 6524 | |
| 6525 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6526 | goto error; |
| 6527 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6528 | goto error; |
| 6529 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6530 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6531 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6532 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6533 | goto posix_error; |
| 6534 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6535 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6536 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6537 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6538 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6539 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6540 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6541 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6542 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6543 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6544 | goto posix_error; |
| 6545 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6546 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6547 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6548 | /* change permission of slave */ |
| 6549 | if (grantpt(master_fd) < 0) { |
| 6550 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6551 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6552 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6553 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6554 | /* unlock slave */ |
| 6555 | if (unlockpt(master_fd) < 0) { |
| 6556 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6557 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6558 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6559 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6560 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6561 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6562 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6563 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6564 | goto posix_error; |
| 6565 | |
| 6566 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6567 | if (slave_fd == -1) |
| 6568 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6569 | |
| 6570 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6571 | goto posix_error; |
| 6572 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6573 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6574 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6575 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6576 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6577 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6578 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6579 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6580 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6581 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6582 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6583 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6584 | posix_error: |
| 6585 | posix_error(); |
| 6586 | error: |
| 6587 | if (master_fd != -1) |
| 6588 | close(master_fd); |
| 6589 | if (slave_fd != -1) |
| 6590 | close(slave_fd); |
| 6591 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6592 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6593 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6594 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6595 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6596 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6597 | /*[clinic input] |
| 6598 | os.forkpty |
| 6599 | |
| 6600 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6601 | |
| 6602 | Returns a tuple of (pid, master_fd). |
| 6603 | Like fork(), return pid of 0 to the child process, |
| 6604 | and pid of child to the parent process. |
| 6605 | To both, return fd of newly opened pseudo-terminal. |
| 6606 | [clinic start generated code]*/ |
| 6607 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6608 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6609 | os_forkpty_impl(PyObject *module) |
| 6610 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6611 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6612 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6613 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6614 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6615 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6616 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6617 | return NULL; |
| 6618 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6619 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6620 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6621 | if (pid == 0) { |
| 6622 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6623 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6624 | } else { |
| 6625 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6626 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6627 | } |
| 6628 | if (pid == -1) |
| 6629 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6630 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6631 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6632 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6633 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6634 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6635 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6636 | /*[clinic input] |
| 6637 | os.getegid |
| 6638 | |
| 6639 | Return the current process's effective group id. |
| 6640 | [clinic start generated code]*/ |
| 6641 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6642 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6643 | os_getegid_impl(PyObject *module) |
| 6644 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6645 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6646 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6647 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6648 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6649 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6650 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6651 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6652 | /*[clinic input] |
| 6653 | os.geteuid |
| 6654 | |
| 6655 | Return the current process's effective user id. |
| 6656 | [clinic start generated code]*/ |
| 6657 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6658 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6659 | os_geteuid_impl(PyObject *module) |
| 6660 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6661 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6662 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6663 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6664 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6665 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6666 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6667 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6668 | /*[clinic input] |
| 6669 | os.getgid |
| 6670 | |
| 6671 | Return the current process's group id. |
| 6672 | [clinic start generated code]*/ |
| 6673 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6674 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6675 | os_getgid_impl(PyObject *module) |
| 6676 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6677 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6678 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6679 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6680 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6681 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6682 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6683 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6684 | /*[clinic input] |
| 6685 | os.getpid |
| 6686 | |
| 6687 | Return the current process id. |
| 6688 | [clinic start generated code]*/ |
| 6689 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6690 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6691 | os_getpid_impl(PyObject *module) |
| 6692 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6693 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6694 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6695 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6696 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6697 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6698 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6699 | |
| 6700 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6701 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6702 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6703 | Returns a list of groups to which a user belongs.\n\n\ |
| 6704 | user: username to lookup\n\ |
| 6705 | group: base group id of the user"); |
| 6706 | |
| 6707 | static PyObject * |
| 6708 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6709 | { |
| 6710 | #ifdef NGROUPS_MAX |
| 6711 | #define MAX_GROUPS NGROUPS_MAX |
| 6712 | #else |
| 6713 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6714 | #define MAX_GROUPS 64 |
| 6715 | #endif |
| 6716 | |
| 6717 | const char *user; |
| 6718 | int i, ngroups; |
| 6719 | PyObject *list; |
| 6720 | #ifdef __APPLE__ |
| 6721 | int *groups, basegid; |
| 6722 | #else |
| 6723 | gid_t *groups, basegid; |
| 6724 | #endif |
| 6725 | ngroups = MAX_GROUPS; |
| 6726 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6727 | #ifdef __APPLE__ |
| 6728 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6729 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6730 | #else |
| 6731 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6732 | _Py_Gid_Converter, &basegid)) |
| 6733 | return NULL; |
| 6734 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6735 | |
| 6736 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6737 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6738 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6739 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6740 | #endif |
| 6741 | if (groups == NULL) |
| 6742 | return PyErr_NoMemory(); |
| 6743 | |
| 6744 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6745 | PyMem_Del(groups); |
| 6746 | return posix_error(); |
| 6747 | } |
| 6748 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6749 | #ifdef _Py_MEMORY_SANITIZER |
| 6750 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
| 6751 | __msan_unpoison(&ngroups, sizeof(ngroups)); |
| 6752 | __msan_unpoison(groups, ngroups*sizeof(*groups)); |
| 6753 | #endif |
| 6754 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6755 | list = PyList_New(ngroups); |
| 6756 | if (list == NULL) { |
| 6757 | PyMem_Del(groups); |
| 6758 | return NULL; |
| 6759 | } |
| 6760 | |
| 6761 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6762 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6763 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6764 | #else |
| 6765 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6766 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6767 | if (o == NULL) { |
| 6768 | Py_DECREF(list); |
| 6769 | PyMem_Del(groups); |
| 6770 | return NULL; |
| 6771 | } |
| 6772 | PyList_SET_ITEM(list, i, o); |
| 6773 | } |
| 6774 | |
| 6775 | PyMem_Del(groups); |
| 6776 | |
| 6777 | return list; |
| 6778 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6779 | #endif /* HAVE_GETGROUPLIST */ |
| 6780 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6781 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6782 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6783 | /*[clinic input] |
| 6784 | os.getgroups |
| 6785 | |
| 6786 | Return list of supplemental group IDs for the process. |
| 6787 | [clinic start generated code]*/ |
| 6788 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6789 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6790 | os_getgroups_impl(PyObject *module) |
| 6791 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6792 | { |
| 6793 | PyObject *result = NULL; |
| 6794 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6795 | #ifdef NGROUPS_MAX |
| 6796 | #define MAX_GROUPS NGROUPS_MAX |
| 6797 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6798 | /* defined to be 16 on Solaris7, so this should be a small number */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6799 | #define MAX_GROUPS 64 |
| 6800 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6801 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6802 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6803 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6804 | * This is a helper variable to store the intermediate result when |
| 6805 | * that happens. |
| 6806 | * |
| 6807 | * To keep the code readable the OSX behaviour is unconditional, |
| 6808 | * according to the POSIX spec this should be safe on all unix-y |
| 6809 | * systems. |
| 6810 | */ |
| 6811 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6812 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6813 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6814 | #ifdef __APPLE__ |
| 6815 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6816 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6817 | * always first call getgroups with length 0 to get the actual number |
| 6818 | * of groups. |
| 6819 | */ |
| 6820 | n = getgroups(0, NULL); |
| 6821 | if (n < 0) { |
| 6822 | return posix_error(); |
| 6823 | } else if (n <= MAX_GROUPS) { |
| 6824 | /* groups will fit in existing array */ |
| 6825 | alt_grouplist = grouplist; |
| 6826 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6827 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6828 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 6829 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6830 | } |
| 6831 | } |
| 6832 | |
| 6833 | n = getgroups(n, alt_grouplist); |
| 6834 | if (n == -1) { |
| 6835 | if (alt_grouplist != grouplist) { |
| 6836 | PyMem_Free(alt_grouplist); |
| 6837 | } |
| 6838 | return posix_error(); |
| 6839 | } |
| 6840 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6841 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6842 | if (n < 0) { |
| 6843 | if (errno == EINVAL) { |
| 6844 | n = getgroups(0, NULL); |
| 6845 | if (n == -1) { |
| 6846 | return posix_error(); |
| 6847 | } |
| 6848 | if (n == 0) { |
| 6849 | /* Avoid malloc(0) */ |
| 6850 | alt_grouplist = grouplist; |
| 6851 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6852 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6853 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 6854 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6855 | } |
| 6856 | n = getgroups(n, alt_grouplist); |
| 6857 | if (n == -1) { |
| 6858 | PyMem_Free(alt_grouplist); |
| 6859 | return posix_error(); |
| 6860 | } |
| 6861 | } |
| 6862 | } else { |
| 6863 | return posix_error(); |
| 6864 | } |
| 6865 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6866 | #endif |
| 6867 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6868 | result = PyList_New(n); |
| 6869 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6870 | int i; |
| 6871 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6872 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6873 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6874 | Py_DECREF(result); |
| 6875 | result = NULL; |
| 6876 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6877 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6878 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6879 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6880 | } |
| 6881 | |
| 6882 | if (alt_grouplist != grouplist) { |
| 6883 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6884 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6885 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6886 | return result; |
| 6887 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6888 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6889 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6890 | #ifdef HAVE_INITGROUPS |
| 6891 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6892 | "initgroups(username, gid) -> None\n\n\ |
| 6893 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6894 | the groups of which the specified username is a member, plus the specified\n\ |
| 6895 | group id."); |
| 6896 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6897 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6898 | static PyObject * |
| 6899 | posix_initgroups(PyObject *self, PyObject *args) |
| 6900 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6901 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6902 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6903 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6904 | #ifdef __APPLE__ |
| 6905 | int gid; |
| 6906 | #else |
| 6907 | gid_t gid; |
| 6908 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6909 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6910 | #ifdef __APPLE__ |
| 6911 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6912 | PyUnicode_FSConverter, &oname, |
| 6913 | &gid)) |
| 6914 | #else |
| 6915 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6916 | PyUnicode_FSConverter, &oname, |
| 6917 | _Py_Gid_Converter, &gid)) |
| 6918 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6919 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6920 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6921 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6922 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6923 | Py_DECREF(oname); |
| 6924 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6925 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6926 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 6927 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6928 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6929 | #endif /* HAVE_INITGROUPS */ |
| 6930 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6931 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6932 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6933 | /*[clinic input] |
| 6934 | os.getpgid |
| 6935 | |
| 6936 | pid: pid_t |
| 6937 | |
| 6938 | Call the system call getpgid(), and return the result. |
| 6939 | [clinic start generated code]*/ |
| 6940 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6941 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6942 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 6943 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6944 | { |
| 6945 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6946 | if (pgid < 0) |
| 6947 | return posix_error(); |
| 6948 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6949 | } |
| 6950 | #endif /* HAVE_GETPGID */ |
| 6951 | |
| 6952 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6953 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6954 | /*[clinic input] |
| 6955 | os.getpgrp |
| 6956 | |
| 6957 | Return the current process group id. |
| 6958 | [clinic start generated code]*/ |
| 6959 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6960 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6961 | os_getpgrp_impl(PyObject *module) |
| 6962 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6963 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6964 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6965 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6966 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6967 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6968 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6969 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6970 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6971 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6972 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6973 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6974 | /*[clinic input] |
| 6975 | os.setpgrp |
| 6976 | |
| 6977 | Make the current process the leader of its process group. |
| 6978 | [clinic start generated code]*/ |
| 6979 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6980 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6981 | os_setpgrp_impl(PyObject *module) |
| 6982 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6983 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6984 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6985 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6986 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6987 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6988 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6989 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 6990 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6991 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6992 | #endif /* HAVE_SETPGRP */ |
| 6993 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6994 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6995 | |
| 6996 | #ifdef MS_WINDOWS |
| 6997 | #include <tlhelp32.h> |
| 6998 | |
| 6999 | static PyObject* |
| 7000 | win32_getppid() |
| 7001 | { |
| 7002 | HANDLE snapshot; |
| 7003 | pid_t mypid; |
| 7004 | PyObject* result = NULL; |
| 7005 | BOOL have_record; |
| 7006 | PROCESSENTRY32 pe; |
| 7007 | |
| 7008 | mypid = getpid(); /* This function never fails */ |
| 7009 | |
| 7010 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 7011 | if (snapshot == INVALID_HANDLE_VALUE) |
| 7012 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 7013 | |
| 7014 | pe.dwSize = sizeof(pe); |
| 7015 | have_record = Process32First(snapshot, &pe); |
| 7016 | while (have_record) { |
| 7017 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 7018 | /* We could cache the ulong value in a static variable. */ |
| 7019 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 7020 | break; |
| 7021 | } |
| 7022 | |
| 7023 | have_record = Process32Next(snapshot, &pe); |
| 7024 | } |
| 7025 | |
| 7026 | /* If our loop exits and our pid was not found (result will be NULL) |
| 7027 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 7028 | * error anyway, so let's raise it. */ |
| 7029 | if (!result) |
| 7030 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7031 | |
| 7032 | CloseHandle(snapshot); |
| 7033 | |
| 7034 | return result; |
| 7035 | } |
| 7036 | #endif /*MS_WINDOWS*/ |
| 7037 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7038 | |
| 7039 | /*[clinic input] |
| 7040 | os.getppid |
| 7041 | |
| 7042 | Return the parent's process id. |
| 7043 | |
| 7044 | If the parent process has already exited, Windows machines will still |
| 7045 | return its id; others systems will return the id of the 'init' process (1). |
| 7046 | [clinic start generated code]*/ |
| 7047 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7048 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7049 | os_getppid_impl(PyObject *module) |
| 7050 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7051 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7052 | #ifdef MS_WINDOWS |
| 7053 | return win32_getppid(); |
| 7054 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7055 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7056 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7057 | } |
| 7058 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7059 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7060 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7061 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7062 | /*[clinic input] |
| 7063 | os.getlogin |
| 7064 | |
| 7065 | Return the actual login name. |
| 7066 | [clinic start generated code]*/ |
| 7067 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7068 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7069 | os_getlogin_impl(PyObject *module) |
| 7070 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7071 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7072 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7073 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7074 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7075 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7076 | |
| 7077 | if (GetUserNameW(user_name, &num_chars)) { |
| 7078 | /* num_chars is the number of unicode chars plus null terminator */ |
| 7079 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7080 | } |
| 7081 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7082 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7083 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7084 | char *name; |
| 7085 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7086 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7087 | errno = 0; |
| 7088 | name = getlogin(); |
| 7089 | if (name == NULL) { |
| 7090 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7091 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7092 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7093 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7094 | } |
| 7095 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7096 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7097 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7098 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7099 | return result; |
| 7100 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7101 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7102 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7103 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7104 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7105 | /*[clinic input] |
| 7106 | os.getuid |
| 7107 | |
| 7108 | Return the current process's user id. |
| 7109 | [clinic start generated code]*/ |
| 7110 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7111 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7112 | os_getuid_impl(PyObject *module) |
| 7113 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7114 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7115 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7116 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7117 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7118 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7119 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7120 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7121 | #define HAVE_KILL |
| 7122 | #endif /* MS_WINDOWS */ |
| 7123 | |
| 7124 | #ifdef HAVE_KILL |
| 7125 | /*[clinic input] |
| 7126 | os.kill |
| 7127 | |
| 7128 | pid: pid_t |
| 7129 | signal: Py_ssize_t |
| 7130 | / |
| 7131 | |
| 7132 | Kill a process with a signal. |
| 7133 | [clinic start generated code]*/ |
| 7134 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7135 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7136 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 7137 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7138 | #ifndef MS_WINDOWS |
| 7139 | { |
| 7140 | if (kill(pid, (int)signal) == -1) |
| 7141 | return posix_error(); |
| 7142 | Py_RETURN_NONE; |
| 7143 | } |
| 7144 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7145 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7146 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7147 | DWORD sig = (DWORD)signal; |
| 7148 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7149 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7150 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7151 | /* Console processes which share a common console can be sent CTRL+C or |
| 7152 | CTRL+BREAK events, provided they handle said events. */ |
| 7153 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7154 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7155 | err = GetLastError(); |
| 7156 | PyErr_SetFromWindowsErr(err); |
| 7157 | } |
| 7158 | else |
| 7159 | Py_RETURN_NONE; |
| 7160 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7161 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7162 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 7163 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7164 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7165 | if (handle == NULL) { |
| 7166 | err = GetLastError(); |
| 7167 | return PyErr_SetFromWindowsErr(err); |
| 7168 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7169 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7170 | if (TerminateProcess(handle, sig) == 0) { |
| 7171 | err = GetLastError(); |
| 7172 | result = PyErr_SetFromWindowsErr(err); |
| 7173 | } else { |
| 7174 | Py_INCREF(Py_None); |
| 7175 | result = Py_None; |
| 7176 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7177 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7178 | CloseHandle(handle); |
| 7179 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7180 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7181 | #endif /* !MS_WINDOWS */ |
| 7182 | #endif /* HAVE_KILL */ |
| 7183 | |
| 7184 | |
| 7185 | #ifdef HAVE_KILLPG |
| 7186 | /*[clinic input] |
| 7187 | os.killpg |
| 7188 | |
| 7189 | pgid: pid_t |
| 7190 | signal: int |
| 7191 | / |
| 7192 | |
| 7193 | Kill a process group with a signal. |
| 7194 | [clinic start generated code]*/ |
| 7195 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7196 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7197 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7198 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7199 | { |
| 7200 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7201 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7202 | take the same type. Moreover, pid_t is always at least as wide as |
| 7203 | int (else compilation of this module fails), which is safe. */ |
| 7204 | if (killpg(pgid, signal) == -1) |
| 7205 | return posix_error(); |
| 7206 | Py_RETURN_NONE; |
| 7207 | } |
| 7208 | #endif /* HAVE_KILLPG */ |
| 7209 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7210 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7211 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7212 | #ifdef HAVE_SYS_LOCK_H |
| 7213 | #include <sys/lock.h> |
| 7214 | #endif |
| 7215 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7216 | /*[clinic input] |
| 7217 | os.plock |
| 7218 | op: int |
| 7219 | / |
| 7220 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7221 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7222 | [clinic start generated code]*/ |
| 7223 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7224 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7225 | os_plock_impl(PyObject *module, int op) |
| 7226 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7227 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7228 | if (plock(op) == -1) |
| 7229 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7230 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7231 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7232 | #endif /* HAVE_PLOCK */ |
| 7233 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7234 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7235 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7236 | /*[clinic input] |
| 7237 | os.setuid |
| 7238 | |
| 7239 | uid: uid_t |
| 7240 | / |
| 7241 | |
| 7242 | Set the current process's user id. |
| 7243 | [clinic start generated code]*/ |
| 7244 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7245 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7246 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7247 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7248 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7249 | if (setuid(uid) < 0) |
| 7250 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7251 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7252 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7253 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7254 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7255 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7256 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7257 | /*[clinic input] |
| 7258 | os.seteuid |
| 7259 | |
| 7260 | euid: uid_t |
| 7261 | / |
| 7262 | |
| 7263 | Set the current process's effective user id. |
| 7264 | [clinic start generated code]*/ |
| 7265 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7266 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7267 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7268 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7269 | { |
| 7270 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7271 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7272 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7273 | } |
| 7274 | #endif /* HAVE_SETEUID */ |
| 7275 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7276 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7277 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7278 | /*[clinic input] |
| 7279 | os.setegid |
| 7280 | |
| 7281 | egid: gid_t |
| 7282 | / |
| 7283 | |
| 7284 | Set the current process's effective group id. |
| 7285 | [clinic start generated code]*/ |
| 7286 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7287 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7288 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7289 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7290 | { |
| 7291 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7292 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7293 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7294 | } |
| 7295 | #endif /* HAVE_SETEGID */ |
| 7296 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7297 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7298 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7299 | /*[clinic input] |
| 7300 | os.setreuid |
| 7301 | |
| 7302 | ruid: uid_t |
| 7303 | euid: uid_t |
| 7304 | / |
| 7305 | |
| 7306 | Set the current process's real and effective user ids. |
| 7307 | [clinic start generated code]*/ |
| 7308 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7309 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7310 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 7311 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7312 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7313 | if (setreuid(ruid, euid) < 0) { |
| 7314 | return posix_error(); |
| 7315 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7316 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7317 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7318 | } |
| 7319 | #endif /* HAVE_SETREUID */ |
| 7320 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7321 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7322 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7323 | /*[clinic input] |
| 7324 | os.setregid |
| 7325 | |
| 7326 | rgid: gid_t |
| 7327 | egid: gid_t |
| 7328 | / |
| 7329 | |
| 7330 | Set the current process's real and effective group ids. |
| 7331 | [clinic start generated code]*/ |
| 7332 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7333 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7334 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 7335 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7336 | { |
| 7337 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7338 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7339 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7340 | } |
| 7341 | #endif /* HAVE_SETREGID */ |
| 7342 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7343 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7344 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7345 | /*[clinic input] |
| 7346 | os.setgid |
| 7347 | gid: gid_t |
| 7348 | / |
| 7349 | |
| 7350 | Set the current process's group id. |
| 7351 | [clinic start generated code]*/ |
| 7352 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7353 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7354 | os_setgid_impl(PyObject *module, gid_t gid) |
| 7355 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7356 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7357 | if (setgid(gid) < 0) |
| 7358 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7359 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7360 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7361 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7362 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7363 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7364 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7365 | /*[clinic input] |
| 7366 | os.setgroups |
| 7367 | |
| 7368 | groups: object |
| 7369 | / |
| 7370 | |
| 7371 | Set the groups of the current process to list. |
| 7372 | [clinic start generated code]*/ |
| 7373 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7374 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7375 | os_setgroups(PyObject *module, PyObject *groups) |
| 7376 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7377 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7378 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7379 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7380 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7381 | if (!PySequence_Check(groups)) { |
| 7382 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 7383 | return NULL; |
| 7384 | } |
| 7385 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7386 | if (len < 0) { |
| 7387 | return NULL; |
| 7388 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7389 | if (len > MAX_GROUPS) { |
| 7390 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7391 | return NULL; |
| 7392 | } |
| 7393 | for(i = 0; i < len; i++) { |
| 7394 | PyObject *elem; |
| 7395 | elem = PySequence_GetItem(groups, i); |
| 7396 | if (!elem) |
| 7397 | return NULL; |
| 7398 | if (!PyLong_Check(elem)) { |
| 7399 | PyErr_SetString(PyExc_TypeError, |
| 7400 | "groups must be integers"); |
| 7401 | Py_DECREF(elem); |
| 7402 | return NULL; |
| 7403 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7404 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7405 | Py_DECREF(elem); |
| 7406 | return NULL; |
| 7407 | } |
| 7408 | } |
| 7409 | Py_DECREF(elem); |
| 7410 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7411 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7412 | if (setgroups(len, grouplist) < 0) |
| 7413 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7414 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7415 | } |
| 7416 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7417 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7418 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7419 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7420 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7421 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7422 | PyObject *result; |
| 7423 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 7424 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7425 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7426 | if (pid == -1) |
| 7427 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7428 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7429 | if (struct_rusage == NULL) { |
| 7430 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7431 | if (m == NULL) |
| 7432 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 7433 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7434 | Py_DECREF(m); |
| 7435 | if (struct_rusage == NULL) |
| 7436 | return NULL; |
| 7437 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7438 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7439 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7440 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 7441 | if (!result) |
| 7442 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7443 | |
| 7444 | #ifndef doubletime |
| 7445 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7446 | #endif |
| 7447 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7448 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7449 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7450 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7451 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7452 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7453 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7454 | SET_INT(result, 2, ru->ru_maxrss); |
| 7455 | SET_INT(result, 3, ru->ru_ixrss); |
| 7456 | SET_INT(result, 4, ru->ru_idrss); |
| 7457 | SET_INT(result, 5, ru->ru_isrss); |
| 7458 | SET_INT(result, 6, ru->ru_minflt); |
| 7459 | SET_INT(result, 7, ru->ru_majflt); |
| 7460 | SET_INT(result, 8, ru->ru_nswap); |
| 7461 | SET_INT(result, 9, ru->ru_inblock); |
| 7462 | SET_INT(result, 10, ru->ru_oublock); |
| 7463 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7464 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7465 | SET_INT(result, 13, ru->ru_nsignals); |
| 7466 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7467 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7468 | #undef SET_INT |
| 7469 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7470 | if (PyErr_Occurred()) { |
| 7471 | Py_DECREF(result); |
| 7472 | return NULL; |
| 7473 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7474 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7475 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7476 | } |
| 7477 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7478 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7479 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7480 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7481 | /*[clinic input] |
| 7482 | os.wait3 |
| 7483 | |
| 7484 | options: int |
| 7485 | Wait for completion of a child process. |
| 7486 | |
| 7487 | Returns a tuple of information about the child process: |
| 7488 | (pid, status, rusage) |
| 7489 | [clinic start generated code]*/ |
| 7490 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7491 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7492 | os_wait3_impl(PyObject *module, int options) |
| 7493 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7494 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7495 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7496 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7497 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7498 | WAIT_TYPE status; |
| 7499 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7500 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7501 | do { |
| 7502 | Py_BEGIN_ALLOW_THREADS |
| 7503 | pid = wait3(&status, options, &ru); |
| 7504 | Py_END_ALLOW_THREADS |
| 7505 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7506 | if (pid < 0) |
| 7507 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7508 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7509 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7510 | } |
| 7511 | #endif /* HAVE_WAIT3 */ |
| 7512 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7513 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7514 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7515 | /*[clinic input] |
| 7516 | |
| 7517 | os.wait4 |
| 7518 | |
| 7519 | pid: pid_t |
| 7520 | options: int |
| 7521 | |
| 7522 | Wait for completion of a specific child process. |
| 7523 | |
| 7524 | Returns a tuple of information about the child process: |
| 7525 | (pid, status, rusage) |
| 7526 | [clinic start generated code]*/ |
| 7527 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7528 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7529 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7530 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7531 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7532 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7533 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7534 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7535 | WAIT_TYPE status; |
| 7536 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7537 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7538 | do { |
| 7539 | Py_BEGIN_ALLOW_THREADS |
| 7540 | res = wait4(pid, &status, options, &ru); |
| 7541 | Py_END_ALLOW_THREADS |
| 7542 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7543 | if (res < 0) |
| 7544 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7545 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7546 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7547 | } |
| 7548 | #endif /* HAVE_WAIT4 */ |
| 7549 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7550 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7551 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7552 | /*[clinic input] |
| 7553 | os.waitid |
| 7554 | |
| 7555 | idtype: idtype_t |
| 7556 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7557 | id: id_t |
| 7558 | The id to wait on. |
| 7559 | options: int |
| 7560 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7561 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7562 | / |
| 7563 | |
| 7564 | Returns the result of waiting for a process or processes. |
| 7565 | |
| 7566 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7567 | no children in a waitable state. |
| 7568 | [clinic start generated code]*/ |
| 7569 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7570 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7571 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7572 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7573 | { |
| 7574 | PyObject *result; |
| 7575 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7576 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7577 | siginfo_t si; |
| 7578 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7579 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7580 | do { |
| 7581 | Py_BEGIN_ALLOW_THREADS |
| 7582 | res = waitid(idtype, id, &si, options); |
| 7583 | Py_END_ALLOW_THREADS |
| 7584 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7585 | if (res < 0) |
| 7586 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7587 | |
| 7588 | if (si.si_pid == 0) |
| 7589 | Py_RETURN_NONE; |
| 7590 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 7591 | result = PyStructSequence_New(WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7592 | if (!result) |
| 7593 | return NULL; |
| 7594 | |
| 7595 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7596 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7597 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7598 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7599 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7600 | if (PyErr_Occurred()) { |
| 7601 | Py_DECREF(result); |
| 7602 | return NULL; |
| 7603 | } |
| 7604 | |
| 7605 | return result; |
| 7606 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7607 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7608 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7609 | |
| 7610 | #if defined(HAVE_WAITPID) |
| 7611 | /*[clinic input] |
| 7612 | os.waitpid |
| 7613 | pid: pid_t |
| 7614 | options: int |
| 7615 | / |
| 7616 | |
| 7617 | Wait for completion of a given child process. |
| 7618 | |
| 7619 | Returns a tuple of information regarding the child process: |
| 7620 | (pid, status) |
| 7621 | |
| 7622 | The options argument is ignored on Windows. |
| 7623 | [clinic start generated code]*/ |
| 7624 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7625 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7626 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7627 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7628 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7629 | pid_t res; |
| 7630 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7631 | WAIT_TYPE status; |
| 7632 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7633 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7634 | do { |
| 7635 | Py_BEGIN_ALLOW_THREADS |
| 7636 | res = waitpid(pid, &status, options); |
| 7637 | Py_END_ALLOW_THREADS |
| 7638 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7639 | if (res < 0) |
| 7640 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7641 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7642 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7643 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7644 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7645 | /* 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] | 7646 | /*[clinic input] |
| 7647 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7648 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7649 | options: int |
| 7650 | / |
| 7651 | |
| 7652 | Wait for completion of a given process. |
| 7653 | |
| 7654 | Returns a tuple of information regarding the process: |
| 7655 | (pid, status << 8) |
| 7656 | |
| 7657 | The options argument is ignored on Windows. |
| 7658 | [clinic start generated code]*/ |
| 7659 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7660 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7661 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7662 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7663 | { |
| 7664 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7665 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7666 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7667 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7668 | do { |
| 7669 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7670 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7671 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7672 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7673 | Py_END_ALLOW_THREADS |
| 7674 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7675 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7676 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7677 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7678 | /* 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] | 7679 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7680 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7681 | #endif |
| 7682 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7683 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7684 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7685 | /*[clinic input] |
| 7686 | os.wait |
| 7687 | |
| 7688 | Wait for completion of a child process. |
| 7689 | |
| 7690 | Returns a tuple of information about the child process: |
| 7691 | (pid, status) |
| 7692 | [clinic start generated code]*/ |
| 7693 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7694 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7695 | os_wait_impl(PyObject *module) |
| 7696 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7697 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7698 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7699 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7700 | WAIT_TYPE status; |
| 7701 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7702 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7703 | do { |
| 7704 | Py_BEGIN_ALLOW_THREADS |
| 7705 | pid = wait(&status); |
| 7706 | Py_END_ALLOW_THREADS |
| 7707 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7708 | if (pid < 0) |
| 7709 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7710 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7711 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7712 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7713 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7714 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7715 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7716 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7717 | /*[clinic input] |
| 7718 | os.readlink |
| 7719 | |
| 7720 | path: path_t |
| 7721 | * |
| 7722 | dir_fd: dir_fd(requires='readlinkat') = None |
| 7723 | |
| 7724 | Return a string representing the path to which the symbolic link points. |
| 7725 | |
| 7726 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7727 | and path should be relative; path will then be relative to that directory. |
| 7728 | |
| 7729 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 7730 | using it will raise a NotImplementedError. |
| 7731 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7732 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7733 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7734 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 7735 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7736 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7737 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 7738 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7739 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7740 | |
| 7741 | Py_BEGIN_ALLOW_THREADS |
| 7742 | #ifdef HAVE_READLINKAT |
| 7743 | if (dir_fd != DEFAULT_DIR_FD) |
| 7744 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 7745 | else |
| 7746 | #endif |
| 7747 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 7748 | Py_END_ALLOW_THREADS |
| 7749 | |
| 7750 | if (length < 0) { |
| 7751 | return path_error(path); |
| 7752 | } |
| 7753 | buffer[length] = '\0'; |
| 7754 | |
| 7755 | if (PyUnicode_Check(path->object)) |
| 7756 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7757 | else |
| 7758 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7759 | #elif defined(MS_WINDOWS) |
| 7760 | DWORD n_bytes_returned; |
| 7761 | DWORD io_result; |
| 7762 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7763 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7764 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
| 7765 | const wchar_t *print_name; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7766 | PyObject *result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7767 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7768 | /* First get a handle to the reparse point */ |
| 7769 | Py_BEGIN_ALLOW_THREADS |
| 7770 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7771 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7772 | 0, |
| 7773 | 0, |
| 7774 | 0, |
| 7775 | OPEN_EXISTING, |
| 7776 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7777 | 0); |
| 7778 | Py_END_ALLOW_THREADS |
| 7779 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7780 | if (reparse_point_handle == INVALID_HANDLE_VALUE) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7781 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7782 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7783 | |
| 7784 | Py_BEGIN_ALLOW_THREADS |
| 7785 | /* New call DeviceIoControl to read the reparse point */ |
| 7786 | io_result = DeviceIoControl( |
| 7787 | reparse_point_handle, |
| 7788 | FSCTL_GET_REPARSE_POINT, |
| 7789 | 0, 0, /* in buffer */ |
| 7790 | target_buffer, sizeof(target_buffer), |
| 7791 | &n_bytes_returned, |
| 7792 | 0 /* we're not using OVERLAPPED_IO */ |
| 7793 | ); |
| 7794 | CloseHandle(reparse_point_handle); |
| 7795 | Py_END_ALLOW_THREADS |
| 7796 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7797 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7798 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7799 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7800 | |
| 7801 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7802 | { |
| 7803 | PyErr_SetString(PyExc_ValueError, |
| 7804 | "not a symbolic link"); |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7805 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7806 | } |
SSE4 | 3c34aad | 2018-02-13 00:10:35 +0700 | [diff] [blame] | 7807 | print_name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7808 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7809 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7810 | result = PyUnicode_FromWideChar(print_name, |
| 7811 | rdb->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t)); |
| 7812 | if (path->narrow) { |
| 7813 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7814 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7815 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7816 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7817 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7818 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7819 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7820 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7821 | |
| 7822 | #if defined(MS_WINDOWS) |
| 7823 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7824 | /* Remove the last portion of the path - return 0 on success */ |
| 7825 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7826 | _dirnameW(WCHAR *path) |
| 7827 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7828 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7829 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 7830 | if (length == MAX_PATH) { |
| 7831 | return -1; |
| 7832 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7833 | |
| 7834 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7835 | for(ptr = path + length; ptr != path; ptr--) { |
| 7836 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7837 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7838 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7839 | } |
| 7840 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7841 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7842 | } |
| 7843 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7844 | /* Is this path absolute? */ |
| 7845 | static int |
| 7846 | _is_absW(const WCHAR *path) |
| 7847 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7848 | return path[0] == L'\\' || path[0] == L'/' || |
| 7849 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7850 | } |
| 7851 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7852 | /* join root and rest with a backslash - return 0 on success */ |
| 7853 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7854 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7855 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7856 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7857 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7858 | } |
| 7859 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7860 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 7861 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7862 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7863 | |
| 7864 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 7865 | return -1; |
| 7866 | } |
| 7867 | |
| 7868 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7869 | } |
| 7870 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7871 | /* Return True if the path at src relative to dest is a directory */ |
| 7872 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7873 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7874 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7875 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7876 | WCHAR dest_parent[MAX_PATH]; |
| 7877 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7878 | |
| 7879 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7880 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 7881 | _dirnameW(dest_parent)) { |
| 7882 | return 0; |
| 7883 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7884 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7885 | if (_joinW(src_resolved, dest_parent, src)) { |
| 7886 | return 0; |
| 7887 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7888 | return ( |
| 7889 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7890 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7891 | ); |
| 7892 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7893 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7894 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7895 | |
| 7896 | /*[clinic input] |
| 7897 | os.symlink |
| 7898 | src: path_t |
| 7899 | dst: path_t |
| 7900 | target_is_directory: bool = False |
| 7901 | * |
| 7902 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7903 | |
| 7904 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7905 | |
| 7906 | Create a symbolic link pointing to src named dst. |
| 7907 | |
| 7908 | target_is_directory is required on Windows if the target is to be |
| 7909 | interpreted as a directory. (On Windows, symlink requires |
| 7910 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7911 | target_is_directory is ignored on non-Windows platforms. |
| 7912 | |
| 7913 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7914 | and path should be relative; path will then be relative to that directory. |
| 7915 | dir_fd may not be implemented on your platform. |
| 7916 | If it is unavailable, using it will raise a NotImplementedError. |
| 7917 | |
| 7918 | [clinic start generated code]*/ |
| 7919 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7920 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7921 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7922 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7923 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7924 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7925 | #ifdef MS_WINDOWS |
| 7926 | DWORD result; |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 7927 | DWORD flags = 0; |
| 7928 | |
| 7929 | /* Assumed true, set to false if detected to not be available. */ |
| 7930 | static int windows_has_symlink_unprivileged_flag = TRUE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7931 | #else |
| 7932 | int result; |
| 7933 | #endif |
| 7934 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7935 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7936 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 7937 | if (windows_has_symlink_unprivileged_flag) { |
| 7938 | /* Allow non-admin symlinks if system allows it. */ |
| 7939 | flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
| 7940 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7941 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7942 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7943 | _Py_BEGIN_SUPPRESS_IPH |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 7944 | /* if src is a directory, ensure flags==1 (target_is_directory bit) */ |
| 7945 | if (target_is_directory || _check_dirW(src->wide, dst->wide)) { |
| 7946 | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 7947 | } |
| 7948 | |
| 7949 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7950 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7951 | Py_END_ALLOW_THREADS |
| 7952 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 7953 | if (windows_has_symlink_unprivileged_flag && !result && |
| 7954 | ERROR_INVALID_PARAMETER == GetLastError()) { |
| 7955 | |
| 7956 | Py_BEGIN_ALLOW_THREADS |
| 7957 | _Py_BEGIN_SUPPRESS_IPH |
| 7958 | /* This error might be caused by |
| 7959 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported. |
| 7960 | Try again, and update windows_has_symlink_unprivileged_flag if we |
| 7961 | are successful this time. |
| 7962 | |
| 7963 | NOTE: There is a risk of a race condition here if there are other |
| 7964 | conditions than the flag causing ERROR_INVALID_PARAMETER, and |
| 7965 | another process (or thread) changes that condition in between our |
| 7966 | calls to CreateSymbolicLink. |
| 7967 | */ |
| 7968 | flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 7969 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
| 7970 | _Py_END_SUPPRESS_IPH |
| 7971 | Py_END_ALLOW_THREADS |
| 7972 | |
| 7973 | if (result || ERROR_INVALID_PARAMETER != GetLastError()) { |
| 7974 | windows_has_symlink_unprivileged_flag = FALSE; |
| 7975 | } |
| 7976 | } |
| 7977 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7978 | if (!result) |
| 7979 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7980 | |
| 7981 | #else |
| 7982 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7983 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 7984 | PyErr_SetString(PyExc_ValueError, |
| 7985 | "symlink: src and dst must be the same type"); |
| 7986 | return NULL; |
| 7987 | } |
| 7988 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7989 | Py_BEGIN_ALLOW_THREADS |
| 7990 | #if HAVE_SYMLINKAT |
| 7991 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7992 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7993 | else |
| 7994 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7995 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7996 | Py_END_ALLOW_THREADS |
| 7997 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7998 | if (result) |
| 7999 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8000 | #endif |
| 8001 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8002 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8003 | } |
| 8004 | #endif /* HAVE_SYMLINK */ |
| 8005 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8006 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8007 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8008 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8009 | static PyStructSequence_Field times_result_fields[] = { |
| 8010 | {"user", "user time"}, |
| 8011 | {"system", "system time"}, |
| 8012 | {"children_user", "user time of children"}, |
| 8013 | {"children_system", "system time of children"}, |
| 8014 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 8015 | {NULL} |
| 8016 | }; |
| 8017 | |
| 8018 | PyDoc_STRVAR(times_result__doc__, |
| 8019 | "times_result: Result from os.times().\n\n\ |
| 8020 | This object may be accessed either as a tuple of\n\ |
| 8021 | (user, system, children_user, children_system, elapsed),\n\ |
| 8022 | or via the attributes user, system, children_user, children_system,\n\ |
| 8023 | and elapsed.\n\ |
| 8024 | \n\ |
| 8025 | See os.times for more information."); |
| 8026 | |
| 8027 | static PyStructSequence_Desc times_result_desc = { |
| 8028 | "times_result", /* name */ |
| 8029 | times_result__doc__, /* doc */ |
| 8030 | times_result_fields, |
| 8031 | 5 |
| 8032 | }; |
| 8033 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 8034 | static PyTypeObject* TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8035 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8036 | #ifdef MS_WINDOWS |
| 8037 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 8038 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8039 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8040 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8041 | |
| 8042 | static PyObject * |
| 8043 | build_times_result(double user, double system, |
| 8044 | double children_user, double children_system, |
| 8045 | double elapsed) |
| 8046 | { |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 8047 | PyObject *value = PyStructSequence_New(TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8048 | if (value == NULL) |
| 8049 | return NULL; |
| 8050 | |
| 8051 | #define SET(i, field) \ |
| 8052 | { \ |
| 8053 | PyObject *o = PyFloat_FromDouble(field); \ |
| 8054 | if (!o) { \ |
| 8055 | Py_DECREF(value); \ |
| 8056 | return NULL; \ |
| 8057 | } \ |
| 8058 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 8059 | } \ |
| 8060 | |
| 8061 | SET(0, user); |
| 8062 | SET(1, system); |
| 8063 | SET(2, children_user); |
| 8064 | SET(3, children_system); |
| 8065 | SET(4, elapsed); |
| 8066 | |
| 8067 | #undef SET |
| 8068 | |
| 8069 | return value; |
| 8070 | } |
| 8071 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8072 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8073 | #ifndef MS_WINDOWS |
| 8074 | #define NEED_TICKS_PER_SECOND |
| 8075 | static long ticks_per_second = -1; |
| 8076 | #endif /* MS_WINDOWS */ |
| 8077 | |
| 8078 | /*[clinic input] |
| 8079 | os.times |
| 8080 | |
| 8081 | Return a collection containing process timing information. |
| 8082 | |
| 8083 | The object returned behaves like a named tuple with these fields: |
| 8084 | (utime, stime, cutime, cstime, elapsed_time) |
| 8085 | All fields are floating point numbers. |
| 8086 | [clinic start generated code]*/ |
| 8087 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8088 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8089 | os_times_impl(PyObject *module) |
| 8090 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8091 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8092 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8093 | FILETIME create, exit, kernel, user; |
| 8094 | HANDLE hProc; |
| 8095 | hProc = GetCurrentProcess(); |
| 8096 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 8097 | /* The fields of a FILETIME structure are the hi and lo part |
| 8098 | of a 64-bit value expressed in 100 nanosecond units. |
| 8099 | 1e7 is one second in such units; 1e-7 the inverse. |
| 8100 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 8101 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8102 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8103 | (double)(user.dwHighDateTime*429.4967296 + |
| 8104 | user.dwLowDateTime*1e-7), |
| 8105 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 8106 | kernel.dwLowDateTime*1e-7), |
| 8107 | (double)0, |
| 8108 | (double)0, |
| 8109 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8110 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8111 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8112 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8113 | |
| 8114 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8115 | struct tms t; |
| 8116 | clock_t c; |
| 8117 | errno = 0; |
| 8118 | c = times(&t); |
| 8119 | if (c == (clock_t) -1) |
| 8120 | return posix_error(); |
| 8121 | return build_times_result( |
| 8122 | (double)t.tms_utime / ticks_per_second, |
| 8123 | (double)t.tms_stime / ticks_per_second, |
| 8124 | (double)t.tms_cutime / ticks_per_second, |
| 8125 | (double)t.tms_cstime / ticks_per_second, |
| 8126 | (double)c / ticks_per_second); |
| 8127 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8128 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8129 | #endif /* HAVE_TIMES */ |
| 8130 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8131 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8132 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8133 | /*[clinic input] |
| 8134 | os.getsid |
| 8135 | |
| 8136 | pid: pid_t |
| 8137 | / |
| 8138 | |
| 8139 | Call the system call getsid(pid) and return the result. |
| 8140 | [clinic start generated code]*/ |
| 8141 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8142 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8143 | os_getsid_impl(PyObject *module, pid_t pid) |
| 8144 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8145 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8146 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8147 | sid = getsid(pid); |
| 8148 | if (sid < 0) |
| 8149 | return posix_error(); |
| 8150 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8151 | } |
| 8152 | #endif /* HAVE_GETSID */ |
| 8153 | |
| 8154 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8155 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8156 | /*[clinic input] |
| 8157 | os.setsid |
| 8158 | |
| 8159 | Call the system call setsid(). |
| 8160 | [clinic start generated code]*/ |
| 8161 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8162 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8163 | os_setsid_impl(PyObject *module) |
| 8164 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8165 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8166 | if (setsid() < 0) |
| 8167 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8168 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8169 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8170 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8171 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8172 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8173 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8174 | /*[clinic input] |
| 8175 | os.setpgid |
| 8176 | |
| 8177 | pid: pid_t |
| 8178 | pgrp: pid_t |
| 8179 | / |
| 8180 | |
| 8181 | Call the system call setpgid(pid, pgrp). |
| 8182 | [clinic start generated code]*/ |
| 8183 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8184 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8185 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 8186 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8187 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8188 | if (setpgid(pid, pgrp) < 0) |
| 8189 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8190 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8191 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8192 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8193 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8194 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8195 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8196 | /*[clinic input] |
| 8197 | os.tcgetpgrp |
| 8198 | |
| 8199 | fd: int |
| 8200 | / |
| 8201 | |
| 8202 | Return the process group associated with the terminal specified by fd. |
| 8203 | [clinic start generated code]*/ |
| 8204 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8205 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8206 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8207 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8208 | { |
| 8209 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8210 | if (pgid < 0) |
| 8211 | return posix_error(); |
| 8212 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8213 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8214 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8215 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8216 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8217 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8218 | /*[clinic input] |
| 8219 | os.tcsetpgrp |
| 8220 | |
| 8221 | fd: int |
| 8222 | pgid: pid_t |
| 8223 | / |
| 8224 | |
| 8225 | Set the process group associated with the terminal specified by fd. |
| 8226 | [clinic start generated code]*/ |
| 8227 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8228 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8229 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8230 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8231 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8232 | if (tcsetpgrp(fd, pgid) < 0) |
| 8233 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8234 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8235 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8236 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8237 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8238 | /* Functions acting on file descriptors */ |
| 8239 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8240 | #ifdef O_CLOEXEC |
| 8241 | extern int _Py_open_cloexec_works; |
| 8242 | #endif |
| 8243 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8244 | |
| 8245 | /*[clinic input] |
| 8246 | os.open -> int |
| 8247 | path: path_t |
| 8248 | flags: int |
| 8249 | mode: int = 0o777 |
| 8250 | * |
| 8251 | dir_fd: dir_fd(requires='openat') = None |
| 8252 | |
| 8253 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 8254 | |
| 8255 | Open a file for low level IO. Returns a file descriptor (integer). |
| 8256 | |
| 8257 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8258 | and path should be relative; path will then be relative to that directory. |
| 8259 | dir_fd may not be implemented on your platform. |
| 8260 | If it is unavailable, using it will raise a NotImplementedError. |
| 8261 | [clinic start generated code]*/ |
| 8262 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8263 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8264 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 8265 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8266 | { |
| 8267 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8268 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8269 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8270 | #ifdef O_CLOEXEC |
| 8271 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 8272 | #elif !defined(MS_WINDOWS) |
| 8273 | int *atomic_flag_works = NULL; |
| 8274 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8275 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8276 | #ifdef MS_WINDOWS |
| 8277 | flags |= O_NOINHERIT; |
| 8278 | #elif defined(O_CLOEXEC) |
| 8279 | flags |= O_CLOEXEC; |
| 8280 | #endif |
| 8281 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8282 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8283 | do { |
| 8284 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8285 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8286 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8287 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8288 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8289 | if (dir_fd != DEFAULT_DIR_FD) |
| 8290 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 8291 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8292 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8293 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8294 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8295 | Py_END_ALLOW_THREADS |
| 8296 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8297 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8298 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8299 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8300 | if (!async_err) |
| 8301 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8302 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8303 | } |
| 8304 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8305 | #ifndef MS_WINDOWS |
| 8306 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 8307 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8308 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8309 | } |
| 8310 | #endif |
| 8311 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8312 | return fd; |
| 8313 | } |
| 8314 | |
| 8315 | |
| 8316 | /*[clinic input] |
| 8317 | os.close |
| 8318 | |
| 8319 | fd: int |
| 8320 | |
| 8321 | Close a file descriptor. |
| 8322 | [clinic start generated code]*/ |
| 8323 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8324 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8325 | os_close_impl(PyObject *module, int fd) |
| 8326 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8327 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8328 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8329 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 8330 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 8331 | * for more details. |
| 8332 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8333 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8334 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8335 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8336 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8337 | Py_END_ALLOW_THREADS |
| 8338 | if (res < 0) |
| 8339 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8340 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8341 | } |
| 8342 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8343 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8344 | /*[clinic input] |
| 8345 | os.closerange |
| 8346 | |
| 8347 | fd_low: int |
| 8348 | fd_high: int |
| 8349 | / |
| 8350 | |
| 8351 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 8352 | [clinic start generated code]*/ |
| 8353 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8354 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8355 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 8356 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8357 | { |
| 8358 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8359 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8360 | _Py_BEGIN_SUPPRESS_IPH |
Benjamin Peterson | 207116b | 2016-09-08 11:28:06 -0700 | [diff] [blame] | 8361 | for (i = Py_MAX(fd_low, 0); i < fd_high; i++) |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 8362 | close(i); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8363 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8364 | Py_END_ALLOW_THREADS |
| 8365 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8366 | } |
| 8367 | |
| 8368 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8369 | /*[clinic input] |
| 8370 | os.dup -> int |
| 8371 | |
| 8372 | fd: int |
| 8373 | / |
| 8374 | |
| 8375 | Return a duplicate of a file descriptor. |
| 8376 | [clinic start generated code]*/ |
| 8377 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8378 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8379 | os_dup_impl(PyObject *module, int fd) |
| 8380 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8381 | { |
| 8382 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8383 | } |
| 8384 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8385 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8386 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8387 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8388 | fd: int |
| 8389 | fd2: int |
| 8390 | inheritable: bool=True |
| 8391 | |
| 8392 | Duplicate file descriptor. |
| 8393 | [clinic start generated code]*/ |
| 8394 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8395 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8396 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8397 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8398 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8399 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8400 | #if defined(HAVE_DUP3) && \ |
| 8401 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8402 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8403 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8404 | #endif |
| 8405 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8406 | if (fd < 0 || fd2 < 0) { |
| 8407 | posix_error(); |
| 8408 | return -1; |
| 8409 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8410 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8411 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8412 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8413 | * upon close(), and therefore below. |
| 8414 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8415 | #ifdef MS_WINDOWS |
| 8416 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8417 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8418 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8419 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8420 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8421 | if (res < 0) { |
| 8422 | posix_error(); |
| 8423 | return -1; |
| 8424 | } |
| 8425 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8426 | |
| 8427 | /* Character files like console cannot be make non-inheritable */ |
| 8428 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8429 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8430 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8431 | } |
| 8432 | |
| 8433 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8434 | Py_BEGIN_ALLOW_THREADS |
| 8435 | if (!inheritable) |
| 8436 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8437 | else |
| 8438 | res = dup2(fd, fd2); |
| 8439 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8440 | if (res < 0) { |
| 8441 | posix_error(); |
| 8442 | return -1; |
| 8443 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8444 | |
| 8445 | #else |
| 8446 | |
| 8447 | #ifdef HAVE_DUP3 |
| 8448 | if (!inheritable && dup3_works != 0) { |
| 8449 | Py_BEGIN_ALLOW_THREADS |
| 8450 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8451 | Py_END_ALLOW_THREADS |
| 8452 | if (res < 0) { |
| 8453 | if (dup3_works == -1) |
| 8454 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8455 | if (dup3_works) { |
| 8456 | posix_error(); |
| 8457 | return -1; |
| 8458 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8459 | } |
| 8460 | } |
| 8461 | |
| 8462 | if (inheritable || dup3_works == 0) |
| 8463 | { |
| 8464 | #endif |
| 8465 | Py_BEGIN_ALLOW_THREADS |
| 8466 | res = dup2(fd, fd2); |
| 8467 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8468 | if (res < 0) { |
| 8469 | posix_error(); |
| 8470 | return -1; |
| 8471 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8472 | |
| 8473 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8474 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8475 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8476 | } |
| 8477 | #ifdef HAVE_DUP3 |
| 8478 | } |
| 8479 | #endif |
| 8480 | |
| 8481 | #endif |
| 8482 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8483 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8484 | } |
| 8485 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8486 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8487 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8488 | /*[clinic input] |
| 8489 | os.lockf |
| 8490 | |
| 8491 | fd: int |
| 8492 | An open file descriptor. |
| 8493 | command: int |
| 8494 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8495 | length: Py_off_t |
| 8496 | The number of bytes to lock, starting at the current position. |
| 8497 | / |
| 8498 | |
| 8499 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8500 | |
| 8501 | [clinic start generated code]*/ |
| 8502 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8503 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8504 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8505 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8506 | { |
| 8507 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8508 | |
| 8509 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8510 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8511 | Py_END_ALLOW_THREADS |
| 8512 | |
| 8513 | if (res < 0) |
| 8514 | return posix_error(); |
| 8515 | |
| 8516 | Py_RETURN_NONE; |
| 8517 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8518 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8519 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8520 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8521 | /*[clinic input] |
| 8522 | os.lseek -> Py_off_t |
| 8523 | |
| 8524 | fd: int |
| 8525 | position: Py_off_t |
| 8526 | how: int |
| 8527 | / |
| 8528 | |
| 8529 | Set the position of a file descriptor. Return the new position. |
| 8530 | |
| 8531 | Return the new cursor position in number of bytes |
| 8532 | relative to the beginning of the file. |
| 8533 | [clinic start generated code]*/ |
| 8534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8535 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8536 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8537 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8538 | { |
| 8539 | Py_off_t result; |
| 8540 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8541 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8542 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8543 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8544 | case 0: how = SEEK_SET; break; |
| 8545 | case 1: how = SEEK_CUR; break; |
| 8546 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8547 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8548 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8549 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8550 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8551 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8552 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8553 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8554 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8555 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8556 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8557 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8558 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8559 | if (result < 0) |
| 8560 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8561 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8562 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8563 | } |
| 8564 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8565 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8566 | /*[clinic input] |
| 8567 | os.read |
| 8568 | fd: int |
| 8569 | length: Py_ssize_t |
| 8570 | / |
| 8571 | |
| 8572 | Read from a file descriptor. Returns a bytes object. |
| 8573 | [clinic start generated code]*/ |
| 8574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8575 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8576 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 8577 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8578 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8579 | Py_ssize_t n; |
| 8580 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8581 | |
| 8582 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8583 | errno = EINVAL; |
| 8584 | return posix_error(); |
| 8585 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8586 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 8587 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8588 | |
| 8589 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8590 | if (buffer == NULL) |
| 8591 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8592 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8593 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8594 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8595 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8596 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8597 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8598 | |
| 8599 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8600 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8601 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8602 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8603 | } |
| 8604 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8605 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8606 | || defined(__APPLE__))) \ |
| 8607 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 8608 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 8609 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8610 | 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] | 8611 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8612 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8613 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8614 | *iov = PyMem_New(struct iovec, cnt); |
| 8615 | if (*iov == NULL) { |
| 8616 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8617 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8618 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8619 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8620 | *buf = PyMem_New(Py_buffer, cnt); |
| 8621 | if (*buf == NULL) { |
| 8622 | PyMem_Del(*iov); |
| 8623 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8624 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8625 | } |
| 8626 | |
| 8627 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8628 | PyObject *item = PySequence_GetItem(seq, i); |
| 8629 | if (item == NULL) |
| 8630 | goto fail; |
| 8631 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8632 | Py_DECREF(item); |
| 8633 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8634 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8635 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8636 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8637 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8638 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8639 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8640 | |
| 8641 | fail: |
| 8642 | PyMem_Del(*iov); |
| 8643 | for (j = 0; j < i; j++) { |
| 8644 | PyBuffer_Release(&(*buf)[j]); |
| 8645 | } |
| 8646 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8647 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8648 | } |
| 8649 | |
| 8650 | static void |
| 8651 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8652 | { |
| 8653 | int i; |
| 8654 | PyMem_Del(iov); |
| 8655 | for (i = 0; i < cnt; i++) { |
| 8656 | PyBuffer_Release(&buf[i]); |
| 8657 | } |
| 8658 | PyMem_Del(buf); |
| 8659 | } |
| 8660 | #endif |
| 8661 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8662 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8663 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8664 | /*[clinic input] |
| 8665 | os.readv -> Py_ssize_t |
| 8666 | |
| 8667 | fd: int |
| 8668 | buffers: object |
| 8669 | / |
| 8670 | |
| 8671 | Read from a file descriptor fd into an iterable of buffers. |
| 8672 | |
| 8673 | The buffers should be mutable buffers accepting bytes. |
| 8674 | readv will transfer data into each buffer until it is full |
| 8675 | and then move on to the next buffer in the sequence to hold |
| 8676 | the rest of the data. |
| 8677 | |
| 8678 | readv returns the total number of bytes read, |
| 8679 | which may be less than the total capacity of all the buffers. |
| 8680 | [clinic start generated code]*/ |
| 8681 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8682 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8683 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 8684 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8685 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8686 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8687 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8688 | struct iovec *iov; |
| 8689 | Py_buffer *buf; |
| 8690 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8691 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8692 | PyErr_SetString(PyExc_TypeError, |
| 8693 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8694 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8695 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8696 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8697 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8698 | if (cnt < 0) |
| 8699 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8700 | |
| 8701 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 8702 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8703 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8704 | do { |
| 8705 | Py_BEGIN_ALLOW_THREADS |
| 8706 | n = readv(fd, iov, cnt); |
| 8707 | Py_END_ALLOW_THREADS |
| 8708 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8709 | |
| 8710 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8711 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8712 | if (!async_err) |
| 8713 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8714 | return -1; |
| 8715 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8716 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8717 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8718 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8719 | #endif /* HAVE_READV */ |
| 8720 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8721 | |
| 8722 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8723 | /*[clinic input] |
| 8724 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 8725 | os.pread |
| 8726 | |
| 8727 | fd: int |
| 8728 | length: int |
| 8729 | offset: Py_off_t |
| 8730 | / |
| 8731 | |
| 8732 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 8733 | |
| 8734 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 8735 | the beginning of the file. The file offset remains unchanged. |
| 8736 | [clinic start generated code]*/ |
| 8737 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8738 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8739 | os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset) |
| 8740 | /*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8741 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8742 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8743 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8744 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8745 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8746 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8747 | errno = EINVAL; |
| 8748 | return posix_error(); |
| 8749 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8750 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8751 | if (buffer == NULL) |
| 8752 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8753 | |
| 8754 | do { |
| 8755 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8756 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8757 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8758 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8759 | Py_END_ALLOW_THREADS |
| 8760 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8761 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8762 | if (n < 0) { |
| 8763 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8764 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8765 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8766 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8767 | _PyBytes_Resize(&buffer, n); |
| 8768 | return buffer; |
| 8769 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8770 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8771 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 8772 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 8773 | /*[clinic input] |
| 8774 | os.preadv -> Py_ssize_t |
| 8775 | |
| 8776 | fd: int |
| 8777 | buffers: object |
| 8778 | offset: Py_off_t |
| 8779 | flags: int = 0 |
| 8780 | / |
| 8781 | |
| 8782 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 8783 | |
| 8784 | Combines the functionality of readv() and pread(). As readv(), it will |
| 8785 | transfer data into each buffer until it is full and then move on to the next |
| 8786 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 8787 | specifies the file offset at which the input operation is to be performed. It |
| 8788 | will return the total number of bytes read (which can be less than the total |
| 8789 | capacity of all the objects). |
| 8790 | |
| 8791 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 8792 | |
| 8793 | - RWF_HIPRI |
| 8794 | - RWF_NOWAIT |
| 8795 | |
| 8796 | Using non-zero flags requires Linux 4.6 or newer. |
| 8797 | [clinic start generated code]*/ |
| 8798 | |
| 8799 | static Py_ssize_t |
| 8800 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 8801 | int flags) |
| 8802 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 8803 | { |
| 8804 | Py_ssize_t cnt, n; |
| 8805 | int async_err = 0; |
| 8806 | struct iovec *iov; |
| 8807 | Py_buffer *buf; |
| 8808 | |
| 8809 | if (!PySequence_Check(buffers)) { |
| 8810 | PyErr_SetString(PyExc_TypeError, |
| 8811 | "preadv2() arg 2 must be a sequence"); |
| 8812 | return -1; |
| 8813 | } |
| 8814 | |
| 8815 | cnt = PySequence_Size(buffers); |
| 8816 | if (cnt < 0) { |
| 8817 | return -1; |
| 8818 | } |
| 8819 | |
| 8820 | #ifndef HAVE_PREADV2 |
| 8821 | if(flags != 0) { |
| 8822 | argument_unavailable_error("preadv2", "flags"); |
| 8823 | return -1; |
| 8824 | } |
| 8825 | #endif |
| 8826 | |
| 8827 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 8828 | return -1; |
| 8829 | } |
| 8830 | #ifdef HAVE_PREADV2 |
| 8831 | do { |
| 8832 | Py_BEGIN_ALLOW_THREADS |
| 8833 | _Py_BEGIN_SUPPRESS_IPH |
| 8834 | n = preadv2(fd, iov, cnt, offset, flags); |
| 8835 | _Py_END_SUPPRESS_IPH |
| 8836 | Py_END_ALLOW_THREADS |
| 8837 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8838 | #else |
| 8839 | do { |
| 8840 | Py_BEGIN_ALLOW_THREADS |
| 8841 | _Py_BEGIN_SUPPRESS_IPH |
| 8842 | n = preadv(fd, iov, cnt, offset); |
| 8843 | _Py_END_SUPPRESS_IPH |
| 8844 | Py_END_ALLOW_THREADS |
| 8845 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8846 | #endif |
| 8847 | |
| 8848 | iov_cleanup(iov, buf, cnt); |
| 8849 | if (n < 0) { |
| 8850 | if (!async_err) { |
| 8851 | posix_error(); |
| 8852 | } |
| 8853 | return -1; |
| 8854 | } |
| 8855 | |
| 8856 | return n; |
| 8857 | } |
| 8858 | #endif /* HAVE_PREADV */ |
| 8859 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8860 | |
| 8861 | /*[clinic input] |
| 8862 | os.write -> Py_ssize_t |
| 8863 | |
| 8864 | fd: int |
| 8865 | data: Py_buffer |
| 8866 | / |
| 8867 | |
| 8868 | Write a bytes object to a file descriptor. |
| 8869 | [clinic start generated code]*/ |
| 8870 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8871 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8872 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 8873 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8874 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8875 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8876 | } |
| 8877 | |
| 8878 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8879 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8880 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8881 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8882 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8883 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8884 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8885 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8886 | static PyObject * |
| 8887 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8888 | { |
| 8889 | int in, out; |
| 8890 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8891 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8892 | off_t offset; |
| 8893 | |
| 8894 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8895 | #ifndef __APPLE__ |
| 8896 | Py_ssize_t len; |
| 8897 | #endif |
| 8898 | PyObject *headers = NULL, *trailers = NULL; |
| 8899 | Py_buffer *hbuf, *tbuf; |
| 8900 | off_t sbytes; |
| 8901 | struct sf_hdtr sf; |
| 8902 | int flags = 0; |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8903 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8904 | static char *keywords[] = {"out", "in", |
| 8905 | "offset", "count", |
| 8906 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8907 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8908 | sf.headers = NULL; |
| 8909 | sf.trailers = NULL; |
| 8910 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8911 | #ifdef __APPLE__ |
| 8912 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8913 | 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] | 8914 | #else |
| 8915 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8916 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8917 | #endif |
| 8918 | &headers, &trailers, &flags)) |
| 8919 | return NULL; |
| 8920 | if (headers != NULL) { |
| 8921 | if (!PySequence_Check(headers)) { |
| 8922 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8923 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8924 | return NULL; |
| 8925 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8926 | Py_ssize_t i = PySequence_Size(headers); |
| 8927 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8928 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8929 | if (i > INT_MAX) { |
| 8930 | PyErr_SetString(PyExc_OverflowError, |
| 8931 | "sendfile() header is too large"); |
| 8932 | return NULL; |
| 8933 | } |
| 8934 | if (i > 0) { |
| 8935 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8936 | if (iov_setup(&(sf.headers), &hbuf, |
| 8937 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8938 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8939 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8940 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 8941 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 8942 | # define OFF_T_MAX 0x7fffffffffffffff |
| 8943 | if (sbytes >= OFF_T_MAX - blen) { |
| 8944 | PyErr_SetString(PyExc_OverflowError, |
| 8945 | "sendfile() header is too large"); |
| 8946 | return NULL; |
| 8947 | } |
| 8948 | sbytes += blen; |
| 8949 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8950 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8951 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8952 | } |
| 8953 | } |
| 8954 | if (trailers != NULL) { |
| 8955 | if (!PySequence_Check(trailers)) { |
| 8956 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8957 | "sendfile() trailers 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(trailers); |
| 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() trailer is too large"); |
| 8966 | return NULL; |
| 8967 | } |
| 8968 | if (i > 0) { |
| 8969 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8970 | if (iov_setup(&(sf.trailers), &tbuf, |
| 8971 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8972 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8973 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8974 | } |
| 8975 | } |
| 8976 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8977 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8978 | do { |
| 8979 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8980 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8981 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8982 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8983 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8984 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8985 | Py_END_ALLOW_THREADS |
| 8986 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8987 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8988 | |
| 8989 | if (sf.headers != NULL) |
| 8990 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8991 | if (sf.trailers != NULL) |
| 8992 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8993 | |
| 8994 | if (ret < 0) { |
| 8995 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8996 | if (sbytes != 0) { |
| 8997 | // some data has been sent |
| 8998 | goto done; |
| 8999 | } |
| 9000 | else { |
| 9001 | // no data has been sent; upper application is supposed |
| 9002 | // to retry on EAGAIN or EBUSY |
| 9003 | return posix_error(); |
| 9004 | } |
| 9005 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9006 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9007 | } |
| 9008 | goto done; |
| 9009 | |
| 9010 | done: |
| 9011 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 9012 | return Py_BuildValue("l", sbytes); |
| 9013 | #else |
| 9014 | return Py_BuildValue("L", sbytes); |
| 9015 | #endif |
| 9016 | |
| 9017 | #else |
| 9018 | Py_ssize_t count; |
| 9019 | PyObject *offobj; |
| 9020 | static char *keywords[] = {"out", "in", |
| 9021 | "offset", "count", NULL}; |
| 9022 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 9023 | keywords, &out, &in, &offobj, &count)) |
| 9024 | return NULL; |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 9025 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9026 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9027 | do { |
| 9028 | Py_BEGIN_ALLOW_THREADS |
| 9029 | ret = sendfile(out, in, NULL, count); |
| 9030 | Py_END_ALLOW_THREADS |
| 9031 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9032 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9033 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9034 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9035 | } |
| 9036 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9037 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 9038 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9039 | |
| 9040 | do { |
| 9041 | Py_BEGIN_ALLOW_THREADS |
| 9042 | ret = sendfile(out, in, &offset, count); |
| 9043 | Py_END_ALLOW_THREADS |
| 9044 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9045 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9046 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9047 | return Py_BuildValue("n", ret); |
| 9048 | #endif |
| 9049 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9050 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9051 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9052 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9053 | #if defined(__APPLE__) |
| 9054 | /*[clinic input] |
| 9055 | os._fcopyfile |
| 9056 | |
| 9057 | infd: int |
| 9058 | outfd: int |
| 9059 | flags: int |
| 9060 | / |
| 9061 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9062 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9063 | [clinic start generated code]*/ |
| 9064 | |
| 9065 | static PyObject * |
| 9066 | os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags) |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9067 | /*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9068 | { |
| 9069 | int ret; |
| 9070 | |
| 9071 | Py_BEGIN_ALLOW_THREADS |
| 9072 | ret = fcopyfile(infd, outfd, NULL, flags); |
| 9073 | Py_END_ALLOW_THREADS |
| 9074 | if (ret < 0) |
| 9075 | return posix_error(); |
| 9076 | Py_RETURN_NONE; |
| 9077 | } |
| 9078 | #endif |
| 9079 | |
| 9080 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9081 | /*[clinic input] |
| 9082 | os.fstat |
| 9083 | |
| 9084 | fd : int |
| 9085 | |
| 9086 | Perform a stat system call on the given file descriptor. |
| 9087 | |
| 9088 | Like stat(), but for an open file descriptor. |
| 9089 | Equivalent to os.stat(fd). |
| 9090 | [clinic start generated code]*/ |
| 9091 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9092 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9093 | os_fstat_impl(PyObject *module, int fd) |
| 9094 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9095 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9096 | STRUCT_STAT st; |
| 9097 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9098 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9099 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9100 | do { |
| 9101 | Py_BEGIN_ALLOW_THREADS |
| 9102 | res = FSTAT(fd, &st); |
| 9103 | Py_END_ALLOW_THREADS |
| 9104 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9105 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9106 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9107 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9108 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9109 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9110 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9111 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9112 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9113 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9114 | } |
| 9115 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9116 | |
| 9117 | /*[clinic input] |
| 9118 | os.isatty -> bool |
| 9119 | fd: int |
| 9120 | / |
| 9121 | |
| 9122 | Return True if the fd is connected to a terminal. |
| 9123 | |
| 9124 | Return True if the file descriptor is an open file descriptor |
| 9125 | connected to the slave end of a terminal. |
| 9126 | [clinic start generated code]*/ |
| 9127 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9128 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9129 | os_isatty_impl(PyObject *module, int fd) |
| 9130 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9131 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9132 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9133 | _Py_BEGIN_SUPPRESS_IPH |
| 9134 | return_value = isatty(fd); |
| 9135 | _Py_END_SUPPRESS_IPH |
| 9136 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9137 | } |
| 9138 | |
| 9139 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9140 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9141 | /*[clinic input] |
| 9142 | os.pipe |
| 9143 | |
| 9144 | Create a pipe. |
| 9145 | |
| 9146 | Returns a tuple of two file descriptors: |
| 9147 | (read_fd, write_fd) |
| 9148 | [clinic start generated code]*/ |
| 9149 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9150 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9151 | os_pipe_impl(PyObject *module) |
| 9152 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9153 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9154 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9155 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9156 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9157 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9158 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9159 | #else |
| 9160 | int res; |
| 9161 | #endif |
| 9162 | |
| 9163 | #ifdef MS_WINDOWS |
| 9164 | attr.nLength = sizeof(attr); |
| 9165 | attr.lpSecurityDescriptor = NULL; |
| 9166 | attr.bInheritHandle = FALSE; |
| 9167 | |
| 9168 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9169 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9170 | ok = CreatePipe(&read, &write, &attr, 0); |
| 9171 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9172 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 9173 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9174 | if (fds[0] == -1 || fds[1] == -1) { |
| 9175 | CloseHandle(read); |
| 9176 | CloseHandle(write); |
| 9177 | ok = 0; |
| 9178 | } |
| 9179 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9180 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9181 | Py_END_ALLOW_THREADS |
| 9182 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9183 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9184 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9185 | #else |
| 9186 | |
| 9187 | #ifdef HAVE_PIPE2 |
| 9188 | Py_BEGIN_ALLOW_THREADS |
| 9189 | res = pipe2(fds, O_CLOEXEC); |
| 9190 | Py_END_ALLOW_THREADS |
| 9191 | |
| 9192 | if (res != 0 && errno == ENOSYS) |
| 9193 | { |
| 9194 | #endif |
| 9195 | Py_BEGIN_ALLOW_THREADS |
| 9196 | res = pipe(fds); |
| 9197 | Py_END_ALLOW_THREADS |
| 9198 | |
| 9199 | if (res == 0) { |
| 9200 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 9201 | close(fds[0]); |
| 9202 | close(fds[1]); |
| 9203 | return NULL; |
| 9204 | } |
| 9205 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 9206 | close(fds[0]); |
| 9207 | close(fds[1]); |
| 9208 | return NULL; |
| 9209 | } |
| 9210 | } |
| 9211 | #ifdef HAVE_PIPE2 |
| 9212 | } |
| 9213 | #endif |
| 9214 | |
| 9215 | if (res != 0) |
| 9216 | return PyErr_SetFromErrno(PyExc_OSError); |
| 9217 | #endif /* !MS_WINDOWS */ |
| 9218 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9219 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9220 | #endif /* HAVE_PIPE */ |
| 9221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9222 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9223 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9224 | /*[clinic input] |
| 9225 | os.pipe2 |
| 9226 | |
| 9227 | flags: int |
| 9228 | / |
| 9229 | |
| 9230 | Create a pipe with flags set atomically. |
| 9231 | |
| 9232 | Returns a tuple of two file descriptors: |
| 9233 | (read_fd, write_fd) |
| 9234 | |
| 9235 | flags can be constructed by ORing together one or more of these values: |
| 9236 | O_NONBLOCK, O_CLOEXEC. |
| 9237 | [clinic start generated code]*/ |
| 9238 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9239 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9240 | os_pipe2_impl(PyObject *module, int flags) |
| 9241 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9242 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9243 | int fds[2]; |
| 9244 | int res; |
| 9245 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9246 | res = pipe2(fds, flags); |
| 9247 | if (res != 0) |
| 9248 | return posix_error(); |
| 9249 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 9250 | } |
| 9251 | #endif /* HAVE_PIPE2 */ |
| 9252 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9253 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9254 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9255 | /*[clinic input] |
| 9256 | os.writev -> Py_ssize_t |
| 9257 | fd: int |
| 9258 | buffers: object |
| 9259 | / |
| 9260 | |
| 9261 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 9262 | |
| 9263 | Returns the total number of bytes written. |
| 9264 | buffers must be a sequence of bytes-like objects. |
| 9265 | [clinic start generated code]*/ |
| 9266 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9267 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9268 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 9269 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9270 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9271 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9272 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9273 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9274 | struct iovec *iov; |
| 9275 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9276 | |
| 9277 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9278 | PyErr_SetString(PyExc_TypeError, |
| 9279 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9280 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9281 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9282 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9283 | if (cnt < 0) |
| 9284 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9285 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9286 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9287 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9288 | } |
| 9289 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9290 | do { |
| 9291 | Py_BEGIN_ALLOW_THREADS |
| 9292 | result = writev(fd, iov, cnt); |
| 9293 | Py_END_ALLOW_THREADS |
| 9294 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9295 | |
| 9296 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9297 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9298 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9299 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9300 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9301 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9302 | #endif /* HAVE_WRITEV */ |
| 9303 | |
| 9304 | |
| 9305 | #ifdef HAVE_PWRITE |
| 9306 | /*[clinic input] |
| 9307 | os.pwrite -> Py_ssize_t |
| 9308 | |
| 9309 | fd: int |
| 9310 | buffer: Py_buffer |
| 9311 | offset: Py_off_t |
| 9312 | / |
| 9313 | |
| 9314 | Write bytes to a file descriptor starting at a particular offset. |
| 9315 | |
| 9316 | Write buffer to fd, starting at offset bytes from the beginning of |
| 9317 | the file. Returns the number of bytes writte. Does not change the |
| 9318 | current file offset. |
| 9319 | [clinic start generated code]*/ |
| 9320 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9321 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9322 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 9323 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9324 | { |
| 9325 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9326 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9327 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9328 | do { |
| 9329 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9330 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9331 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9332 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9333 | Py_END_ALLOW_THREADS |
| 9334 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9335 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9336 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9337 | posix_error(); |
| 9338 | return size; |
| 9339 | } |
| 9340 | #endif /* HAVE_PWRITE */ |
| 9341 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9342 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9343 | /*[clinic input] |
| 9344 | os.pwritev -> Py_ssize_t |
| 9345 | |
| 9346 | fd: int |
| 9347 | buffers: object |
| 9348 | offset: Py_off_t |
| 9349 | flags: int = 0 |
| 9350 | / |
| 9351 | |
| 9352 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 9353 | |
| 9354 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 9355 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 9356 | buffer is written before proceeding to second, and so on. The operating system may |
| 9357 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 9358 | This function writes the contents of each object to the file descriptor and returns |
| 9359 | the total number of bytes written. |
| 9360 | |
| 9361 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9362 | |
| 9363 | - RWF_DSYNC |
| 9364 | - RWF_SYNC |
| 9365 | |
| 9366 | Using non-zero flags requires Linux 4.7 or newer. |
| 9367 | [clinic start generated code]*/ |
| 9368 | |
| 9369 | static Py_ssize_t |
| 9370 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9371 | int flags) |
| 9372 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/ |
| 9373 | { |
| 9374 | Py_ssize_t cnt; |
| 9375 | Py_ssize_t result; |
| 9376 | int async_err = 0; |
| 9377 | struct iovec *iov; |
| 9378 | Py_buffer *buf; |
| 9379 | |
| 9380 | if (!PySequence_Check(buffers)) { |
| 9381 | PyErr_SetString(PyExc_TypeError, |
| 9382 | "pwritev() arg 2 must be a sequence"); |
| 9383 | return -1; |
| 9384 | } |
| 9385 | |
| 9386 | cnt = PySequence_Size(buffers); |
| 9387 | if (cnt < 0) { |
| 9388 | return -1; |
| 9389 | } |
| 9390 | |
| 9391 | #ifndef HAVE_PWRITEV2 |
| 9392 | if(flags != 0) { |
| 9393 | argument_unavailable_error("pwritev2", "flags"); |
| 9394 | return -1; |
| 9395 | } |
| 9396 | #endif |
| 9397 | |
| 9398 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9399 | return -1; |
| 9400 | } |
| 9401 | #ifdef HAVE_PWRITEV2 |
| 9402 | do { |
| 9403 | Py_BEGIN_ALLOW_THREADS |
| 9404 | _Py_BEGIN_SUPPRESS_IPH |
| 9405 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9406 | _Py_END_SUPPRESS_IPH |
| 9407 | Py_END_ALLOW_THREADS |
| 9408 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9409 | #else |
| 9410 | do { |
| 9411 | Py_BEGIN_ALLOW_THREADS |
| 9412 | _Py_BEGIN_SUPPRESS_IPH |
| 9413 | result = pwritev(fd, iov, cnt, offset); |
| 9414 | _Py_END_SUPPRESS_IPH |
| 9415 | Py_END_ALLOW_THREADS |
| 9416 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9417 | #endif |
| 9418 | |
| 9419 | iov_cleanup(iov, buf, cnt); |
| 9420 | if (result < 0) { |
| 9421 | if (!async_err) { |
| 9422 | posix_error(); |
| 9423 | } |
| 9424 | return -1; |
| 9425 | } |
| 9426 | |
| 9427 | return result; |
| 9428 | } |
| 9429 | #endif /* HAVE_PWRITEV */ |
| 9430 | |
| 9431 | |
| 9432 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9433 | |
| 9434 | #ifdef HAVE_MKFIFO |
| 9435 | /*[clinic input] |
| 9436 | os.mkfifo |
| 9437 | |
| 9438 | path: path_t |
| 9439 | mode: int=0o666 |
| 9440 | * |
| 9441 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9442 | |
| 9443 | Create a "fifo" (a POSIX named pipe). |
| 9444 | |
| 9445 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9446 | and path should be relative; path will then be relative to that directory. |
| 9447 | dir_fd may not be implemented on your platform. |
| 9448 | If it is unavailable, using it will raise a NotImplementedError. |
| 9449 | [clinic start generated code]*/ |
| 9450 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9451 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9452 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 9453 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9454 | { |
| 9455 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9456 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9457 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9458 | do { |
| 9459 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9460 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9461 | if (dir_fd != DEFAULT_DIR_FD) |
| 9462 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 9463 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9464 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9465 | result = mkfifo(path->narrow, mode); |
| 9466 | Py_END_ALLOW_THREADS |
| 9467 | } while (result != 0 && errno == EINTR && |
| 9468 | !(async_err = PyErr_CheckSignals())); |
| 9469 | if (result != 0) |
| 9470 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9471 | |
| 9472 | Py_RETURN_NONE; |
| 9473 | } |
| 9474 | #endif /* HAVE_MKFIFO */ |
| 9475 | |
| 9476 | |
| 9477 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 9478 | /*[clinic input] |
| 9479 | os.mknod |
| 9480 | |
| 9481 | path: path_t |
| 9482 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9483 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9484 | * |
| 9485 | dir_fd: dir_fd(requires='mknodat')=None |
| 9486 | |
| 9487 | Create a node in the file system. |
| 9488 | |
| 9489 | Create a node in the file system (file, device special file or named pipe) |
| 9490 | at path. mode specifies both the permissions to use and the |
| 9491 | type of node to be created, being combined (bitwise OR) with one of |
| 9492 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 9493 | device defines the newly created device special file (probably using |
| 9494 | os.makedev()). Otherwise device is ignored. |
| 9495 | |
| 9496 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9497 | and path should be relative; path will then be relative to that directory. |
| 9498 | dir_fd may not be implemented on your platform. |
| 9499 | If it is unavailable, using it will raise a NotImplementedError. |
| 9500 | [clinic start generated code]*/ |
| 9501 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9502 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9503 | 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] | 9504 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9505 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9506 | { |
| 9507 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9508 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9509 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9510 | do { |
| 9511 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9512 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9513 | if (dir_fd != DEFAULT_DIR_FD) |
| 9514 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 9515 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9516 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9517 | result = mknod(path->narrow, mode, device); |
| 9518 | Py_END_ALLOW_THREADS |
| 9519 | } while (result != 0 && errno == EINTR && |
| 9520 | !(async_err = PyErr_CheckSignals())); |
| 9521 | if (result != 0) |
| 9522 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9523 | |
| 9524 | Py_RETURN_NONE; |
| 9525 | } |
| 9526 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 9527 | |
| 9528 | |
| 9529 | #ifdef HAVE_DEVICE_MACROS |
| 9530 | /*[clinic input] |
| 9531 | os.major -> unsigned_int |
| 9532 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9533 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9534 | / |
| 9535 | |
| 9536 | Extracts a device major number from a raw device number. |
| 9537 | [clinic start generated code]*/ |
| 9538 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9539 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9540 | os_major_impl(PyObject *module, dev_t device) |
| 9541 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9542 | { |
| 9543 | return major(device); |
| 9544 | } |
| 9545 | |
| 9546 | |
| 9547 | /*[clinic input] |
| 9548 | os.minor -> unsigned_int |
| 9549 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9550 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9551 | / |
| 9552 | |
| 9553 | Extracts a device minor number from a raw device number. |
| 9554 | [clinic start generated code]*/ |
| 9555 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9556 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9557 | os_minor_impl(PyObject *module, dev_t device) |
| 9558 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9559 | { |
| 9560 | return minor(device); |
| 9561 | } |
| 9562 | |
| 9563 | |
| 9564 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9565 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9566 | |
| 9567 | major: int |
| 9568 | minor: int |
| 9569 | / |
| 9570 | |
| 9571 | Composes a raw device number from the major and minor device numbers. |
| 9572 | [clinic start generated code]*/ |
| 9573 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9574 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9575 | os_makedev_impl(PyObject *module, int major, int minor) |
| 9576 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9577 | { |
| 9578 | return makedev(major, minor); |
| 9579 | } |
| 9580 | #endif /* HAVE_DEVICE_MACROS */ |
| 9581 | |
| 9582 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9583 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9584 | /*[clinic input] |
| 9585 | os.ftruncate |
| 9586 | |
| 9587 | fd: int |
| 9588 | length: Py_off_t |
| 9589 | / |
| 9590 | |
| 9591 | Truncate a file, specified by file descriptor, to a specific length. |
| 9592 | [clinic start generated code]*/ |
| 9593 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9594 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9595 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 9596 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9597 | { |
| 9598 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9599 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9600 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9601 | do { |
| 9602 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9603 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9604 | #ifdef MS_WINDOWS |
| 9605 | result = _chsize_s(fd, length); |
| 9606 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9607 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9608 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9609 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9610 | Py_END_ALLOW_THREADS |
| 9611 | } while (result != 0 && errno == EINTR && |
| 9612 | !(async_err = PyErr_CheckSignals())); |
| 9613 | if (result != 0) |
| 9614 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9615 | Py_RETURN_NONE; |
| 9616 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9617 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9618 | |
| 9619 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9620 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9621 | /*[clinic input] |
| 9622 | os.truncate |
| 9623 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 9624 | length: Py_off_t |
| 9625 | |
| 9626 | Truncate a file, specified by path, to a specific length. |
| 9627 | |
| 9628 | On some platforms, path may also be specified as an open file descriptor. |
| 9629 | If this functionality is unavailable, using it raises an exception. |
| 9630 | [clinic start generated code]*/ |
| 9631 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9632 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9633 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 9634 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9635 | { |
| 9636 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9637 | #ifdef MS_WINDOWS |
| 9638 | int fd; |
| 9639 | #endif |
| 9640 | |
| 9641 | if (path->fd != -1) |
| 9642 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9643 | |
| 9644 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9645 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9646 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 9647 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 9648 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9649 | result = -1; |
| 9650 | else { |
| 9651 | result = _chsize_s(fd, length); |
| 9652 | close(fd); |
| 9653 | if (result < 0) |
| 9654 | errno = result; |
| 9655 | } |
| 9656 | #else |
| 9657 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9658 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9659 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9660 | Py_END_ALLOW_THREADS |
| 9661 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 9662 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9663 | |
| 9664 | Py_RETURN_NONE; |
| 9665 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9666 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9667 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9668 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9669 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 9670 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 9671 | defined, which is the case in Python on AIX. AIX bug report: |
| 9672 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 9673 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 9674 | # define POSIX_FADVISE_AIX_BUG |
| 9675 | #endif |
| 9676 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9677 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9678 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9679 | /*[clinic input] |
| 9680 | os.posix_fallocate |
| 9681 | |
| 9682 | fd: int |
| 9683 | offset: Py_off_t |
| 9684 | length: Py_off_t |
| 9685 | / |
| 9686 | |
| 9687 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 9688 | |
| 9689 | Ensure that the file specified by fd encompasses a range of bytes |
| 9690 | starting at offset bytes from the beginning and continuing for length bytes. |
| 9691 | [clinic start generated code]*/ |
| 9692 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9693 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9694 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9695 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9696 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
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; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9700 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9701 | do { |
| 9702 | Py_BEGIN_ALLOW_THREADS |
| 9703 | result = posix_fallocate(fd, offset, length); |
| 9704 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9705 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9706 | |
| 9707 | if (result == 0) |
| 9708 | Py_RETURN_NONE; |
| 9709 | |
| 9710 | if (async_err) |
| 9711 | return NULL; |
| 9712 | |
| 9713 | errno = result; |
| 9714 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9715 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9716 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9717 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9718 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9719 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9720 | /*[clinic input] |
| 9721 | os.posix_fadvise |
| 9722 | |
| 9723 | fd: int |
| 9724 | offset: Py_off_t |
| 9725 | length: Py_off_t |
| 9726 | advice: int |
| 9727 | / |
| 9728 | |
| 9729 | Announce an intention to access data in a specific pattern. |
| 9730 | |
| 9731 | Announce an intention to access data in a specific pattern, thus allowing |
| 9732 | the kernel to make optimizations. |
| 9733 | The advice applies to the region of the file specified by fd starting at |
| 9734 | offset and continuing for length bytes. |
| 9735 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 9736 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 9737 | POSIX_FADV_DONTNEED. |
| 9738 | [clinic start generated code]*/ |
| 9739 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9740 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9741 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9742 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9743 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9744 | { |
| 9745 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9746 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9747 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9748 | do { |
| 9749 | Py_BEGIN_ALLOW_THREADS |
| 9750 | result = posix_fadvise(fd, offset, length, advice); |
| 9751 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9752 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9753 | |
| 9754 | if (result == 0) |
| 9755 | Py_RETURN_NONE; |
| 9756 | |
| 9757 | if (async_err) |
| 9758 | return NULL; |
| 9759 | |
| 9760 | errno = result; |
| 9761 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9762 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9763 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9764 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9765 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9766 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 9767 | /* Save putenv() parameters as values here, so we can collect them when they |
| 9768 | * get re-set with another call for the same key. */ |
| 9769 | static PyObject *posix_putenv_garbage; |
| 9770 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9771 | static void |
| 9772 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9773 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9774 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 9775 | * this will cause previous value to be collected. This has to |
| 9776 | * happen after the real putenv() call because the old value |
| 9777 | * was still accessible until then. */ |
| 9778 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 9779 | /* really not much we can do; just leak */ |
| 9780 | PyErr_Clear(); |
| 9781 | else |
| 9782 | Py_DECREF(value); |
| 9783 | } |
| 9784 | |
| 9785 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 9786 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9787 | /*[clinic input] |
| 9788 | os.putenv |
| 9789 | |
| 9790 | name: unicode |
| 9791 | value: unicode |
| 9792 | / |
| 9793 | |
| 9794 | Change or add an environment variable. |
| 9795 | [clinic start generated code]*/ |
| 9796 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9797 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9798 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 9799 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9800 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 9801 | const wchar_t *env; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9802 | Py_ssize_t size; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9803 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9804 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 9805 | defining hidden environment variables. */ |
| 9806 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 9807 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 9808 | { |
| 9809 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 9810 | return NULL; |
| 9811 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9812 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 9813 | if (unicode == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9814 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9815 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9816 | |
| 9817 | env = PyUnicode_AsUnicodeAndSize(unicode, &size); |
| 9818 | if (env == NULL) |
| 9819 | goto error; |
| 9820 | if (size > _MAX_ENV) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9821 | PyErr_Format(PyExc_ValueError, |
| 9822 | "the environment variable is longer than %u characters", |
| 9823 | _MAX_ENV); |
| 9824 | goto error; |
| 9825 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9826 | if (wcslen(env) != (size_t)size) { |
| 9827 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9828 | goto error; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9829 | } |
| 9830 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9831 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9832 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9833 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9834 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9835 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9836 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9837 | Py_RETURN_NONE; |
| 9838 | |
| 9839 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9840 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9841 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9842 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9843 | #else /* MS_WINDOWS */ |
| 9844 | /*[clinic input] |
| 9845 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9847 | name: FSConverter |
| 9848 | value: FSConverter |
| 9849 | / |
| 9850 | |
| 9851 | Change or add an environment variable. |
| 9852 | [clinic start generated code]*/ |
| 9853 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9854 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9855 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 9856 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9857 | { |
| 9858 | PyObject *bytes = NULL; |
| 9859 | char *env; |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9860 | const char *name_string = PyBytes_AS_STRING(name); |
| 9861 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9862 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9863 | if (strchr(name_string, '=') != NULL) { |
| 9864 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 9865 | return NULL; |
| 9866 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9867 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 9868 | if (bytes == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9869 | return NULL; |
| 9870 | } |
| 9871 | |
| 9872 | env = PyBytes_AS_STRING(bytes); |
| 9873 | if (putenv(env)) { |
| 9874 | Py_DECREF(bytes); |
| 9875 | return posix_error(); |
| 9876 | } |
| 9877 | |
| 9878 | posix_putenv_garbage_setitem(name, bytes); |
| 9879 | Py_RETURN_NONE; |
| 9880 | } |
| 9881 | #endif /* MS_WINDOWS */ |
| 9882 | #endif /* HAVE_PUTENV */ |
| 9883 | |
| 9884 | |
| 9885 | #ifdef HAVE_UNSETENV |
| 9886 | /*[clinic input] |
| 9887 | os.unsetenv |
| 9888 | name: FSConverter |
| 9889 | / |
| 9890 | |
| 9891 | Delete an environment variable. |
| 9892 | [clinic start generated code]*/ |
| 9893 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9894 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9895 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 9896 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9897 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9898 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9899 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9900 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9901 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9902 | #ifdef HAVE_BROKEN_UNSETENV |
| 9903 | unsetenv(PyBytes_AS_STRING(name)); |
| 9904 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9905 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9906 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9907 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9908 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9909 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9910 | /* Remove the key from posix_putenv_garbage; |
| 9911 | * this will cause it to be collected. This has to |
| 9912 | * happen after the real unsetenv() call because the |
| 9913 | * old value was still accessible until then. |
| 9914 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9915 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9916 | /* really not much we can do; just leak */ |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 9917 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 9918 | return NULL; |
| 9919 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9920 | PyErr_Clear(); |
| 9921 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9922 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9923 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9924 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9925 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9926 | |
| 9927 | /*[clinic input] |
| 9928 | os.strerror |
| 9929 | |
| 9930 | code: int |
| 9931 | / |
| 9932 | |
| 9933 | Translate an error code to a message string. |
| 9934 | [clinic start generated code]*/ |
| 9935 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9936 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9937 | os_strerror_impl(PyObject *module, int code) |
| 9938 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9939 | { |
| 9940 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9941 | if (message == NULL) { |
| 9942 | PyErr_SetString(PyExc_ValueError, |
| 9943 | "strerror() argument out of range"); |
| 9944 | return NULL; |
| 9945 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 9946 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9947 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9948 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9949 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9950 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9951 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9952 | /*[clinic input] |
| 9953 | os.WCOREDUMP -> bool |
| 9954 | |
| 9955 | status: int |
| 9956 | / |
| 9957 | |
| 9958 | Return True if the process returning status was dumped to a core file. |
| 9959 | [clinic start generated code]*/ |
| 9960 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9961 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9962 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 9963 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9964 | { |
| 9965 | WAIT_TYPE wait_status; |
| 9966 | WAIT_STATUS_INT(wait_status) = status; |
| 9967 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9968 | } |
| 9969 | #endif /* WCOREDUMP */ |
| 9970 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9971 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9972 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9973 | /*[clinic input] |
| 9974 | os.WIFCONTINUED -> bool |
| 9975 | |
| 9976 | status: int |
| 9977 | |
| 9978 | Return True if a particular process was continued from a job control stop. |
| 9979 | |
| 9980 | Return True if the process returning status was continued from a |
| 9981 | job control stop. |
| 9982 | [clinic start generated code]*/ |
| 9983 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9984 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9985 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 9986 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9987 | { |
| 9988 | WAIT_TYPE wait_status; |
| 9989 | WAIT_STATUS_INT(wait_status) = status; |
| 9990 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9991 | } |
| 9992 | #endif /* WIFCONTINUED */ |
| 9993 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9994 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9995 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9996 | /*[clinic input] |
| 9997 | os.WIFSTOPPED -> bool |
| 9998 | |
| 9999 | status: int |
| 10000 | |
| 10001 | Return True if the process returning status was stopped. |
| 10002 | [clinic start generated code]*/ |
| 10003 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10004 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10005 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 10006 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10007 | { |
| 10008 | WAIT_TYPE wait_status; |
| 10009 | WAIT_STATUS_INT(wait_status) = status; |
| 10010 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10011 | } |
| 10012 | #endif /* WIFSTOPPED */ |
| 10013 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10014 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10015 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10016 | /*[clinic input] |
| 10017 | os.WIFSIGNALED -> bool |
| 10018 | |
| 10019 | status: int |
| 10020 | |
| 10021 | Return True if the process returning status was terminated by a signal. |
| 10022 | [clinic start generated code]*/ |
| 10023 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10024 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10025 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 10026 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10027 | { |
| 10028 | WAIT_TYPE wait_status; |
| 10029 | WAIT_STATUS_INT(wait_status) = status; |
| 10030 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10031 | } |
| 10032 | #endif /* WIFSIGNALED */ |
| 10033 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10034 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10035 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10036 | /*[clinic input] |
| 10037 | os.WIFEXITED -> bool |
| 10038 | |
| 10039 | status: int |
| 10040 | |
| 10041 | Return True if the process returning status exited via the exit() system call. |
| 10042 | [clinic start generated code]*/ |
| 10043 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10044 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10045 | os_WIFEXITED_impl(PyObject *module, int status) |
| 10046 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10047 | { |
| 10048 | WAIT_TYPE wait_status; |
| 10049 | WAIT_STATUS_INT(wait_status) = status; |
| 10050 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10051 | } |
| 10052 | #endif /* WIFEXITED */ |
| 10053 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10054 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 10055 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10056 | /*[clinic input] |
| 10057 | os.WEXITSTATUS -> int |
| 10058 | |
| 10059 | status: int |
| 10060 | |
| 10061 | Return the process return code from status. |
| 10062 | [clinic start generated code]*/ |
| 10063 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10064 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10065 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 10066 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10067 | { |
| 10068 | WAIT_TYPE wait_status; |
| 10069 | WAIT_STATUS_INT(wait_status) = status; |
| 10070 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10071 | } |
| 10072 | #endif /* WEXITSTATUS */ |
| 10073 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10074 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10075 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10076 | /*[clinic input] |
| 10077 | os.WTERMSIG -> int |
| 10078 | |
| 10079 | status: int |
| 10080 | |
| 10081 | Return the signal that terminated the process that provided the status value. |
| 10082 | [clinic start generated code]*/ |
| 10083 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10084 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10085 | os_WTERMSIG_impl(PyObject *module, int status) |
| 10086 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10087 | { |
| 10088 | WAIT_TYPE wait_status; |
| 10089 | WAIT_STATUS_INT(wait_status) = status; |
| 10090 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10091 | } |
| 10092 | #endif /* WTERMSIG */ |
| 10093 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10094 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10095 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10096 | /*[clinic input] |
| 10097 | os.WSTOPSIG -> int |
| 10098 | |
| 10099 | status: int |
| 10100 | |
| 10101 | Return the signal that stopped the process that provided the status value. |
| 10102 | [clinic start generated code]*/ |
| 10103 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10104 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10105 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 10106 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10107 | { |
| 10108 | WAIT_TYPE wait_status; |
| 10109 | WAIT_STATUS_INT(wait_status) = status; |
| 10110 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10111 | } |
| 10112 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10113 | #endif /* HAVE_SYS_WAIT_H */ |
| 10114 | |
| 10115 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10116 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 10117 | #ifdef _SCO_DS |
| 10118 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 10119 | needed definitions in sys/statvfs.h */ |
| 10120 | #define _SVID3 |
| 10121 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10122 | #include <sys/statvfs.h> |
| 10123 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10124 | static PyObject* |
| 10125 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 10126 | PyObject *v = PyStructSequence_New(StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10127 | if (v == NULL) |
| 10128 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10129 | |
| 10130 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10131 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10132 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10133 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 10134 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 10135 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 10136 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 10137 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 10138 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 10139 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10140 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10141 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10142 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10143 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10144 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10145 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10146 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10147 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10148 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10149 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10150 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10151 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10152 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10153 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10154 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10155 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10156 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10157 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10158 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10159 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 10160 | * (issue #32390). */ |
| 10161 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 10162 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 10163 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 10164 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10165 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 10166 | if (PyErr_Occurred()) { |
| 10167 | Py_DECREF(v); |
| 10168 | return NULL; |
| 10169 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10170 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10171 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10172 | } |
| 10173 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10174 | |
| 10175 | /*[clinic input] |
| 10176 | os.fstatvfs |
| 10177 | fd: int |
| 10178 | / |
| 10179 | |
| 10180 | Perform an fstatvfs system call on the given fd. |
| 10181 | |
| 10182 | Equivalent to statvfs(fd). |
| 10183 | [clinic start generated code]*/ |
| 10184 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10185 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10186 | os_fstatvfs_impl(PyObject *module, int fd) |
| 10187 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10188 | { |
| 10189 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10190 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10191 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10192 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10193 | do { |
| 10194 | Py_BEGIN_ALLOW_THREADS |
| 10195 | result = fstatvfs(fd, &st); |
| 10196 | Py_END_ALLOW_THREADS |
| 10197 | } while (result != 0 && errno == EINTR && |
| 10198 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10199 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10200 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10201 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10202 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10203 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10204 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10205 | |
| 10206 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10207 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10208 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10209 | /*[clinic input] |
| 10210 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10211 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10212 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 10213 | |
| 10214 | Perform a statvfs system call on the given path. |
| 10215 | |
| 10216 | path may always be specified as a string. |
| 10217 | On some platforms, path may also be specified as an open file descriptor. |
| 10218 | If this functionality is unavailable, using it raises an exception. |
| 10219 | [clinic start generated code]*/ |
| 10220 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10221 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10222 | os_statvfs_impl(PyObject *module, path_t *path) |
| 10223 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10224 | { |
| 10225 | int result; |
| 10226 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10227 | |
| 10228 | Py_BEGIN_ALLOW_THREADS |
| 10229 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10230 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10231 | #ifdef __APPLE__ |
| 10232 | /* handle weak-linking on Mac OS X 10.3 */ |
| 10233 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10234 | fd_specified("statvfs", path->fd); |
| 10235 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10236 | } |
| 10237 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10238 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10239 | } |
| 10240 | else |
| 10241 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10242 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10243 | Py_END_ALLOW_THREADS |
| 10244 | |
| 10245 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10246 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10247 | } |
| 10248 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10249 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10250 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10251 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 10252 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10253 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10254 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10255 | /*[clinic input] |
| 10256 | os._getdiskusage |
| 10257 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10258 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10259 | |
| 10260 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 10261 | [clinic start generated code]*/ |
| 10262 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10263 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10264 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 10265 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10266 | { |
| 10267 | BOOL retval; |
| 10268 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10269 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10270 | |
| 10271 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10272 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10273 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10274 | if (retval == 0) { |
| 10275 | if (GetLastError() == ERROR_DIRECTORY) { |
| 10276 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10277 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10278 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 10279 | if (dir_path == NULL) { |
| 10280 | return PyErr_NoMemory(); |
| 10281 | } |
| 10282 | |
| 10283 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 10284 | |
| 10285 | if (_dirnameW(dir_path) != -1) { |
| 10286 | Py_BEGIN_ALLOW_THREADS |
| 10287 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 10288 | Py_END_ALLOW_THREADS |
| 10289 | } |
| 10290 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 10291 | err = GetLastError(); |
| 10292 | PyMem_Free(dir_path); |
| 10293 | if (retval) { |
| 10294 | goto success; |
| 10295 | } |
| 10296 | } |
| 10297 | return PyErr_SetFromWindowsErr(err); |
| 10298 | } |
| 10299 | |
| 10300 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10301 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 10302 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10303 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10304 | |
| 10305 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10306 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 10307 | * It maps strings representing configuration variable names to |
| 10308 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10309 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10310 | * rarely-used constants. There are three separate tables that use |
| 10311 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10312 | * |
| 10313 | * This code is always included, even if none of the interfaces that |
| 10314 | * need it are included. The #if hackery needed to avoid it would be |
| 10315 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10316 | */ |
| 10317 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10318 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10319 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10320 | }; |
| 10321 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10322 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10323 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10324 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10325 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10326 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10327 | int value = _PyLong_AsInt(arg); |
| 10328 | if (value == -1 && PyErr_Occurred()) |
| 10329 | return 0; |
| 10330 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10331 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10332 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10333 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10334 | /* look up the value in the table using a binary search */ |
| 10335 | size_t lo = 0; |
| 10336 | size_t mid; |
| 10337 | size_t hi = tablesize; |
| 10338 | int cmp; |
| 10339 | const char *confname; |
| 10340 | if (!PyUnicode_Check(arg)) { |
| 10341 | PyErr_SetString(PyExc_TypeError, |
| 10342 | "configuration names must be strings or integers"); |
| 10343 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10344 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10345 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10346 | if (confname == NULL) |
| 10347 | return 0; |
| 10348 | while (lo < hi) { |
| 10349 | mid = (lo + hi) / 2; |
| 10350 | cmp = strcmp(confname, table[mid].name); |
| 10351 | if (cmp < 0) |
| 10352 | hi = mid; |
| 10353 | else if (cmp > 0) |
| 10354 | lo = mid + 1; |
| 10355 | else { |
| 10356 | *valuep = table[mid].value; |
| 10357 | return 1; |
| 10358 | } |
| 10359 | } |
| 10360 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 10361 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10362 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10363 | } |
| 10364 | |
| 10365 | |
| 10366 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 10367 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10368 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10369 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10370 | #endif |
| 10371 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10372 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10373 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10374 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10375 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10376 | #endif |
| 10377 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10378 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10379 | #endif |
| 10380 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10381 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10382 | #endif |
| 10383 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10384 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10385 | #endif |
| 10386 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10387 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10388 | #endif |
| 10389 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10390 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10391 | #endif |
| 10392 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10393 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10394 | #endif |
| 10395 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10396 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10397 | #endif |
| 10398 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10399 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10400 | #endif |
| 10401 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10402 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10403 | #endif |
| 10404 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10405 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10406 | #endif |
| 10407 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10408 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10409 | #endif |
| 10410 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10411 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10412 | #endif |
| 10413 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10414 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10415 | #endif |
| 10416 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10417 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10418 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10419 | #ifdef _PC_ACL_ENABLED |
| 10420 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10421 | #endif |
| 10422 | #ifdef _PC_MIN_HOLE_SIZE |
| 10423 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10424 | #endif |
| 10425 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10426 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10427 | #endif |
| 10428 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10429 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10430 | #endif |
| 10431 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10432 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10433 | #endif |
| 10434 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10435 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10436 | #endif |
| 10437 | #ifdef _PC_REC_XFER_ALIGN |
| 10438 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10439 | #endif |
| 10440 | #ifdef _PC_SYMLINK_MAX |
| 10441 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 10442 | #endif |
| 10443 | #ifdef _PC_XATTR_ENABLED |
| 10444 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 10445 | #endif |
| 10446 | #ifdef _PC_XATTR_EXISTS |
| 10447 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 10448 | #endif |
| 10449 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 10450 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 10451 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10452 | }; |
| 10453 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10454 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10455 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10456 | { |
| 10457 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 10458 | sizeof(posix_constants_pathconf) |
| 10459 | / sizeof(struct constdef)); |
| 10460 | } |
| 10461 | #endif |
| 10462 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10463 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10464 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10465 | /*[clinic input] |
| 10466 | os.fpathconf -> long |
| 10467 | |
| 10468 | fd: int |
| 10469 | name: path_confname |
| 10470 | / |
| 10471 | |
| 10472 | Return the configuration limit name for the file descriptor fd. |
| 10473 | |
| 10474 | If there is no limit, return -1. |
| 10475 | [clinic start generated code]*/ |
| 10476 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10477 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10478 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 10479 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10480 | { |
| 10481 | long limit; |
| 10482 | |
| 10483 | errno = 0; |
| 10484 | limit = fpathconf(fd, name); |
| 10485 | if (limit == -1 && errno != 0) |
| 10486 | posix_error(); |
| 10487 | |
| 10488 | return limit; |
| 10489 | } |
| 10490 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10491 | |
| 10492 | |
| 10493 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10494 | /*[clinic input] |
| 10495 | os.pathconf -> long |
| 10496 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 10497 | name: path_confname |
| 10498 | |
| 10499 | Return the configuration limit name for the file or directory path. |
| 10500 | |
| 10501 | If there is no limit, return -1. |
| 10502 | On some platforms, path may also be specified as an open file descriptor. |
| 10503 | If this functionality is unavailable, using it raises an exception. |
| 10504 | [clinic start generated code]*/ |
| 10505 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10506 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10507 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 10508 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10509 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10510 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10511 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10512 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10513 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10514 | if (path->fd != -1) |
| 10515 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10516 | else |
| 10517 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10518 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10519 | if (limit == -1 && errno != 0) { |
| 10520 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 10521 | /* could be a path or name problem */ |
| 10522 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10523 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10524 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10525 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10526 | |
| 10527 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10528 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10529 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10530 | |
| 10531 | #ifdef HAVE_CONFSTR |
| 10532 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10533 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10534 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10535 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10536 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10537 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10538 | #endif |
| 10539 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10540 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10541 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10542 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10543 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10544 | #endif |
| 10545 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10546 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10547 | #endif |
| 10548 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10549 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10550 | #endif |
| 10551 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10552 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10553 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10554 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10555 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10556 | #endif |
| 10557 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10558 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10559 | #endif |
| 10560 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10561 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10562 | #endif |
| 10563 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10564 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10565 | #endif |
| 10566 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10567 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10568 | #endif |
| 10569 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10570 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10571 | #endif |
| 10572 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10573 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10574 | #endif |
| 10575 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10576 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10577 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10578 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10579 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10580 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10581 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10582 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10583 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10584 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10585 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10586 | #endif |
| 10587 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10588 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10589 | #endif |
| 10590 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10591 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10592 | #endif |
| 10593 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10594 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10595 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10596 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10597 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10598 | #endif |
| 10599 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10600 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10601 | #endif |
| 10602 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10603 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10604 | #endif |
| 10605 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10606 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10607 | #endif |
| 10608 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10609 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10610 | #endif |
| 10611 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10612 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10613 | #endif |
| 10614 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10615 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10616 | #endif |
| 10617 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10618 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10619 | #endif |
| 10620 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10621 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10622 | #endif |
| 10623 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10624 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10625 | #endif |
| 10626 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10627 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10628 | #endif |
| 10629 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10630 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10631 | #endif |
| 10632 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10633 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10634 | #endif |
| 10635 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10636 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10637 | #endif |
| 10638 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10639 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10640 | #endif |
| 10641 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10642 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10643 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10644 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10645 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10646 | #endif |
| 10647 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10648 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10649 | #endif |
| 10650 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10651 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10652 | #endif |
| 10653 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10654 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10655 | #endif |
| 10656 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10657 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10658 | #endif |
| 10659 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10660 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10661 | #endif |
| 10662 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10663 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10664 | #endif |
| 10665 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10666 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10667 | #endif |
| 10668 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10669 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10670 | #endif |
| 10671 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10672 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10673 | #endif |
| 10674 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10675 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10676 | #endif |
| 10677 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10678 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10679 | #endif |
| 10680 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10681 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10682 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10683 | }; |
| 10684 | |
| 10685 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10686 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10687 | { |
| 10688 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 10689 | sizeof(posix_constants_confstr) |
| 10690 | / sizeof(struct constdef)); |
| 10691 | } |
| 10692 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10693 | |
| 10694 | /*[clinic input] |
| 10695 | os.confstr |
| 10696 | |
| 10697 | name: confstr_confname |
| 10698 | / |
| 10699 | |
| 10700 | Return a string-valued system configuration variable. |
| 10701 | [clinic start generated code]*/ |
| 10702 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10703 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10704 | os_confstr_impl(PyObject *module, int name) |
| 10705 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10706 | { |
| 10707 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10708 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10709 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10710 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10711 | errno = 0; |
| 10712 | len = confstr(name, buffer, sizeof(buffer)); |
| 10713 | if (len == 0) { |
| 10714 | if (errno) { |
| 10715 | posix_error(); |
| 10716 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10717 | } |
| 10718 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10719 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10720 | } |
| 10721 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10722 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10723 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 10724 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10725 | char *buf = PyMem_Malloc(len); |
| 10726 | if (buf == NULL) |
| 10727 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 10728 | len2 = confstr(name, buf, len); |
| 10729 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 10730 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10731 | PyMem_Free(buf); |
| 10732 | } |
| 10733 | else |
| 10734 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10735 | return result; |
| 10736 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10737 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10738 | |
| 10739 | |
| 10740 | #ifdef HAVE_SYSCONF |
| 10741 | static struct constdef posix_constants_sysconf[] = { |
| 10742 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10743 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10744 | #endif |
| 10745 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10746 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10747 | #endif |
| 10748 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10749 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10750 | #endif |
| 10751 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10752 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10753 | #endif |
| 10754 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10755 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10756 | #endif |
| 10757 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10758 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10759 | #endif |
| 10760 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10761 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10762 | #endif |
| 10763 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10764 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10765 | #endif |
| 10766 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10767 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10768 | #endif |
| 10769 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10770 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10771 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10772 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10773 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10774 | #endif |
| 10775 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10776 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10777 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10778 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10779 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10780 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10781 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10782 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10783 | #endif |
| 10784 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10785 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10786 | #endif |
| 10787 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10788 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10789 | #endif |
| 10790 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10791 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10792 | #endif |
| 10793 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10794 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10795 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10796 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10797 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10798 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10799 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10800 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10801 | #endif |
| 10802 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10803 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10804 | #endif |
| 10805 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10806 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10807 | #endif |
| 10808 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10809 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10810 | #endif |
| 10811 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10812 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10813 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10814 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10815 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10816 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10817 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10818 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10819 | #endif |
| 10820 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10821 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10822 | #endif |
| 10823 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10824 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10825 | #endif |
| 10826 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10827 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10828 | #endif |
| 10829 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10830 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10831 | #endif |
| 10832 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10833 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10834 | #endif |
| 10835 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10836 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10837 | #endif |
| 10838 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10839 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10840 | #endif |
| 10841 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10842 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10843 | #endif |
| 10844 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10845 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10846 | #endif |
| 10847 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10848 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10849 | #endif |
| 10850 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10851 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10852 | #endif |
| 10853 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10854 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10855 | #endif |
| 10856 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10857 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10858 | #endif |
| 10859 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10860 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10861 | #endif |
| 10862 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10863 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10864 | #endif |
| 10865 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10866 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10867 | #endif |
| 10868 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10869 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10870 | #endif |
| 10871 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10872 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10873 | #endif |
| 10874 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10875 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10876 | #endif |
| 10877 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10878 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10879 | #endif |
| 10880 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10881 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10882 | #endif |
| 10883 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10884 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10885 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10886 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10887 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10888 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10889 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10890 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10891 | #endif |
| 10892 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10893 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10894 | #endif |
| 10895 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10896 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10897 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10898 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10899 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10900 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10901 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10902 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
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_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10905 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10906 | #endif |
| 10907 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10908 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10909 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10910 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10911 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10912 | #endif |
| 10913 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10914 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10915 | #endif |
| 10916 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10917 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10918 | #endif |
| 10919 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10920 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
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_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10923 | {"SC_MAC", _SC_MAC}, |
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_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10926 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10927 | #endif |
| 10928 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10929 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10930 | #endif |
| 10931 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10932 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10933 | #endif |
| 10934 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10935 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10936 | #endif |
| 10937 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10938 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10939 | #endif |
| 10940 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10941 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10942 | #endif |
| 10943 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10944 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10945 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10946 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10947 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10948 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10949 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10950 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10951 | #endif |
| 10952 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10953 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10954 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10955 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10956 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10957 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10958 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10959 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10960 | #endif |
| 10961 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10962 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10963 | #endif |
| 10964 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10965 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10966 | #endif |
| 10967 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10968 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10969 | #endif |
| 10970 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10971 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10972 | #endif |
| 10973 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10974 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10975 | #endif |
| 10976 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10977 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10978 | #endif |
| 10979 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10980 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10981 | #endif |
| 10982 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10983 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10984 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10985 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10986 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10987 | #endif |
| 10988 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10989 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10990 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10991 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10992 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10993 | #endif |
| 10994 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10995 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10996 | #endif |
| 10997 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10998 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10999 | #endif |
| 11000 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11001 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11002 | #endif |
| 11003 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11004 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11005 | #endif |
| 11006 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11007 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11008 | #endif |
| 11009 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11010 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11011 | #endif |
| 11012 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11013 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11014 | #endif |
| 11015 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11016 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11017 | #endif |
| 11018 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11019 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11020 | #endif |
| 11021 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11022 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11023 | #endif |
| 11024 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11025 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11026 | #endif |
| 11027 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11028 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11029 | #endif |
| 11030 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11031 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11032 | #endif |
| 11033 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11034 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11035 | #endif |
| 11036 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11037 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11038 | #endif |
| 11039 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11040 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11041 | #endif |
| 11042 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11043 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11044 | #endif |
| 11045 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11046 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11047 | #endif |
| 11048 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11049 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11050 | #endif |
| 11051 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11052 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11053 | #endif |
| 11054 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11055 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11056 | #endif |
| 11057 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11058 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11059 | #endif |
| 11060 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11061 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11062 | #endif |
| 11063 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11064 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11065 | #endif |
| 11066 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11067 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11068 | #endif |
| 11069 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11070 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11071 | #endif |
| 11072 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11073 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11074 | #endif |
| 11075 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11076 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11077 | #endif |
| 11078 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11079 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11080 | #endif |
| 11081 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11082 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11083 | #endif |
| 11084 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11085 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11086 | #endif |
| 11087 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11088 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11089 | #endif |
| 11090 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11091 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11092 | #endif |
| 11093 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11094 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11095 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11096 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11097 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
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_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11100 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11101 | #endif |
| 11102 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11103 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11104 | #endif |
| 11105 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11106 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11107 | #endif |
| 11108 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11109 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11110 | #endif |
| 11111 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11112 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11113 | #endif |
| 11114 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11115 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11116 | #endif |
| 11117 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11118 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11119 | #endif |
| 11120 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11121 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11122 | #endif |
| 11123 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11124 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11125 | #endif |
| 11126 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11127 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11128 | #endif |
| 11129 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11130 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11131 | #endif |
| 11132 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11133 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11134 | #endif |
| 11135 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11136 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11137 | #endif |
| 11138 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11139 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11140 | #endif |
| 11141 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11142 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11143 | #endif |
| 11144 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11145 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11146 | #endif |
| 11147 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11148 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11149 | #endif |
| 11150 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11151 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11152 | #endif |
| 11153 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11154 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11155 | #endif |
| 11156 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11157 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11158 | #endif |
| 11159 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11160 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11161 | #endif |
| 11162 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11163 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11164 | #endif |
| 11165 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11166 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11167 | #endif |
| 11168 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11169 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11170 | #endif |
| 11171 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11172 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11173 | #endif |
| 11174 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11175 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11176 | #endif |
| 11177 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11178 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11179 | #endif |
| 11180 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11181 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11182 | #endif |
| 11183 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11184 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11185 | #endif |
| 11186 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11187 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11188 | #endif |
| 11189 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11190 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11191 | #endif |
| 11192 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11193 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11194 | #endif |
| 11195 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11196 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11197 | #endif |
| 11198 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11199 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11200 | #endif |
| 11201 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11202 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11203 | #endif |
| 11204 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11205 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11206 | #endif |
| 11207 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11208 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11209 | #endif |
| 11210 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11211 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11212 | #endif |
| 11213 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11214 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11215 | #endif |
| 11216 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11217 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11218 | #endif |
| 11219 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11220 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11221 | #endif |
| 11222 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11223 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11224 | #endif |
| 11225 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11226 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11227 | #endif |
| 11228 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11229 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11230 | #endif |
| 11231 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11232 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11233 | #endif |
| 11234 | }; |
| 11235 | |
| 11236 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11237 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11238 | { |
| 11239 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 11240 | sizeof(posix_constants_sysconf) |
| 11241 | / sizeof(struct constdef)); |
| 11242 | } |
| 11243 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11244 | |
| 11245 | /*[clinic input] |
| 11246 | os.sysconf -> long |
| 11247 | name: sysconf_confname |
| 11248 | / |
| 11249 | |
| 11250 | Return an integer-valued system configuration variable. |
| 11251 | [clinic start generated code]*/ |
| 11252 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11253 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11254 | os_sysconf_impl(PyObject *module, int name) |
| 11255 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11256 | { |
| 11257 | long value; |
| 11258 | |
| 11259 | errno = 0; |
| 11260 | value = sysconf(name); |
| 11261 | if (value == -1 && errno != 0) |
| 11262 | posix_error(); |
| 11263 | return value; |
| 11264 | } |
| 11265 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11266 | |
| 11267 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11268 | /* 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] | 11269 | * 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] | 11270 | * the exported dictionaries that are used to publish information about the |
| 11271 | * names available on the host platform. |
| 11272 | * |
| 11273 | * Sorting the table at runtime ensures that the table is properly ordered |
| 11274 | * when used, even for platforms we're not able to test on. It also makes |
| 11275 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11276 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11277 | |
| 11278 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11279 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11280 | { |
| 11281 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11282 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11283 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11284 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11285 | |
| 11286 | return strcmp(c1->name, c2->name); |
| 11287 | } |
| 11288 | |
| 11289 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11290 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11291 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11292 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11293 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11294 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11295 | |
| 11296 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 11297 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11298 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11299 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11300 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11301 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11302 | PyObject *o = PyLong_FromLong(table[i].value); |
| 11303 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 11304 | Py_XDECREF(o); |
| 11305 | Py_DECREF(d); |
| 11306 | return -1; |
| 11307 | } |
| 11308 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11309 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11310 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11311 | } |
| 11312 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11313 | /* Return -1 on failure, 0 on success. */ |
| 11314 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11315 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11316 | { |
| 11317 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11318 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11319 | sizeof(posix_constants_pathconf) |
| 11320 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11321 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11322 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11323 | #endif |
| 11324 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11325 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11326 | sizeof(posix_constants_confstr) |
| 11327 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11328 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11329 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11330 | #endif |
| 11331 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11332 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11333 | sizeof(posix_constants_sysconf) |
| 11334 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11335 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11336 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11337 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11338 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11339 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11340 | |
| 11341 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11342 | /*[clinic input] |
| 11343 | os.abort |
| 11344 | |
| 11345 | Abort the interpreter immediately. |
| 11346 | |
| 11347 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 11348 | on the hosting operating system. This function never returns. |
| 11349 | [clinic start generated code]*/ |
| 11350 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11351 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11352 | os_abort_impl(PyObject *module) |
| 11353 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11354 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11355 | abort(); |
| 11356 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11357 | #ifndef __clang__ |
| 11358 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 11359 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 11360 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11361 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 11362 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11363 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11364 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11365 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11366 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11367 | /* Grab ShellExecute dynamically from shell32 */ |
| 11368 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11369 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 11370 | LPCWSTR, INT); |
| 11371 | static int |
| 11372 | check_ShellExecute() |
| 11373 | { |
| 11374 | HINSTANCE hShell32; |
| 11375 | |
| 11376 | /* only recheck */ |
| 11377 | if (-1 == has_ShellExecute) { |
| 11378 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11379 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 11380 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 11381 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 11382 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11383 | hShell32 = LoadLibraryW(L"SHELL32"); |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11384 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11385 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 11386 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11387 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11388 | } else { |
| 11389 | has_ShellExecute = 0; |
| 11390 | } |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 11391 | Py_END_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11392 | } |
| 11393 | return has_ShellExecute; |
| 11394 | } |
| 11395 | |
| 11396 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11397 | /*[clinic input] |
| 11398 | os.startfile |
| 11399 | filepath: path_t |
| 11400 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11401 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11402 | startfile(filepath [, operation]) |
| 11403 | |
| 11404 | Start a file with its associated application. |
| 11405 | |
| 11406 | When "operation" is not specified or "open", this acts like |
| 11407 | double-clicking the file in Explorer, or giving the file name as an |
| 11408 | argument to the DOS "start" command: the file is opened with whatever |
| 11409 | application (if any) its extension is associated. |
| 11410 | When another "operation" is given, it specifies what should be done with |
| 11411 | the file. A typical operation is "print". |
| 11412 | |
| 11413 | startfile returns as soon as the associated application is launched. |
| 11414 | There is no option to wait for the application to close, and no way |
| 11415 | to retrieve the application's exit status. |
| 11416 | |
| 11417 | The filepath is relative to the current directory. If you want to use |
| 11418 | an absolute path, make sure the first character is not a slash ("/"); |
| 11419 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 11420 | [clinic start generated code]*/ |
| 11421 | |
| 11422 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11423 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 11424 | const Py_UNICODE *operation) |
| 11425 | /*[clinic end generated code: output=66dc311c94d50797 input=63950bf2986380d0]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11426 | { |
| 11427 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11428 | |
| 11429 | if(!check_ShellExecute()) { |
| 11430 | /* If the OS doesn't have ShellExecute, return a |
| 11431 | NotImplementedError. */ |
| 11432 | return PyErr_Format(PyExc_NotImplementedError, |
| 11433 | "startfile not available on this platform"); |
| 11434 | } |
| 11435 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11436 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11437 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11438 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11439 | Py_END_ALLOW_THREADS |
| 11440 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11441 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11442 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11443 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11444 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11445 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11446 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11447 | #endif /* MS_WINDOWS */ |
| 11448 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11449 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11450 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11451 | /*[clinic input] |
| 11452 | os.getloadavg |
| 11453 | |
| 11454 | Return average recent system load information. |
| 11455 | |
| 11456 | Return the number of processes in the system run queue averaged over |
| 11457 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 11458 | Raises OSError if the load average was unobtainable. |
| 11459 | [clinic start generated code]*/ |
| 11460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11461 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11462 | os_getloadavg_impl(PyObject *module) |
| 11463 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11464 | { |
| 11465 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11466 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11467 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 11468 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11469 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11470 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11471 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11472 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11473 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11474 | |
| 11475 | /*[clinic input] |
| 11476 | os.device_encoding |
| 11477 | fd: int |
| 11478 | |
| 11479 | Return a string describing the encoding of a terminal's file descriptor. |
| 11480 | |
| 11481 | The file descriptor must be attached to a terminal. |
| 11482 | If the device is not a terminal, return None. |
| 11483 | [clinic start generated code]*/ |
| 11484 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11485 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11486 | os_device_encoding_impl(PyObject *module, int fd) |
| 11487 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11488 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 11489 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 11490 | } |
| 11491 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11492 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11493 | #ifdef HAVE_SETRESUID |
| 11494 | /*[clinic input] |
| 11495 | os.setresuid |
| 11496 | |
| 11497 | ruid: uid_t |
| 11498 | euid: uid_t |
| 11499 | suid: uid_t |
| 11500 | / |
| 11501 | |
| 11502 | Set the current process's real, effective, and saved user ids. |
| 11503 | [clinic start generated code]*/ |
| 11504 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11505 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11506 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 11507 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11508 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11509 | if (setresuid(ruid, euid, suid) < 0) |
| 11510 | return posix_error(); |
| 11511 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11512 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11513 | #endif /* HAVE_SETRESUID */ |
| 11514 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11515 | |
| 11516 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11517 | /*[clinic input] |
| 11518 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11519 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11520 | rgid: gid_t |
| 11521 | egid: gid_t |
| 11522 | sgid: gid_t |
| 11523 | / |
| 11524 | |
| 11525 | Set the current process's real, effective, and saved group ids. |
| 11526 | [clinic start generated code]*/ |
| 11527 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11528 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11529 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 11530 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11531 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11532 | if (setresgid(rgid, egid, sgid) < 0) |
| 11533 | return posix_error(); |
| 11534 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11535 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11536 | #endif /* HAVE_SETRESGID */ |
| 11537 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11538 | |
| 11539 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11540 | /*[clinic input] |
| 11541 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11542 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11543 | Return a tuple of the current process's real, effective, and saved user ids. |
| 11544 | [clinic start generated code]*/ |
| 11545 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11546 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11547 | os_getresuid_impl(PyObject *module) |
| 11548 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11549 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11550 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11551 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 11552 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11553 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 11554 | _PyLong_FromUid(euid), |
| 11555 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11556 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11557 | #endif /* HAVE_GETRESUID */ |
| 11558 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11559 | |
| 11560 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11561 | /*[clinic input] |
| 11562 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11563 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11564 | Return a tuple of the current process's real, effective, and saved group ids. |
| 11565 | [clinic start generated code]*/ |
| 11566 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11567 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11568 | os_getresgid_impl(PyObject *module) |
| 11569 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11570 | { |
| 11571 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11572 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 11573 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11574 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 11575 | _PyLong_FromGid(egid), |
| 11576 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11577 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11578 | #endif /* HAVE_GETRESGID */ |
| 11579 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11580 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11581 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11582 | /*[clinic input] |
| 11583 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11584 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11585 | path: path_t(allow_fd=True) |
| 11586 | attribute: path_t |
| 11587 | * |
| 11588 | follow_symlinks: bool = True |
| 11589 | |
| 11590 | Return the value of extended attribute attribute on path. |
| 11591 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11592 | 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] | 11593 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11594 | link, getxattr will examine the symbolic link itself instead of the file |
| 11595 | the link points to. |
| 11596 | |
| 11597 | [clinic start generated code]*/ |
| 11598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11599 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11600 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11601 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11602 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11603 | { |
| 11604 | Py_ssize_t i; |
| 11605 | PyObject *buffer = NULL; |
| 11606 | |
| 11607 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 11608 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11609 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11610 | for (i = 0; ; i++) { |
| 11611 | void *ptr; |
| 11612 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11613 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11614 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11615 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11616 | path_error(path); |
| 11617 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11618 | } |
| 11619 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 11620 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11621 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11622 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11623 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11624 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11625 | if (path->fd >= 0) |
| 11626 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11627 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11628 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11629 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11630 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11631 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11632 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11633 | if (result < 0) { |
| 11634 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11635 | if (errno == ERANGE) |
| 11636 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11637 | path_error(path); |
| 11638 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11639 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11640 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11641 | if (result != buffer_size) { |
| 11642 | /* Can only shrink. */ |
| 11643 | _PyBytes_Resize(&buffer, result); |
| 11644 | } |
| 11645 | break; |
| 11646 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11647 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11648 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11649 | } |
| 11650 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11651 | |
| 11652 | /*[clinic input] |
| 11653 | os.setxattr |
| 11654 | |
| 11655 | path: path_t(allow_fd=True) |
| 11656 | attribute: path_t |
| 11657 | value: Py_buffer |
| 11658 | flags: int = 0 |
| 11659 | * |
| 11660 | follow_symlinks: bool = True |
| 11661 | |
| 11662 | Set extended attribute attribute on path to value. |
| 11663 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11664 | 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] | 11665 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11666 | link, setxattr will modify the symbolic link itself instead of the file |
| 11667 | the link points to. |
| 11668 | |
| 11669 | [clinic start generated code]*/ |
| 11670 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11671 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11672 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11673 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11674 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11675 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11676 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11677 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11678 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11679 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11680 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11681 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11682 | if (path->fd > -1) |
| 11683 | result = fsetxattr(path->fd, attribute->narrow, |
| 11684 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11685 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11686 | result = setxattr(path->narrow, attribute->narrow, |
| 11687 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11688 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11689 | result = lsetxattr(path->narrow, attribute->narrow, |
| 11690 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11691 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11692 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11693 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11694 | path_error(path); |
| 11695 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11696 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11697 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11698 | Py_RETURN_NONE; |
| 11699 | } |
| 11700 | |
| 11701 | |
| 11702 | /*[clinic input] |
| 11703 | os.removexattr |
| 11704 | |
| 11705 | path: path_t(allow_fd=True) |
| 11706 | attribute: path_t |
| 11707 | * |
| 11708 | follow_symlinks: bool = True |
| 11709 | |
| 11710 | Remove extended attribute attribute on path. |
| 11711 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11712 | 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] | 11713 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11714 | link, removexattr will modify the symbolic link itself instead of the file |
| 11715 | the link points to. |
| 11716 | |
| 11717 | [clinic start generated code]*/ |
| 11718 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11719 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11720 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11721 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11722 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11723 | { |
| 11724 | ssize_t result; |
| 11725 | |
| 11726 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 11727 | return NULL; |
| 11728 | |
| 11729 | Py_BEGIN_ALLOW_THREADS; |
| 11730 | if (path->fd > -1) |
| 11731 | result = fremovexattr(path->fd, attribute->narrow); |
| 11732 | else if (follow_symlinks) |
| 11733 | result = removexattr(path->narrow, attribute->narrow); |
| 11734 | else |
| 11735 | result = lremovexattr(path->narrow, attribute->narrow); |
| 11736 | Py_END_ALLOW_THREADS; |
| 11737 | |
| 11738 | if (result) { |
| 11739 | return path_error(path); |
| 11740 | } |
| 11741 | |
| 11742 | Py_RETURN_NONE; |
| 11743 | } |
| 11744 | |
| 11745 | |
| 11746 | /*[clinic input] |
| 11747 | os.listxattr |
| 11748 | |
| 11749 | path: path_t(allow_fd=True, nullable=True) = None |
| 11750 | * |
| 11751 | follow_symlinks: bool = True |
| 11752 | |
| 11753 | Return a list of extended attributes on path. |
| 11754 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11755 | 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] | 11756 | if path is None, listxattr will examine the current directory. |
| 11757 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11758 | link, listxattr will examine the symbolic link itself instead of the file |
| 11759 | the link points to. |
| 11760 | [clinic start generated code]*/ |
| 11761 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11762 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11763 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11764 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11765 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11766 | Py_ssize_t i; |
| 11767 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11768 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11769 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11770 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11771 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11772 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11773 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11774 | name = path->narrow ? path->narrow : "."; |
| 11775 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11776 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11777 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11778 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11779 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11780 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11781 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 11782 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11783 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11784 | break; |
| 11785 | } |
| 11786 | buffer = PyMem_MALLOC(buffer_size); |
| 11787 | if (!buffer) { |
| 11788 | PyErr_NoMemory(); |
| 11789 | break; |
| 11790 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11791 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11792 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11793 | if (path->fd > -1) |
| 11794 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11795 | else if (follow_symlinks) |
| 11796 | length = listxattr(name, buffer, buffer_size); |
| 11797 | else |
| 11798 | length = llistxattr(name, buffer, buffer_size); |
| 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 (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11802 | if (errno == ERANGE) { |
| 11803 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 11804 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11805 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11806 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11807 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11808 | break; |
| 11809 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11810 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11811 | result = PyList_New(0); |
| 11812 | if (!result) { |
| 11813 | goto exit; |
| 11814 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11815 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11816 | end = buffer + length; |
| 11817 | for (trace = start = buffer; trace != end; trace++) { |
| 11818 | if (!*trace) { |
| 11819 | int error; |
| 11820 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 11821 | trace - start); |
| 11822 | if (!attribute) { |
| 11823 | Py_DECREF(result); |
| 11824 | result = NULL; |
| 11825 | goto exit; |
| 11826 | } |
| 11827 | error = PyList_Append(result, attribute); |
| 11828 | Py_DECREF(attribute); |
| 11829 | if (error) { |
| 11830 | Py_DECREF(result); |
| 11831 | result = NULL; |
| 11832 | goto exit; |
| 11833 | } |
| 11834 | start = trace + 1; |
| 11835 | } |
| 11836 | } |
| 11837 | break; |
| 11838 | } |
| 11839 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11840 | if (buffer) |
| 11841 | PyMem_FREE(buffer); |
| 11842 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11843 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11844 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11845 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11847 | /*[clinic input] |
| 11848 | os.urandom |
| 11849 | |
| 11850 | size: Py_ssize_t |
| 11851 | / |
| 11852 | |
| 11853 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 11854 | [clinic start generated code]*/ |
| 11855 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11856 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11857 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 11858 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11859 | { |
| 11860 | PyObject *bytes; |
| 11861 | int result; |
| 11862 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11863 | if (size < 0) |
| 11864 | return PyErr_Format(PyExc_ValueError, |
| 11865 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11866 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 11867 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11868 | return NULL; |
| 11869 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 11870 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11871 | if (result == -1) { |
| 11872 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11873 | return NULL; |
| 11874 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11875 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11876 | } |
| 11877 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11878 | /* Terminal size querying */ |
| 11879 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 11880 | static PyTypeObject* TerminalSizeType; |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11881 | |
| 11882 | PyDoc_STRVAR(TerminalSize_docstring, |
| 11883 | "A tuple of (columns, lines) for holding terminal window size"); |
| 11884 | |
| 11885 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 11886 | {"columns", "width of the terminal window in characters"}, |
| 11887 | {"lines", "height of the terminal window in characters"}, |
| 11888 | {NULL, NULL} |
| 11889 | }; |
| 11890 | |
| 11891 | static PyStructSequence_Desc TerminalSize_desc = { |
| 11892 | "os.terminal_size", |
| 11893 | TerminalSize_docstring, |
| 11894 | TerminalSize_fields, |
| 11895 | 2, |
| 11896 | }; |
| 11897 | |
| 11898 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11899 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11900 | PyDoc_STRVAR(termsize__doc__, |
| 11901 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 11902 | "\n" \ |
| 11903 | "The optional argument fd (default standard output) specifies\n" \ |
| 11904 | "which file descriptor should be queried.\n" \ |
| 11905 | "\n" \ |
| 11906 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 11907 | "is thrown.\n" \ |
| 11908 | "\n" \ |
| 11909 | "This function will only be defined if an implementation is\n" \ |
| 11910 | "available for this system.\n" \ |
| 11911 | "\n" \ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 11912 | "shutil.get_terminal_size is the high-level function which should\n" \ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11913 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 11914 | |
| 11915 | static PyObject* |
| 11916 | get_terminal_size(PyObject *self, PyObject *args) |
| 11917 | { |
| 11918 | int columns, lines; |
| 11919 | PyObject *termsize; |
| 11920 | |
| 11921 | int fd = fileno(stdout); |
| 11922 | /* Under some conditions stdout may not be connected and |
| 11923 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 11924 | * GUI apps don't have valid standard streams by default. |
| 11925 | * |
| 11926 | * If this happens, and the optional fd argument is not present, |
| 11927 | * the ioctl below will fail returning EBADF. This is what we want. |
| 11928 | */ |
| 11929 | |
| 11930 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 11931 | return NULL; |
| 11932 | |
| 11933 | #ifdef TERMSIZE_USE_IOCTL |
| 11934 | { |
| 11935 | struct winsize w; |
| 11936 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 11937 | return PyErr_SetFromErrno(PyExc_OSError); |
| 11938 | columns = w.ws_col; |
| 11939 | lines = w.ws_row; |
| 11940 | } |
| 11941 | #endif /* TERMSIZE_USE_IOCTL */ |
| 11942 | |
| 11943 | #ifdef TERMSIZE_USE_CONIO |
| 11944 | { |
| 11945 | DWORD nhandle; |
| 11946 | HANDLE handle; |
| 11947 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 11948 | switch (fd) { |
| 11949 | case 0: nhandle = STD_INPUT_HANDLE; |
| 11950 | break; |
| 11951 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 11952 | break; |
| 11953 | case 2: nhandle = STD_ERROR_HANDLE; |
| 11954 | break; |
| 11955 | default: |
| 11956 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 11957 | } |
| 11958 | handle = GetStdHandle(nhandle); |
| 11959 | if (handle == NULL) |
| 11960 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 11961 | if (handle == INVALID_HANDLE_VALUE) |
| 11962 | return PyErr_SetFromWindowsErr(0); |
| 11963 | |
| 11964 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 11965 | return PyErr_SetFromWindowsErr(0); |
| 11966 | |
| 11967 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 11968 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 11969 | } |
| 11970 | #endif /* TERMSIZE_USE_CONIO */ |
| 11971 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 11972 | termsize = PyStructSequence_New(TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11973 | if (termsize == NULL) |
| 11974 | return NULL; |
| 11975 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 11976 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 11977 | if (PyErr_Occurred()) { |
| 11978 | Py_DECREF(termsize); |
| 11979 | return NULL; |
| 11980 | } |
| 11981 | return termsize; |
| 11982 | } |
| 11983 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 11984 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11985 | |
| 11986 | /*[clinic input] |
| 11987 | os.cpu_count |
| 11988 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 11989 | Return the number of CPUs in the system; return None if indeterminable. |
| 11990 | |
| 11991 | This number is not equivalent to the number of CPUs the current process can |
| 11992 | use. The number of usable CPUs can be obtained with |
| 11993 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11994 | [clinic start generated code]*/ |
| 11995 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11996 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11997 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 11998 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11999 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12000 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12001 | #ifdef MS_WINDOWS |
Christopher Wilcox | c67bae0 | 2017-08-30 05:01:08 -0400 | [diff] [blame] | 12002 | /* Vista is supported and the GetMaximumProcessorCount API is Win7+ |
| 12003 | Need to fallback to Vista behavior if this call isn't present */ |
| 12004 | HINSTANCE hKernel32; |
Christopher Wilcox | c67bae0 | 2017-08-30 05:01:08 -0400 | [diff] [blame] | 12005 | static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL; |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 12006 | Py_BEGIN_ALLOW_THREADS |
| 12007 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Christopher Wilcox | c67bae0 | 2017-08-30 05:01:08 -0400 | [diff] [blame] | 12008 | *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32, |
| 12009 | "GetMaximumProcessorCount"); |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 12010 | Py_END_ALLOW_THREADS |
Christopher Wilcox | c67bae0 | 2017-08-30 05:01:08 -0400 | [diff] [blame] | 12011 | if (_GetMaximumProcessorCount != NULL) { |
| 12012 | ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS); |
| 12013 | } |
| 12014 | else { |
| 12015 | SYSTEM_INFO sysinfo; |
| 12016 | GetSystemInfo(&sysinfo); |
| 12017 | ncpu = sysinfo.dwNumberOfProcessors; |
| 12018 | } |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12019 | #elif defined(__hpux) |
| 12020 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 12021 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 12022 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12023 | #elif defined(__DragonFly__) || \ |
| 12024 | defined(__OpenBSD__) || \ |
| 12025 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12026 | defined(__NetBSD__) || \ |
| 12027 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 12028 | int mib[2]; |
| 12029 | size_t len = sizeof(ncpu); |
| 12030 | mib[0] = CTL_HW; |
| 12031 | mib[1] = HW_NCPU; |
| 12032 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 12033 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12034 | #endif |
| 12035 | if (ncpu >= 1) |
| 12036 | return PyLong_FromLong(ncpu); |
| 12037 | else |
| 12038 | Py_RETURN_NONE; |
| 12039 | } |
| 12040 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12041 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12042 | /*[clinic input] |
| 12043 | os.get_inheritable -> bool |
| 12044 | |
| 12045 | fd: int |
| 12046 | / |
| 12047 | |
| 12048 | Get the close-on-exe flag of the specified file descriptor. |
| 12049 | [clinic start generated code]*/ |
| 12050 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12051 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12052 | os_get_inheritable_impl(PyObject *module, int fd) |
| 12053 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12054 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12055 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12056 | _Py_BEGIN_SUPPRESS_IPH |
| 12057 | return_value = _Py_get_inheritable(fd); |
| 12058 | _Py_END_SUPPRESS_IPH |
| 12059 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12060 | } |
| 12061 | |
| 12062 | |
| 12063 | /*[clinic input] |
| 12064 | os.set_inheritable |
| 12065 | fd: int |
| 12066 | inheritable: int |
| 12067 | / |
| 12068 | |
| 12069 | Set the inheritable flag of the specified file descriptor. |
| 12070 | [clinic start generated code]*/ |
| 12071 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12072 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12073 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 12074 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12075 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12076 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12077 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12078 | _Py_BEGIN_SUPPRESS_IPH |
| 12079 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 12080 | _Py_END_SUPPRESS_IPH |
| 12081 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12082 | return NULL; |
| 12083 | Py_RETURN_NONE; |
| 12084 | } |
| 12085 | |
| 12086 | |
| 12087 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12088 | /*[clinic input] |
| 12089 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12090 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12091 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12092 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12093 | Get the close-on-exe flag of the specified file descriptor. |
| 12094 | [clinic start generated code]*/ |
| 12095 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12096 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12097 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12098 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12099 | { |
| 12100 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12101 | |
| 12102 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 12103 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12104 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12105 | } |
| 12106 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12107 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12108 | } |
| 12109 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12110 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12111 | /*[clinic input] |
| 12112 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12113 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12114 | inheritable: bool |
| 12115 | / |
| 12116 | |
| 12117 | Set the inheritable flag of the specified handle. |
| 12118 | [clinic start generated code]*/ |
| 12119 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12120 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12121 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12122 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12123 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12124 | { |
| 12125 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12126 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 12127 | PyErr_SetFromWindowsErr(0); |
| 12128 | return NULL; |
| 12129 | } |
| 12130 | Py_RETURN_NONE; |
| 12131 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12132 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12133 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12134 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12135 | /*[clinic input] |
| 12136 | os.get_blocking -> bool |
| 12137 | fd: int |
| 12138 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12139 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12140 | Get the blocking mode of the file descriptor. |
| 12141 | |
| 12142 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 12143 | [clinic start generated code]*/ |
| 12144 | |
| 12145 | static int |
| 12146 | os_get_blocking_impl(PyObject *module, int fd) |
| 12147 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12148 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12149 | int blocking; |
| 12150 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12151 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12152 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12153 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12154 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12155 | } |
| 12156 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12157 | /*[clinic input] |
| 12158 | os.set_blocking |
| 12159 | fd: int |
| 12160 | blocking: bool(accept={int}) |
| 12161 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12162 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12163 | Set the blocking mode of the specified file descriptor. |
| 12164 | |
| 12165 | Set the O_NONBLOCK flag if blocking is False, |
| 12166 | clear the O_NONBLOCK flag otherwise. |
| 12167 | [clinic start generated code]*/ |
| 12168 | |
| 12169 | static PyObject * |
| 12170 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 12171 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12172 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12173 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12174 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12175 | _Py_BEGIN_SUPPRESS_IPH |
| 12176 | result = _Py_set_blocking(fd, blocking); |
| 12177 | _Py_END_SUPPRESS_IPH |
| 12178 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12179 | return NULL; |
| 12180 | Py_RETURN_NONE; |
| 12181 | } |
| 12182 | #endif /* !MS_WINDOWS */ |
| 12183 | |
| 12184 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12185 | /*[clinic input] |
| 12186 | class os.DirEntry "DirEntry *" "&DirEntryType" |
| 12187 | [clinic start generated code]*/ |
| 12188 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12189 | |
| 12190 | typedef struct { |
| 12191 | PyObject_HEAD |
| 12192 | PyObject *name; |
| 12193 | PyObject *path; |
| 12194 | PyObject *stat; |
| 12195 | PyObject *lstat; |
| 12196 | #ifdef MS_WINDOWS |
| 12197 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12198 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12199 | int got_file_index; |
| 12200 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12201 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12202 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12203 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12204 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12205 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12206 | #endif |
| 12207 | } DirEntry; |
| 12208 | |
| 12209 | static void |
| 12210 | DirEntry_dealloc(DirEntry *entry) |
| 12211 | { |
| 12212 | Py_XDECREF(entry->name); |
| 12213 | Py_XDECREF(entry->path); |
| 12214 | Py_XDECREF(entry->stat); |
| 12215 | Py_XDECREF(entry->lstat); |
| 12216 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 12217 | } |
| 12218 | |
| 12219 | /* Forward reference */ |
| 12220 | static int |
| 12221 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 12222 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12223 | /*[clinic input] |
| 12224 | os.DirEntry.is_symlink -> bool |
| 12225 | |
| 12226 | Return True if the entry is a symbolic link; cached per entry. |
| 12227 | [clinic start generated code]*/ |
| 12228 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12229 | static int |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12230 | os_DirEntry_is_symlink_impl(DirEntry *self) |
| 12231 | /*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12232 | { |
| 12233 | #ifdef MS_WINDOWS |
| 12234 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12235 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 12236 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12237 | if (self->d_type != DT_UNKNOWN) |
| 12238 | return self->d_type == DT_LNK; |
| 12239 | else |
| 12240 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12241 | #else |
| 12242 | /* POSIX without d_type */ |
| 12243 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12244 | #endif |
| 12245 | } |
| 12246 | |
| 12247 | static PyObject * |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12248 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 12249 | { |
| 12250 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12251 | STRUCT_STAT st; |
| 12252 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12253 | |
| 12254 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12255 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 12256 | return NULL; |
| 12257 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12258 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12259 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 12260 | return NULL; |
| 12261 | const char *path = PyBytes_AS_STRING(ub); |
| 12262 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 12263 | #ifdef HAVE_FSTATAT |
| 12264 | result = fstatat(self->dir_fd, path, &st, |
| 12265 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 12266 | #else |
| 12267 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 12268 | return NULL; |
| 12269 | #endif /* HAVE_FSTATAT */ |
| 12270 | } |
| 12271 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12272 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12273 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12274 | if (follow_symlinks) |
| 12275 | result = STAT(path, &st); |
| 12276 | else |
| 12277 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12278 | } |
| 12279 | Py_DECREF(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12280 | |
| 12281 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12282 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12283 | |
| 12284 | return _pystat_fromstructstat(&st); |
| 12285 | } |
| 12286 | |
| 12287 | static PyObject * |
| 12288 | DirEntry_get_lstat(DirEntry *self) |
| 12289 | { |
| 12290 | if (!self->lstat) { |
| 12291 | #ifdef MS_WINDOWS |
| 12292 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 12293 | #else /* POSIX */ |
| 12294 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 12295 | #endif |
| 12296 | } |
| 12297 | Py_XINCREF(self->lstat); |
| 12298 | return self->lstat; |
| 12299 | } |
| 12300 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12301 | /*[clinic input] |
| 12302 | os.DirEntry.stat |
| 12303 | * |
| 12304 | follow_symlinks: bool = True |
| 12305 | |
| 12306 | Return stat_result object for the entry; cached per entry. |
| 12307 | [clinic start generated code]*/ |
| 12308 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12309 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12310 | os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks) |
| 12311 | /*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12312 | { |
| 12313 | if (!follow_symlinks) |
| 12314 | return DirEntry_get_lstat(self); |
| 12315 | |
| 12316 | if (!self->stat) { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12317 | int result = os_DirEntry_is_symlink_impl(self); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12318 | if (result == -1) |
| 12319 | return NULL; |
| 12320 | else if (result) |
| 12321 | self->stat = DirEntry_fetch_stat(self, 1); |
| 12322 | else |
| 12323 | self->stat = DirEntry_get_lstat(self); |
| 12324 | } |
| 12325 | |
| 12326 | Py_XINCREF(self->stat); |
| 12327 | return self->stat; |
| 12328 | } |
| 12329 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12330 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 12331 | static int |
| 12332 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 12333 | { |
| 12334 | PyObject *stat = NULL; |
| 12335 | PyObject *st_mode = NULL; |
| 12336 | long mode; |
| 12337 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12338 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12339 | int is_symlink; |
| 12340 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12341 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12342 | #ifdef MS_WINDOWS |
| 12343 | unsigned long dir_bits; |
| 12344 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12345 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12346 | |
| 12347 | #ifdef MS_WINDOWS |
| 12348 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 12349 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12350 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12351 | is_symlink = self->d_type == DT_LNK; |
| 12352 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 12353 | #endif |
| 12354 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12355 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12356 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12357 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12358 | stat = os_DirEntry_stat_impl(self, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12359 | if (!stat) { |
| 12360 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 12361 | /* If file doesn't exist (anymore), then return False |
| 12362 | (i.e., say it's not a file/directory) */ |
| 12363 | PyErr_Clear(); |
| 12364 | return 0; |
| 12365 | } |
| 12366 | goto error; |
| 12367 | } |
| 12368 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 12369 | if (!st_mode) |
| 12370 | goto error; |
| 12371 | |
| 12372 | mode = PyLong_AsLong(st_mode); |
| 12373 | if (mode == -1 && PyErr_Occurred()) |
| 12374 | goto error; |
| 12375 | Py_CLEAR(st_mode); |
| 12376 | Py_CLEAR(stat); |
| 12377 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12378 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12379 | } |
| 12380 | else if (is_symlink) { |
| 12381 | assert(mode_bits != S_IFLNK); |
| 12382 | result = 0; |
| 12383 | } |
| 12384 | else { |
| 12385 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 12386 | #ifdef MS_WINDOWS |
| 12387 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 12388 | if (mode_bits == S_IFDIR) |
| 12389 | result = dir_bits != 0; |
| 12390 | else |
| 12391 | result = dir_bits == 0; |
| 12392 | #else /* POSIX */ |
| 12393 | if (mode_bits == S_IFDIR) |
| 12394 | result = self->d_type == DT_DIR; |
| 12395 | else |
| 12396 | result = self->d_type == DT_REG; |
| 12397 | #endif |
| 12398 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12399 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12400 | |
| 12401 | return result; |
| 12402 | |
| 12403 | error: |
| 12404 | Py_XDECREF(st_mode); |
| 12405 | Py_XDECREF(stat); |
| 12406 | return -1; |
| 12407 | } |
| 12408 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12409 | /*[clinic input] |
| 12410 | os.DirEntry.is_dir -> bool |
| 12411 | * |
| 12412 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12413 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12414 | Return True if the entry is a directory; cached per entry. |
| 12415 | [clinic start generated code]*/ |
| 12416 | |
| 12417 | static int |
| 12418 | os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks) |
| 12419 | /*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/ |
| 12420 | { |
| 12421 | return DirEntry_test_mode(self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12422 | } |
| 12423 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12424 | /*[clinic input] |
| 12425 | os.DirEntry.is_file -> bool |
| 12426 | * |
| 12427 | follow_symlinks: bool = True |
| 12428 | |
| 12429 | Return True if the entry is a file; cached per entry. |
| 12430 | [clinic start generated code]*/ |
| 12431 | |
| 12432 | static int |
| 12433 | os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks) |
| 12434 | /*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12435 | { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12436 | return DirEntry_test_mode(self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12437 | } |
| 12438 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12439 | /*[clinic input] |
| 12440 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12441 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12442 | Return inode of the entry; cached per entry. |
| 12443 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12444 | |
| 12445 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12446 | os_DirEntry_inode_impl(DirEntry *self) |
| 12447 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12448 | { |
| 12449 | #ifdef MS_WINDOWS |
| 12450 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12451 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12452 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12453 | STRUCT_STAT stat; |
| 12454 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12455 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12456 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12457 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12458 | path = PyUnicode_AsUnicode(unicode); |
| 12459 | result = LSTAT(path, &stat); |
| 12460 | Py_DECREF(unicode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12461 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12462 | if (result != 0) |
| 12463 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12464 | |
| 12465 | self->win32_file_index = stat.st_ino; |
| 12466 | self->got_file_index = 1; |
| 12467 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12468 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 12469 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12470 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 12471 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 12472 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12473 | #endif |
| 12474 | } |
| 12475 | |
| 12476 | static PyObject * |
| 12477 | DirEntry_repr(DirEntry *self) |
| 12478 | { |
| 12479 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 12480 | } |
| 12481 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12482 | /*[clinic input] |
| 12483 | os.DirEntry.__fspath__ |
| 12484 | |
| 12485 | Returns the path for the entry. |
| 12486 | [clinic start generated code]*/ |
| 12487 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12488 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12489 | os_DirEntry___fspath___impl(DirEntry *self) |
| 12490 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12491 | { |
| 12492 | Py_INCREF(self->path); |
| 12493 | return self->path; |
| 12494 | } |
| 12495 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12496 | static PyMemberDef DirEntry_members[] = { |
| 12497 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 12498 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 12499 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 12500 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 12501 | {NULL} |
| 12502 | }; |
| 12503 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12504 | #include "clinic/posixmodule.c.h" |
| 12505 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12506 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12507 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 12508 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 12509 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 12510 | OS_DIRENTRY_STAT_METHODDEF |
| 12511 | OS_DIRENTRY_INODE_METHODDEF |
| 12512 | OS_DIRENTRY___FSPATH___METHODDEF |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12513 | {NULL} |
| 12514 | }; |
| 12515 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12516 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12517 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12518 | MODNAME ".DirEntry", /* tp_name */ |
| 12519 | sizeof(DirEntry), /* tp_basicsize */ |
| 12520 | 0, /* tp_itemsize */ |
| 12521 | /* methods */ |
| 12522 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
| 12523 | 0, /* tp_print */ |
| 12524 | 0, /* tp_getattr */ |
| 12525 | 0, /* tp_setattr */ |
| 12526 | 0, /* tp_compare */ |
| 12527 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 12528 | 0, /* tp_as_number */ |
| 12529 | 0, /* tp_as_sequence */ |
| 12530 | 0, /* tp_as_mapping */ |
| 12531 | 0, /* tp_hash */ |
| 12532 | 0, /* tp_call */ |
| 12533 | 0, /* tp_str */ |
| 12534 | 0, /* tp_getattro */ |
| 12535 | 0, /* tp_setattro */ |
| 12536 | 0, /* tp_as_buffer */ |
| 12537 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 12538 | 0, /* tp_doc */ |
| 12539 | 0, /* tp_traverse */ |
| 12540 | 0, /* tp_clear */ |
| 12541 | 0, /* tp_richcompare */ |
| 12542 | 0, /* tp_weaklistoffset */ |
| 12543 | 0, /* tp_iter */ |
| 12544 | 0, /* tp_iternext */ |
| 12545 | DirEntry_methods, /* tp_methods */ |
| 12546 | DirEntry_members, /* tp_members */ |
| 12547 | }; |
| 12548 | |
| 12549 | #ifdef MS_WINDOWS |
| 12550 | |
| 12551 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12552 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12553 | { |
| 12554 | Py_ssize_t path_len; |
| 12555 | Py_ssize_t size; |
| 12556 | wchar_t *result; |
| 12557 | wchar_t ch; |
| 12558 | |
| 12559 | if (!path_wide) { /* Default arg: "." */ |
| 12560 | path_wide = L"."; |
| 12561 | path_len = 1; |
| 12562 | } |
| 12563 | else { |
| 12564 | path_len = wcslen(path_wide); |
| 12565 | } |
| 12566 | |
| 12567 | /* The +1's are for the path separator and the NUL */ |
| 12568 | size = path_len + 1 + wcslen(filename) + 1; |
| 12569 | result = PyMem_New(wchar_t, size); |
| 12570 | if (!result) { |
| 12571 | PyErr_NoMemory(); |
| 12572 | return NULL; |
| 12573 | } |
| 12574 | wcscpy(result, path_wide); |
| 12575 | if (path_len > 0) { |
| 12576 | ch = result[path_len - 1]; |
| 12577 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 12578 | result[path_len++] = SEP; |
| 12579 | wcscpy(result + path_len, filename); |
| 12580 | } |
| 12581 | return result; |
| 12582 | } |
| 12583 | |
| 12584 | static PyObject * |
| 12585 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 12586 | { |
| 12587 | DirEntry *entry; |
| 12588 | BY_HANDLE_FILE_INFORMATION file_info; |
| 12589 | ULONG reparse_tag; |
| 12590 | wchar_t *joined_path; |
| 12591 | |
| 12592 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 12593 | if (!entry) |
| 12594 | return NULL; |
| 12595 | entry->name = NULL; |
| 12596 | entry->path = NULL; |
| 12597 | entry->stat = NULL; |
| 12598 | entry->lstat = NULL; |
| 12599 | entry->got_file_index = 0; |
| 12600 | |
| 12601 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 12602 | if (!entry->name) |
| 12603 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12604 | if (path->narrow) { |
| 12605 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 12606 | if (!entry->name) |
| 12607 | goto error; |
| 12608 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12609 | |
| 12610 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 12611 | if (!joined_path) |
| 12612 | goto error; |
| 12613 | |
| 12614 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 12615 | PyMem_Free(joined_path); |
| 12616 | if (!entry->path) |
| 12617 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12618 | if (path->narrow) { |
| 12619 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 12620 | if (!entry->path) |
| 12621 | goto error; |
| 12622 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12623 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12624 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12625 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 12626 | |
| 12627 | return (PyObject *)entry; |
| 12628 | |
| 12629 | error: |
| 12630 | Py_DECREF(entry); |
| 12631 | return NULL; |
| 12632 | } |
| 12633 | |
| 12634 | #else /* POSIX */ |
| 12635 | |
| 12636 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12637 | 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] | 12638 | { |
| 12639 | Py_ssize_t path_len; |
| 12640 | Py_ssize_t size; |
| 12641 | char *result; |
| 12642 | |
| 12643 | if (!path_narrow) { /* Default arg: "." */ |
| 12644 | path_narrow = "."; |
| 12645 | path_len = 1; |
| 12646 | } |
| 12647 | else { |
| 12648 | path_len = strlen(path_narrow); |
| 12649 | } |
| 12650 | |
| 12651 | if (filename_len == -1) |
| 12652 | filename_len = strlen(filename); |
| 12653 | |
| 12654 | /* The +1's are for the path separator and the NUL */ |
| 12655 | size = path_len + 1 + filename_len + 1; |
| 12656 | result = PyMem_New(char, size); |
| 12657 | if (!result) { |
| 12658 | PyErr_NoMemory(); |
| 12659 | return NULL; |
| 12660 | } |
| 12661 | strcpy(result, path_narrow); |
| 12662 | if (path_len > 0 && result[path_len - 1] != '/') |
| 12663 | result[path_len++] = '/'; |
| 12664 | strcpy(result + path_len, filename); |
| 12665 | return result; |
| 12666 | } |
| 12667 | |
| 12668 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12669 | 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] | 12670 | ino_t d_ino |
| 12671 | #ifdef HAVE_DIRENT_D_TYPE |
| 12672 | , unsigned char d_type |
| 12673 | #endif |
| 12674 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12675 | { |
| 12676 | DirEntry *entry; |
| 12677 | char *joined_path; |
| 12678 | |
| 12679 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 12680 | if (!entry) |
| 12681 | return NULL; |
| 12682 | entry->name = NULL; |
| 12683 | entry->path = NULL; |
| 12684 | entry->stat = NULL; |
| 12685 | entry->lstat = NULL; |
| 12686 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12687 | if (path->fd != -1) { |
| 12688 | entry->dir_fd = path->fd; |
| 12689 | joined_path = NULL; |
| 12690 | } |
| 12691 | else { |
| 12692 | entry->dir_fd = DEFAULT_DIR_FD; |
| 12693 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 12694 | if (!joined_path) |
| 12695 | goto error; |
| 12696 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12697 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 12698 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12699 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12700 | if (joined_path) |
| 12701 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12702 | } |
| 12703 | else { |
| 12704 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12705 | if (joined_path) |
| 12706 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12707 | } |
| 12708 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12709 | if (!entry->name) |
| 12710 | goto error; |
| 12711 | |
| 12712 | if (path->fd != -1) { |
| 12713 | entry->path = entry->name; |
| 12714 | Py_INCREF(entry->path); |
| 12715 | } |
| 12716 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12717 | goto error; |
| 12718 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12719 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12720 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12721 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12722 | entry->d_ino = d_ino; |
| 12723 | |
| 12724 | return (PyObject *)entry; |
| 12725 | |
| 12726 | error: |
| 12727 | Py_XDECREF(entry); |
| 12728 | return NULL; |
| 12729 | } |
| 12730 | |
| 12731 | #endif |
| 12732 | |
| 12733 | |
| 12734 | typedef struct { |
| 12735 | PyObject_HEAD |
| 12736 | path_t path; |
| 12737 | #ifdef MS_WINDOWS |
| 12738 | HANDLE handle; |
| 12739 | WIN32_FIND_DATAW file_data; |
| 12740 | int first_time; |
| 12741 | #else /* POSIX */ |
| 12742 | DIR *dirp; |
| 12743 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12744 | #ifdef HAVE_FDOPENDIR |
| 12745 | int fd; |
| 12746 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12747 | } ScandirIterator; |
| 12748 | |
| 12749 | #ifdef MS_WINDOWS |
| 12750 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12751 | static int |
| 12752 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 12753 | { |
| 12754 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 12755 | } |
| 12756 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12757 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12758 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12759 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12760 | HANDLE handle = iterator->handle; |
| 12761 | |
| 12762 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12763 | return; |
| 12764 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12765 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12766 | Py_BEGIN_ALLOW_THREADS |
| 12767 | FindClose(handle); |
| 12768 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12769 | } |
| 12770 | |
| 12771 | static PyObject * |
| 12772 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12773 | { |
| 12774 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 12775 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12776 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12777 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12778 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12779 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12780 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12781 | |
| 12782 | while (1) { |
| 12783 | if (!iterator->first_time) { |
| 12784 | Py_BEGIN_ALLOW_THREADS |
| 12785 | success = FindNextFileW(iterator->handle, file_data); |
| 12786 | Py_END_ALLOW_THREADS |
| 12787 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12788 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12789 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12790 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12791 | break; |
| 12792 | } |
| 12793 | } |
| 12794 | iterator->first_time = 0; |
| 12795 | |
| 12796 | /* Skip over . and .. */ |
| 12797 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12798 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 12799 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 12800 | if (!entry) |
| 12801 | break; |
| 12802 | return entry; |
| 12803 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12804 | |
| 12805 | /* Loop till we get a non-dot directory or finish iterating */ |
| 12806 | } |
| 12807 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12808 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12809 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12810 | return NULL; |
| 12811 | } |
| 12812 | |
| 12813 | #else /* POSIX */ |
| 12814 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12815 | static int |
| 12816 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 12817 | { |
| 12818 | return !iterator->dirp; |
| 12819 | } |
| 12820 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12821 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12822 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12823 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12824 | DIR *dirp = iterator->dirp; |
| 12825 | |
| 12826 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12827 | return; |
| 12828 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12829 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12830 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12831 | #ifdef HAVE_FDOPENDIR |
| 12832 | if (iterator->path.fd != -1) |
| 12833 | rewinddir(dirp); |
| 12834 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12835 | closedir(dirp); |
| 12836 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12837 | return; |
| 12838 | } |
| 12839 | |
| 12840 | static PyObject * |
| 12841 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12842 | { |
| 12843 | struct dirent *direntp; |
| 12844 | Py_ssize_t name_len; |
| 12845 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12846 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12847 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12848 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12849 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12850 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12851 | |
| 12852 | while (1) { |
| 12853 | errno = 0; |
| 12854 | Py_BEGIN_ALLOW_THREADS |
| 12855 | direntp = readdir(iterator->dirp); |
| 12856 | Py_END_ALLOW_THREADS |
| 12857 | |
| 12858 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12859 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12860 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12861 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12862 | break; |
| 12863 | } |
| 12864 | |
| 12865 | /* Skip over . and .. */ |
| 12866 | name_len = NAMLEN(direntp); |
| 12867 | is_dot = direntp->d_name[0] == '.' && |
| 12868 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 12869 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12870 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12871 | name_len, direntp->d_ino |
| 12872 | #ifdef HAVE_DIRENT_D_TYPE |
| 12873 | , direntp->d_type |
| 12874 | #endif |
| 12875 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12876 | if (!entry) |
| 12877 | break; |
| 12878 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12879 | } |
| 12880 | |
| 12881 | /* Loop till we get a non-dot directory or finish iterating */ |
| 12882 | } |
| 12883 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12884 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12885 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12886 | return NULL; |
| 12887 | } |
| 12888 | |
| 12889 | #endif |
| 12890 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12891 | static PyObject * |
| 12892 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 12893 | { |
| 12894 | ScandirIterator_closedir(self); |
| 12895 | Py_RETURN_NONE; |
| 12896 | } |
| 12897 | |
| 12898 | static PyObject * |
| 12899 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 12900 | { |
| 12901 | Py_INCREF(self); |
| 12902 | return self; |
| 12903 | } |
| 12904 | |
| 12905 | static PyObject * |
| 12906 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 12907 | { |
| 12908 | ScandirIterator_closedir(self); |
| 12909 | Py_RETURN_NONE; |
| 12910 | } |
| 12911 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12912 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12913 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 12914 | { |
| 12915 | PyObject *error_type, *error_value, *error_traceback; |
| 12916 | |
| 12917 | /* Save the current exception, if any. */ |
| 12918 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 12919 | |
| 12920 | if (!ScandirIterator_is_closed(iterator)) { |
| 12921 | ScandirIterator_closedir(iterator); |
| 12922 | |
| 12923 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 12924 | "unclosed scandir iterator %R", iterator)) { |
| 12925 | /* Spurious errors can appear at shutdown */ |
| 12926 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 12927 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 12928 | } |
| 12929 | } |
| 12930 | } |
| 12931 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12932 | path_cleanup(&iterator->path); |
| 12933 | |
| 12934 | /* Restore the saved exception. */ |
| 12935 | PyErr_Restore(error_type, error_value, error_traceback); |
| 12936 | } |
| 12937 | |
| 12938 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12939 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 12940 | { |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12941 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 12942 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12943 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12944 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 12945 | } |
| 12946 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12947 | static PyMethodDef ScandirIterator_methods[] = { |
| 12948 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 12949 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 12950 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 12951 | {NULL} |
| 12952 | }; |
| 12953 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12954 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12955 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12956 | MODNAME ".ScandirIterator", /* tp_name */ |
| 12957 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 12958 | 0, /* tp_itemsize */ |
| 12959 | /* methods */ |
| 12960 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
| 12961 | 0, /* tp_print */ |
| 12962 | 0, /* tp_getattr */ |
| 12963 | 0, /* tp_setattr */ |
| 12964 | 0, /* tp_compare */ |
| 12965 | 0, /* tp_repr */ |
| 12966 | 0, /* tp_as_number */ |
| 12967 | 0, /* tp_as_sequence */ |
| 12968 | 0, /* tp_as_mapping */ |
| 12969 | 0, /* tp_hash */ |
| 12970 | 0, /* tp_call */ |
| 12971 | 0, /* tp_str */ |
| 12972 | 0, /* tp_getattro */ |
| 12973 | 0, /* tp_setattro */ |
| 12974 | 0, /* tp_as_buffer */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12975 | Py_TPFLAGS_DEFAULT |
| 12976 | | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12977 | 0, /* tp_doc */ |
| 12978 | 0, /* tp_traverse */ |
| 12979 | 0, /* tp_clear */ |
| 12980 | 0, /* tp_richcompare */ |
| 12981 | 0, /* tp_weaklistoffset */ |
| 12982 | PyObject_SelfIter, /* tp_iter */ |
| 12983 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12984 | ScandirIterator_methods, /* tp_methods */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12985 | 0, /* tp_members */ |
| 12986 | 0, /* tp_getset */ |
| 12987 | 0, /* tp_base */ |
| 12988 | 0, /* tp_dict */ |
| 12989 | 0, /* tp_descr_get */ |
| 12990 | 0, /* tp_descr_set */ |
| 12991 | 0, /* tp_dictoffset */ |
| 12992 | 0, /* tp_init */ |
| 12993 | 0, /* tp_alloc */ |
| 12994 | 0, /* tp_new */ |
| 12995 | 0, /* tp_free */ |
| 12996 | 0, /* tp_is_gc */ |
| 12997 | 0, /* tp_bases */ |
| 12998 | 0, /* tp_mro */ |
| 12999 | 0, /* tp_cache */ |
| 13000 | 0, /* tp_subclasses */ |
| 13001 | 0, /* tp_weaklist */ |
| 13002 | 0, /* tp_del */ |
| 13003 | 0, /* tp_version_tag */ |
| 13004 | (destructor)ScandirIterator_finalize, /* tp_finalize */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13005 | }; |
| 13006 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13007 | /*[clinic input] |
| 13008 | os.scandir |
| 13009 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13010 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13011 | |
| 13012 | Return an iterator of DirEntry objects for given path. |
| 13013 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13014 | 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] | 13015 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 13016 | all other circumstances they will be str. |
| 13017 | |
| 13018 | If path is None, uses the path='.'. |
| 13019 | [clinic start generated code]*/ |
| 13020 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13021 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13022 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13023 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13024 | { |
| 13025 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13026 | #ifdef MS_WINDOWS |
| 13027 | wchar_t *path_strW; |
| 13028 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13029 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13030 | #ifdef HAVE_FDOPENDIR |
| 13031 | int fd = -1; |
| 13032 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13033 | #endif |
| 13034 | |
| 13035 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 13036 | if (!iterator) |
| 13037 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13038 | |
| 13039 | #ifdef MS_WINDOWS |
| 13040 | iterator->handle = INVALID_HANDLE_VALUE; |
| 13041 | #else |
| 13042 | iterator->dirp = NULL; |
| 13043 | #endif |
| 13044 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13045 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 13046 | /* Move the ownership to iterator->path */ |
| 13047 | path->object = NULL; |
| 13048 | path->cleanup = NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13049 | |
| 13050 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13051 | iterator->first_time = 1; |
| 13052 | |
| 13053 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 13054 | if (!path_strW) |
| 13055 | goto error; |
| 13056 | |
| 13057 | Py_BEGIN_ALLOW_THREADS |
| 13058 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 13059 | Py_END_ALLOW_THREADS |
| 13060 | |
| 13061 | PyMem_Free(path_strW); |
| 13062 | |
| 13063 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 13064 | path_error(&iterator->path); |
| 13065 | goto error; |
| 13066 | } |
| 13067 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13068 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13069 | #ifdef HAVE_FDOPENDIR |
| 13070 | if (path->fd != -1) { |
| 13071 | /* closedir() closes the FD, so we duplicate it */ |
| 13072 | fd = _Py_dup(path->fd); |
| 13073 | if (fd == -1) |
| 13074 | goto error; |
| 13075 | |
| 13076 | Py_BEGIN_ALLOW_THREADS |
| 13077 | iterator->dirp = fdopendir(fd); |
| 13078 | Py_END_ALLOW_THREADS |
| 13079 | } |
| 13080 | else |
| 13081 | #endif |
| 13082 | { |
| 13083 | if (iterator->path.narrow) |
| 13084 | path_str = iterator->path.narrow; |
| 13085 | else |
| 13086 | path_str = "."; |
| 13087 | |
| 13088 | Py_BEGIN_ALLOW_THREADS |
| 13089 | iterator->dirp = opendir(path_str); |
| 13090 | Py_END_ALLOW_THREADS |
| 13091 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13092 | |
| 13093 | if (!iterator->dirp) { |
| 13094 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13095 | #ifdef HAVE_FDOPENDIR |
| 13096 | if (fd != -1) { |
| 13097 | Py_BEGIN_ALLOW_THREADS |
| 13098 | close(fd); |
| 13099 | Py_END_ALLOW_THREADS |
| 13100 | } |
| 13101 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13102 | goto error; |
| 13103 | } |
| 13104 | #endif |
| 13105 | |
| 13106 | return (PyObject *)iterator; |
| 13107 | |
| 13108 | error: |
| 13109 | Py_DECREF(iterator); |
| 13110 | return NULL; |
| 13111 | } |
| 13112 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13113 | /* |
| 13114 | Return the file system path representation of the object. |
| 13115 | |
| 13116 | If the object is str or bytes, then allow it to pass through with |
| 13117 | an incremented refcount. If the object defines __fspath__(), then |
| 13118 | return the result of that method. All other types raise a TypeError. |
| 13119 | */ |
| 13120 | PyObject * |
| 13121 | PyOS_FSPath(PyObject *path) |
| 13122 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 13123 | /* For error message reasons, this function is manually inlined in |
| 13124 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13125 | _Py_IDENTIFIER(__fspath__); |
| 13126 | PyObject *func = NULL; |
| 13127 | PyObject *path_repr = NULL; |
| 13128 | |
| 13129 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 13130 | Py_INCREF(path); |
| 13131 | return path; |
| 13132 | } |
| 13133 | |
| 13134 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 13135 | if (NULL == func) { |
| 13136 | return PyErr_Format(PyExc_TypeError, |
| 13137 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13138 | "not %.200s", |
| 13139 | Py_TYPE(path)->tp_name); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13140 | } |
| 13141 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 13142 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13143 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 13144 | if (NULL == path_repr) { |
| 13145 | return NULL; |
| 13146 | } |
| 13147 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13148 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 13149 | PyErr_Format(PyExc_TypeError, |
| 13150 | "expected %.200s.__fspath__() to return str or bytes, " |
| 13151 | "not %.200s", Py_TYPE(path)->tp_name, |
| 13152 | Py_TYPE(path_repr)->tp_name); |
| 13153 | Py_DECREF(path_repr); |
| 13154 | return NULL; |
| 13155 | } |
| 13156 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13157 | return path_repr; |
| 13158 | } |
| 13159 | |
| 13160 | /*[clinic input] |
| 13161 | os.fspath |
| 13162 | |
| 13163 | path: object |
| 13164 | |
| 13165 | Return the file system path representation of the object. |
| 13166 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 13167 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 13168 | object defines __fspath__(), then return the result of that method. All other |
| 13169 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13170 | [clinic start generated code]*/ |
| 13171 | |
| 13172 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 13173 | os_fspath_impl(PyObject *module, PyObject *path) |
| 13174 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13175 | { |
| 13176 | return PyOS_FSPath(path); |
| 13177 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13178 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13179 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13180 | /*[clinic input] |
| 13181 | os.getrandom |
| 13182 | |
| 13183 | size: Py_ssize_t |
| 13184 | flags: int=0 |
| 13185 | |
| 13186 | Obtain a series of random bytes. |
| 13187 | [clinic start generated code]*/ |
| 13188 | |
| 13189 | static PyObject * |
| 13190 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 13191 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 13192 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13193 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13194 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13195 | |
| 13196 | if (size < 0) { |
| 13197 | errno = EINVAL; |
| 13198 | return posix_error(); |
| 13199 | } |
| 13200 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13201 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 13202 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13203 | PyErr_NoMemory(); |
| 13204 | return NULL; |
| 13205 | } |
| 13206 | |
| 13207 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13208 | n = syscall(SYS_getrandom, |
| 13209 | PyBytes_AS_STRING(bytes), |
| 13210 | PyBytes_GET_SIZE(bytes), |
| 13211 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13212 | if (n < 0 && errno == EINTR) { |
| 13213 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13214 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13215 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13216 | |
| 13217 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13218 | continue; |
| 13219 | } |
| 13220 | break; |
| 13221 | } |
| 13222 | |
| 13223 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13224 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13225 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13226 | } |
| 13227 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13228 | if (n != size) { |
| 13229 | _PyBytes_Resize(&bytes, n); |
| 13230 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13231 | |
| 13232 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13233 | |
| 13234 | error: |
| 13235 | Py_DECREF(bytes); |
| 13236 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13237 | } |
| 13238 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 13239 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13240 | #ifdef MS_WINDOWS |
| 13241 | /* bpo-36085: Helper functions for managing DLL search directories |
| 13242 | * on win32 |
| 13243 | */ |
| 13244 | |
| 13245 | typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory); |
| 13246 | typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie); |
| 13247 | |
| 13248 | /*[clinic input] |
| 13249 | os._add_dll_directory |
| 13250 | |
| 13251 | path: path_t |
| 13252 | |
| 13253 | Add a path to the DLL search path. |
| 13254 | |
| 13255 | This search path is used when resolving dependencies for imported |
| 13256 | extension modules (the module itself is resolved through sys.path), |
| 13257 | and also by ctypes. |
| 13258 | |
| 13259 | Returns an opaque value that may be passed to os.remove_dll_directory |
| 13260 | to remove this directory from the search path. |
| 13261 | [clinic start generated code]*/ |
| 13262 | |
| 13263 | static PyObject * |
| 13264 | os__add_dll_directory_impl(PyObject *module, path_t *path) |
| 13265 | /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ |
| 13266 | { |
| 13267 | HMODULE hKernel32; |
| 13268 | PAddDllDirectory AddDllDirectory; |
| 13269 | DLL_DIRECTORY_COOKIE cookie = 0; |
| 13270 | DWORD err = 0; |
| 13271 | |
| 13272 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13273 | infrequent operation, just do it each time. Kernel32 is always |
| 13274 | loaded. */ |
| 13275 | Py_BEGIN_ALLOW_THREADS |
| 13276 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13277 | !(AddDllDirectory = (PAddDllDirectory)GetProcAddress( |
| 13278 | hKernel32, "AddDllDirectory")) || |
| 13279 | !(cookie = (*AddDllDirectory)(path->wide))) { |
| 13280 | err = GetLastError(); |
| 13281 | } |
| 13282 | Py_END_ALLOW_THREADS |
| 13283 | |
| 13284 | if (err) { |
| 13285 | return win32_error_object_err("add_dll_directory", |
| 13286 | path->object, err); |
| 13287 | } |
| 13288 | |
| 13289 | return PyCapsule_New(cookie, "DLL directory cookie", NULL); |
| 13290 | } |
| 13291 | |
| 13292 | /*[clinic input] |
| 13293 | os._remove_dll_directory |
| 13294 | |
| 13295 | cookie: object |
| 13296 | |
| 13297 | Removes a path from the DLL search path. |
| 13298 | |
| 13299 | The parameter is an opaque value that was returned from |
| 13300 | os.add_dll_directory. You can only remove directories that you added |
| 13301 | yourself. |
| 13302 | [clinic start generated code]*/ |
| 13303 | |
| 13304 | static PyObject * |
| 13305 | os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) |
| 13306 | /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ |
| 13307 | { |
| 13308 | HMODULE hKernel32; |
| 13309 | PRemoveDllDirectory RemoveDllDirectory; |
| 13310 | DLL_DIRECTORY_COOKIE cookieValue; |
| 13311 | DWORD err = 0; |
| 13312 | |
| 13313 | if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) { |
| 13314 | PyErr_SetString(PyExc_TypeError, |
| 13315 | "Provided cookie was not returned from os.add_dll_directory"); |
| 13316 | return NULL; |
| 13317 | } |
| 13318 | |
| 13319 | cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( |
| 13320 | cookie, "DLL directory cookie"); |
| 13321 | |
| 13322 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13323 | infrequent operation, just do it each time. Kernel32 is always |
| 13324 | loaded. */ |
| 13325 | Py_BEGIN_ALLOW_THREADS |
| 13326 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13327 | !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress( |
| 13328 | hKernel32, "RemoveDllDirectory")) || |
| 13329 | !(*RemoveDllDirectory)(cookieValue)) { |
| 13330 | err = GetLastError(); |
| 13331 | } |
| 13332 | Py_END_ALLOW_THREADS |
| 13333 | |
| 13334 | if (err) { |
| 13335 | return win32_error_object_err("remove_dll_directory", |
| 13336 | NULL, err); |
| 13337 | } |
| 13338 | |
| 13339 | if (PyCapsule_SetName(cookie, NULL)) { |
| 13340 | return NULL; |
| 13341 | } |
| 13342 | |
| 13343 | Py_RETURN_NONE; |
| 13344 | } |
| 13345 | |
| 13346 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13347 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13348 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13349 | |
| 13350 | OS_STAT_METHODDEF |
| 13351 | OS_ACCESS_METHODDEF |
| 13352 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13353 | OS_CHDIR_METHODDEF |
| 13354 | OS_CHFLAGS_METHODDEF |
| 13355 | OS_CHMOD_METHODDEF |
| 13356 | OS_FCHMOD_METHODDEF |
| 13357 | OS_LCHMOD_METHODDEF |
| 13358 | OS_CHOWN_METHODDEF |
| 13359 | OS_FCHOWN_METHODDEF |
| 13360 | OS_LCHOWN_METHODDEF |
| 13361 | OS_LCHFLAGS_METHODDEF |
| 13362 | OS_CHROOT_METHODDEF |
| 13363 | OS_CTERMID_METHODDEF |
| 13364 | OS_GETCWD_METHODDEF |
| 13365 | OS_GETCWDB_METHODDEF |
| 13366 | OS_LINK_METHODDEF |
| 13367 | OS_LISTDIR_METHODDEF |
| 13368 | OS_LSTAT_METHODDEF |
| 13369 | OS_MKDIR_METHODDEF |
| 13370 | OS_NICE_METHODDEF |
| 13371 | OS_GETPRIORITY_METHODDEF |
| 13372 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13373 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 13374 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13375 | OS_READLINK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13376 | OS_RENAME_METHODDEF |
| 13377 | OS_REPLACE_METHODDEF |
| 13378 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13379 | OS_SYMLINK_METHODDEF |
| 13380 | OS_SYSTEM_METHODDEF |
| 13381 | OS_UMASK_METHODDEF |
| 13382 | OS_UNAME_METHODDEF |
| 13383 | OS_UNLINK_METHODDEF |
| 13384 | OS_REMOVE_METHODDEF |
| 13385 | OS_UTIME_METHODDEF |
| 13386 | OS_TIMES_METHODDEF |
| 13387 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 13388 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13389 | OS_EXECV_METHODDEF |
| 13390 | OS_EXECVE_METHODDEF |
| 13391 | OS_SPAWNV_METHODDEF |
| 13392 | OS_SPAWNVE_METHODDEF |
| 13393 | OS_FORK1_METHODDEF |
| 13394 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 13395 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13396 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 13397 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 13398 | OS_SCHED_GETPARAM_METHODDEF |
| 13399 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 13400 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 13401 | OS_SCHED_SETPARAM_METHODDEF |
| 13402 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 13403 | OS_SCHED_YIELD_METHODDEF |
| 13404 | OS_SCHED_SETAFFINITY_METHODDEF |
| 13405 | OS_SCHED_GETAFFINITY_METHODDEF |
| 13406 | OS_OPENPTY_METHODDEF |
| 13407 | OS_FORKPTY_METHODDEF |
| 13408 | OS_GETEGID_METHODDEF |
| 13409 | OS_GETEUID_METHODDEF |
| 13410 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 13411 | #ifdef HAVE_GETGROUPLIST |
| 13412 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 13413 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13414 | OS_GETGROUPS_METHODDEF |
| 13415 | OS_GETPID_METHODDEF |
| 13416 | OS_GETPGRP_METHODDEF |
| 13417 | OS_GETPPID_METHODDEF |
| 13418 | OS_GETUID_METHODDEF |
| 13419 | OS_GETLOGIN_METHODDEF |
| 13420 | OS_KILL_METHODDEF |
| 13421 | OS_KILLPG_METHODDEF |
| 13422 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13423 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13424 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13425 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13426 | OS_SETUID_METHODDEF |
| 13427 | OS_SETEUID_METHODDEF |
| 13428 | OS_SETREUID_METHODDEF |
| 13429 | OS_SETGID_METHODDEF |
| 13430 | OS_SETEGID_METHODDEF |
| 13431 | OS_SETREGID_METHODDEF |
| 13432 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13433 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13434 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13435 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13436 | OS_GETPGID_METHODDEF |
| 13437 | OS_SETPGRP_METHODDEF |
| 13438 | OS_WAIT_METHODDEF |
| 13439 | OS_WAIT3_METHODDEF |
| 13440 | OS_WAIT4_METHODDEF |
| 13441 | OS_WAITID_METHODDEF |
| 13442 | OS_WAITPID_METHODDEF |
| 13443 | OS_GETSID_METHODDEF |
| 13444 | OS_SETSID_METHODDEF |
| 13445 | OS_SETPGID_METHODDEF |
| 13446 | OS_TCGETPGRP_METHODDEF |
| 13447 | OS_TCSETPGRP_METHODDEF |
| 13448 | OS_OPEN_METHODDEF |
| 13449 | OS_CLOSE_METHODDEF |
| 13450 | OS_CLOSERANGE_METHODDEF |
| 13451 | OS_DEVICE_ENCODING_METHODDEF |
| 13452 | OS_DUP_METHODDEF |
| 13453 | OS_DUP2_METHODDEF |
| 13454 | OS_LOCKF_METHODDEF |
| 13455 | OS_LSEEK_METHODDEF |
| 13456 | OS_READ_METHODDEF |
| 13457 | OS_READV_METHODDEF |
| 13458 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13459 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13460 | OS_WRITE_METHODDEF |
| 13461 | OS_WRITEV_METHODDEF |
| 13462 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13463 | OS_PWRITEV_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13464 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 13465 | {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13466 | posix_sendfile__doc__}, |
| 13467 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13468 | OS_FSTAT_METHODDEF |
| 13469 | OS_ISATTY_METHODDEF |
| 13470 | OS_PIPE_METHODDEF |
| 13471 | OS_PIPE2_METHODDEF |
| 13472 | OS_MKFIFO_METHODDEF |
| 13473 | OS_MKNOD_METHODDEF |
| 13474 | OS_MAJOR_METHODDEF |
| 13475 | OS_MINOR_METHODDEF |
| 13476 | OS_MAKEDEV_METHODDEF |
| 13477 | OS_FTRUNCATE_METHODDEF |
| 13478 | OS_TRUNCATE_METHODDEF |
| 13479 | OS_POSIX_FALLOCATE_METHODDEF |
| 13480 | OS_POSIX_FADVISE_METHODDEF |
| 13481 | OS_PUTENV_METHODDEF |
| 13482 | OS_UNSETENV_METHODDEF |
| 13483 | OS_STRERROR_METHODDEF |
| 13484 | OS_FCHDIR_METHODDEF |
| 13485 | OS_FSYNC_METHODDEF |
| 13486 | OS_SYNC_METHODDEF |
| 13487 | OS_FDATASYNC_METHODDEF |
| 13488 | OS_WCOREDUMP_METHODDEF |
| 13489 | OS_WIFCONTINUED_METHODDEF |
| 13490 | OS_WIFSTOPPED_METHODDEF |
| 13491 | OS_WIFSIGNALED_METHODDEF |
| 13492 | OS_WIFEXITED_METHODDEF |
| 13493 | OS_WEXITSTATUS_METHODDEF |
| 13494 | OS_WTERMSIG_METHODDEF |
| 13495 | OS_WSTOPSIG_METHODDEF |
| 13496 | OS_FSTATVFS_METHODDEF |
| 13497 | OS_STATVFS_METHODDEF |
| 13498 | OS_CONFSTR_METHODDEF |
| 13499 | OS_SYSCONF_METHODDEF |
| 13500 | OS_FPATHCONF_METHODDEF |
| 13501 | OS_PATHCONF_METHODDEF |
| 13502 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 13503 | OS__GETFULLPATHNAME_METHODDEF |
| 13504 | OS__ISDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13505 | OS__GETDISKUSAGE_METHODDEF |
| 13506 | OS__GETFINALPATHNAME_METHODDEF |
| 13507 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 13508 | OS_GETLOADAVG_METHODDEF |
| 13509 | OS_URANDOM_METHODDEF |
| 13510 | OS_SETRESUID_METHODDEF |
| 13511 | OS_SETRESGID_METHODDEF |
| 13512 | OS_GETRESUID_METHODDEF |
| 13513 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 13514 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13515 | OS_GETXATTR_METHODDEF |
| 13516 | OS_SETXATTR_METHODDEF |
| 13517 | OS_REMOVEXATTR_METHODDEF |
| 13518 | OS_LISTXATTR_METHODDEF |
| 13519 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13520 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 13521 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 13522 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13523 | OS_CPU_COUNT_METHODDEF |
| 13524 | OS_GET_INHERITABLE_METHODDEF |
| 13525 | OS_SET_INHERITABLE_METHODDEF |
| 13526 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 13527 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13528 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13529 | OS_GET_BLOCKING_METHODDEF |
| 13530 | OS_SET_BLOCKING_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13531 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13532 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13533 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13534 | OS_GETRANDOM_METHODDEF |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13535 | #ifdef MS_WINDOWS |
| 13536 | OS__ADD_DLL_DIRECTORY_METHODDEF |
| 13537 | OS__REMOVE_DLL_DIRECTORY_METHODDEF |
| 13538 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13539 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13540 | }; |
| 13541 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13542 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13543 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13544 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13545 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13546 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13547 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13548 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13549 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13550 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13551 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13552 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13553 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13554 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13555 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13556 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13557 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13558 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13559 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13560 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13561 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13562 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13563 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13564 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13565 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13566 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13567 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13568 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13569 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13570 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13571 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13572 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13573 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13574 | #endif |
| 13575 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13576 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13577 | #endif |
| 13578 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13579 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13580 | #endif |
| 13581 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13582 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13583 | #endif |
| 13584 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13585 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13586 | #endif |
| 13587 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13588 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13589 | #endif |
| 13590 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13591 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13592 | #endif |
| 13593 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13594 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13595 | #endif |
| 13596 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13597 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13598 | #endif |
| 13599 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13600 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13601 | #endif |
| 13602 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13603 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13604 | #endif |
| 13605 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13606 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13607 | #endif |
| 13608 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13609 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13610 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13611 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13612 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13613 | #endif |
| 13614 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13615 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13616 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13617 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13618 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13619 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13620 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13621 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13622 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13623 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13624 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13625 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13626 | #endif |
| 13627 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13628 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13629 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13630 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13631 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13632 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13633 | #endif |
| 13634 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13635 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13636 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13637 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13638 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13639 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13640 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13641 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13642 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 13643 | #ifdef O_TMPFILE |
| 13644 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 13645 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13646 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13647 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13648 | #endif |
| 13649 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13650 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13651 | #endif |
| 13652 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13653 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13654 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13655 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13656 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13657 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13658 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13659 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13660 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13661 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13662 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13663 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13664 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13665 | #endif |
| 13666 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13667 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13668 | #endif |
| 13669 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13670 | /* MS Windows */ |
| 13671 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13672 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13673 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13674 | #endif |
| 13675 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13676 | /* Optimize for short life (keep in memory). */ |
| 13677 | /* 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] | 13678 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13679 | #endif |
| 13680 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13681 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13682 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13683 | #endif |
| 13684 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13685 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13686 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13687 | #endif |
| 13688 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13689 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13690 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13691 | #endif |
| 13692 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13693 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13694 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13695 | /* Send a SIGIO signal whenever input or output |
| 13696 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13697 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13698 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13699 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13700 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13701 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13702 | #endif |
| 13703 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13704 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13705 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13706 | #endif |
| 13707 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13708 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13709 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13710 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13711 | #ifdef O_NOLINKS |
| 13712 | /* 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] | 13713 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13714 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13715 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13716 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13717 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13718 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 13719 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13720 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13721 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13722 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13723 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13724 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13725 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13726 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13727 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13728 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13729 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13730 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13731 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13732 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13733 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13734 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13735 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13736 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13737 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13738 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13739 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13740 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13741 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13742 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13743 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13744 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13745 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13746 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13747 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13748 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13749 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13750 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13751 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13752 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13753 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13754 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13755 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13756 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13757 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13758 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13759 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13760 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13761 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13762 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13763 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13764 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13765 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13766 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13767 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13768 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13769 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13770 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13771 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13772 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 13773 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13774 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13775 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13776 | #endif /* ST_RDONLY */ |
| 13777 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13778 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13779 | #endif /* ST_NOSUID */ |
| 13780 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 13781 | /* GNU extensions */ |
| 13782 | #ifdef ST_NODEV |
| 13783 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 13784 | #endif /* ST_NODEV */ |
| 13785 | #ifdef ST_NOEXEC |
| 13786 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 13787 | #endif /* ST_NOEXEC */ |
| 13788 | #ifdef ST_SYNCHRONOUS |
| 13789 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 13790 | #endif /* ST_SYNCHRONOUS */ |
| 13791 | #ifdef ST_MANDLOCK |
| 13792 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 13793 | #endif /* ST_MANDLOCK */ |
| 13794 | #ifdef ST_WRITE |
| 13795 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 13796 | #endif /* ST_WRITE */ |
| 13797 | #ifdef ST_APPEND |
| 13798 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 13799 | #endif /* ST_APPEND */ |
| 13800 | #ifdef ST_NOATIME |
| 13801 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 13802 | #endif /* ST_NOATIME */ |
| 13803 | #ifdef ST_NODIRATIME |
| 13804 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 13805 | #endif /* ST_NODIRATIME */ |
| 13806 | #ifdef ST_RELATIME |
| 13807 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 13808 | #endif /* ST_RELATIME */ |
| 13809 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13810 | /* FreeBSD sendfile() constants */ |
| 13811 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13812 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13813 | #endif |
| 13814 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13815 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13816 | #endif |
| 13817 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13818 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13819 | #endif |
| 13820 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13821 | /* constants for posix_fadvise */ |
| 13822 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13823 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13824 | #endif |
| 13825 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13826 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13827 | #endif |
| 13828 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13829 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13830 | #endif |
| 13831 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13832 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13833 | #endif |
| 13834 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13835 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13836 | #endif |
| 13837 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13838 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13839 | #endif |
| 13840 | |
| 13841 | /* constants for waitid */ |
| 13842 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13843 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 13844 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 13845 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13846 | #endif |
| 13847 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13848 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13849 | #endif |
| 13850 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13851 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13852 | #endif |
| 13853 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13854 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13855 | #endif |
| 13856 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13857 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13858 | #endif |
| 13859 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13860 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13861 | #endif |
| 13862 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13863 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13864 | #endif |
| 13865 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13866 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13867 | #endif |
| 13868 | |
| 13869 | /* constants for lockf */ |
| 13870 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13871 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13872 | #endif |
| 13873 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13874 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13875 | #endif |
| 13876 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13877 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13878 | #endif |
| 13879 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13880 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13881 | #endif |
| 13882 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13883 | #ifdef RWF_DSYNC |
| 13884 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 13885 | #endif |
| 13886 | #ifdef RWF_HIPRI |
| 13887 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 13888 | #endif |
| 13889 | #ifdef RWF_SYNC |
| 13890 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 13891 | #endif |
| 13892 | #ifdef RWF_NOWAIT |
| 13893 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 13894 | #endif |
| 13895 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13896 | /* constants for posix_spawn */ |
| 13897 | #ifdef HAVE_POSIX_SPAWN |
| 13898 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 13899 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 13900 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 13901 | #endif |
| 13902 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 13903 | #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13904 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 13905 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13906 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 13907 | #endif |
| 13908 | #ifdef HAVE_SPAWNV |
| 13909 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13910 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 13911 | #endif |
| 13912 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13913 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13914 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13915 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13916 | #endif |
| 13917 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13918 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13919 | #endif |
| 13920 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13921 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13922 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13923 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 13924 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13925 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13926 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13927 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13928 | #endif |
| 13929 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13930 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13931 | #endif |
| 13932 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13933 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13934 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13935 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13936 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13937 | #endif |
| 13938 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13939 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13940 | #endif |
| 13941 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13942 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13943 | #endif |
| 13944 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13945 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13946 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13947 | #endif |
| 13948 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 13949 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13950 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 13951 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 13952 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 13953 | #endif |
| 13954 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13955 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13956 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13957 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13958 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13959 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13960 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13961 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13962 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13963 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13964 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13965 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13966 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13967 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13968 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13969 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13970 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13971 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13972 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13973 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13974 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13975 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 13976 | #if HAVE_DECL_RTLD_MEMBER |
| 13977 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 13978 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13979 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13980 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13981 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 13982 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 13983 | #endif |
| 13984 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 13985 | #if defined(__APPLE__) |
| 13986 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 13987 | #endif |
| 13988 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13989 | #ifdef MS_WINDOWS |
| 13990 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1; |
| 13991 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1; |
| 13992 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1; |
| 13993 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1; |
| 13994 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1; |
| 13995 | #endif |
| 13996 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13997 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13998 | } |
| 13999 | |
| 14000 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14001 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14002 | PyModuleDef_HEAD_INIT, |
| 14003 | MODNAME, |
| 14004 | posix__doc__, |
| 14005 | -1, |
| 14006 | posix_methods, |
| 14007 | NULL, |
| 14008 | NULL, |
| 14009 | NULL, |
| 14010 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14011 | }; |
| 14012 | |
| 14013 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14014 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14015 | |
| 14016 | #ifdef HAVE_FACCESSAT |
| 14017 | "HAVE_FACCESSAT", |
| 14018 | #endif |
| 14019 | |
| 14020 | #ifdef HAVE_FCHDIR |
| 14021 | "HAVE_FCHDIR", |
| 14022 | #endif |
| 14023 | |
| 14024 | #ifdef HAVE_FCHMOD |
| 14025 | "HAVE_FCHMOD", |
| 14026 | #endif |
| 14027 | |
| 14028 | #ifdef HAVE_FCHMODAT |
| 14029 | "HAVE_FCHMODAT", |
| 14030 | #endif |
| 14031 | |
| 14032 | #ifdef HAVE_FCHOWN |
| 14033 | "HAVE_FCHOWN", |
| 14034 | #endif |
| 14035 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 14036 | #ifdef HAVE_FCHOWNAT |
| 14037 | "HAVE_FCHOWNAT", |
| 14038 | #endif |
| 14039 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14040 | #ifdef HAVE_FEXECVE |
| 14041 | "HAVE_FEXECVE", |
| 14042 | #endif |
| 14043 | |
| 14044 | #ifdef HAVE_FDOPENDIR |
| 14045 | "HAVE_FDOPENDIR", |
| 14046 | #endif |
| 14047 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14048 | #ifdef HAVE_FPATHCONF |
| 14049 | "HAVE_FPATHCONF", |
| 14050 | #endif |
| 14051 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14052 | #ifdef HAVE_FSTATAT |
| 14053 | "HAVE_FSTATAT", |
| 14054 | #endif |
| 14055 | |
| 14056 | #ifdef HAVE_FSTATVFS |
| 14057 | "HAVE_FSTATVFS", |
| 14058 | #endif |
| 14059 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 14060 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14061 | "HAVE_FTRUNCATE", |
| 14062 | #endif |
| 14063 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14064 | #ifdef HAVE_FUTIMENS |
| 14065 | "HAVE_FUTIMENS", |
| 14066 | #endif |
| 14067 | |
| 14068 | #ifdef HAVE_FUTIMES |
| 14069 | "HAVE_FUTIMES", |
| 14070 | #endif |
| 14071 | |
| 14072 | #ifdef HAVE_FUTIMESAT |
| 14073 | "HAVE_FUTIMESAT", |
| 14074 | #endif |
| 14075 | |
| 14076 | #ifdef HAVE_LINKAT |
| 14077 | "HAVE_LINKAT", |
| 14078 | #endif |
| 14079 | |
| 14080 | #ifdef HAVE_LCHFLAGS |
| 14081 | "HAVE_LCHFLAGS", |
| 14082 | #endif |
| 14083 | |
| 14084 | #ifdef HAVE_LCHMOD |
| 14085 | "HAVE_LCHMOD", |
| 14086 | #endif |
| 14087 | |
| 14088 | #ifdef HAVE_LCHOWN |
| 14089 | "HAVE_LCHOWN", |
| 14090 | #endif |
| 14091 | |
| 14092 | #ifdef HAVE_LSTAT |
| 14093 | "HAVE_LSTAT", |
| 14094 | #endif |
| 14095 | |
| 14096 | #ifdef HAVE_LUTIMES |
| 14097 | "HAVE_LUTIMES", |
| 14098 | #endif |
| 14099 | |
| 14100 | #ifdef HAVE_MKDIRAT |
| 14101 | "HAVE_MKDIRAT", |
| 14102 | #endif |
| 14103 | |
| 14104 | #ifdef HAVE_MKFIFOAT |
| 14105 | "HAVE_MKFIFOAT", |
| 14106 | #endif |
| 14107 | |
| 14108 | #ifdef HAVE_MKNODAT |
| 14109 | "HAVE_MKNODAT", |
| 14110 | #endif |
| 14111 | |
| 14112 | #ifdef HAVE_OPENAT |
| 14113 | "HAVE_OPENAT", |
| 14114 | #endif |
| 14115 | |
| 14116 | #ifdef HAVE_READLINKAT |
| 14117 | "HAVE_READLINKAT", |
| 14118 | #endif |
| 14119 | |
| 14120 | #ifdef HAVE_RENAMEAT |
| 14121 | "HAVE_RENAMEAT", |
| 14122 | #endif |
| 14123 | |
| 14124 | #ifdef HAVE_SYMLINKAT |
| 14125 | "HAVE_SYMLINKAT", |
| 14126 | #endif |
| 14127 | |
| 14128 | #ifdef HAVE_UNLINKAT |
| 14129 | "HAVE_UNLINKAT", |
| 14130 | #endif |
| 14131 | |
| 14132 | #ifdef HAVE_UTIMENSAT |
| 14133 | "HAVE_UTIMENSAT", |
| 14134 | #endif |
| 14135 | |
| 14136 | #ifdef MS_WINDOWS |
| 14137 | "MS_WINDOWS", |
| 14138 | #endif |
| 14139 | |
| 14140 | NULL |
| 14141 | }; |
| 14142 | |
| 14143 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 14144 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 14145 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14146 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14147 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14148 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14149 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14150 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14151 | m = PyModule_Create(&posixmodule); |
| 14152 | if (m == NULL) |
| 14153 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14154 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14155 | /* Initialize environ dictionary */ |
| 14156 | v = convertenviron(); |
| 14157 | Py_XINCREF(v); |
| 14158 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 14159 | return NULL; |
| 14160 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14161 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14162 | if (all_ins(m)) |
| 14163 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14164 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14165 | if (setup_confname_tables(m)) |
| 14166 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14167 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14168 | Py_INCREF(PyExc_OSError); |
| 14169 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 14170 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 14171 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14172 | if (posix_putenv_garbage == NULL) |
| 14173 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 14174 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 14175 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14176 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14177 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 14178 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14179 | WaitidResultType = PyStructSequence_NewType(&waitid_result_desc); |
| 14180 | if (WaitidResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14181 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14182 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14183 | #endif |
| 14184 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 14185 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14186 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 14187 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 14188 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14189 | StatResultType = PyStructSequence_NewType(&stat_result_desc); |
| 14190 | if (StatResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14191 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14192 | } |
| 14193 | structseq_new = StatResultType->tp_new; |
| 14194 | StatResultType->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14195 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 14196 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14197 | StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc); |
| 14198 | if (StatVFSResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14199 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14200 | } |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14201 | #ifdef NEED_TICKS_PER_SECOND |
| 14202 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14203 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14204 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14205 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14206 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14207 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14208 | # endif |
| 14209 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14210 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 14211 | #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] | 14212 | sched_param_desc.name = MODNAME ".sched_param"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14213 | SchedParamType = PyStructSequence_NewType(&sched_param_desc); |
| 14214 | if (SchedParamType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14215 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14216 | } |
| 14217 | SchedParamType->tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14218 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14219 | |
| 14220 | /* initialize TerminalSize_info */ |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14221 | TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc); |
| 14222 | if (TerminalSizeType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14223 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14224 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14225 | |
| 14226 | /* initialize scandir types */ |
| 14227 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 14228 | return NULL; |
| 14229 | if (PyType_Ready(&DirEntryType) < 0) |
| 14230 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14231 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14232 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14233 | Py_INCREF((PyObject*) WaitidResultType); |
| 14234 | PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14235 | #endif |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14236 | Py_INCREF((PyObject*) StatResultType); |
| 14237 | PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType); |
| 14238 | Py_INCREF((PyObject*) StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14239 | PyModule_AddObject(m, "statvfs_result", |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14240 | (PyObject*) StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14241 | |
| 14242 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14243 | Py_INCREF(SchedParamType); |
| 14244 | PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14245 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14246 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14247 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14248 | TimesResultType = PyStructSequence_NewType(×_result_desc); |
| 14249 | if (TimesResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14250 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14251 | } |
| 14252 | PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14253 | |
| 14254 | uname_result_desc.name = MODNAME ".uname_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14255 | UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
| 14256 | if (UnameResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14257 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14258 | } |
| 14259 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14260 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14261 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14262 | /* |
| 14263 | * Step 2 of weak-linking support on Mac OS X. |
| 14264 | * |
| 14265 | * The code below removes functions that are not available on the |
| 14266 | * currently active platform. |
| 14267 | * |
| 14268 | * 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] | 14269 | * 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] | 14270 | * OSX 10.4. |
| 14271 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14272 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14273 | if (fstatvfs == NULL) { |
| 14274 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 14275 | return NULL; |
| 14276 | } |
| 14277 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14278 | #endif /* HAVE_FSTATVFS */ |
| 14279 | |
| 14280 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14281 | if (statvfs == NULL) { |
| 14282 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 14283 | return NULL; |
| 14284 | } |
| 14285 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14286 | #endif /* HAVE_STATVFS */ |
| 14287 | |
| 14288 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14289 | if (lchown == NULL) { |
| 14290 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 14291 | return NULL; |
| 14292 | } |
| 14293 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14294 | #endif /* HAVE_LCHOWN */ |
| 14295 | |
| 14296 | |
| 14297 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14298 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14299 | Py_INCREF(TerminalSizeType); |
| 14300 | PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14301 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 14302 | billion = PyLong_FromLong(1000000000); |
| 14303 | if (!billion) |
| 14304 | return NULL; |
| 14305 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14306 | /* suppress "function not used" warnings */ |
| 14307 | { |
| 14308 | int ignored; |
| 14309 | fd_specified("", -1); |
| 14310 | follow_symlinks_specified("", 1); |
| 14311 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 14312 | dir_fd_converter(Py_None, &ignored); |
| 14313 | dir_fd_unavailable(Py_None, &ignored); |
| 14314 | } |
| 14315 | |
| 14316 | /* |
| 14317 | * provide list of locally available functions |
| 14318 | * so os.py can populate support_* lists |
| 14319 | */ |
| 14320 | list = PyList_New(0); |
| 14321 | if (!list) |
| 14322 | return NULL; |
| 14323 | for (trace = have_functions; *trace; trace++) { |
| 14324 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 14325 | if (!unicode) |
| 14326 | return NULL; |
| 14327 | if (PyList_Append(list, unicode)) |
| 14328 | return NULL; |
| 14329 | Py_DECREF(unicode); |
| 14330 | } |
| 14331 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 14332 | |
| 14333 | Py_INCREF((PyObject *) &DirEntryType); |
Brett Cannon | a32c4d0 | 2016-06-24 14:14:44 -0700 | [diff] [blame] | 14334 | PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14335 | |
| 14336 | initialized = 1; |
| 14337 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14338 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14339 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14340 | |
| 14341 | #ifdef __cplusplus |
| 14342 | } |
| 14343 | #endif |