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" |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 28 | #include "pythread.h" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 29 | #include "structmember.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 30 | #ifndef MS_WINDOWS |
| 31 | #include "posixmodule.h" |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 32 | #else |
| 33 | #include "winreparse.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 34 | #endif |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 35 | #include "pycore_pystate.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 36 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 37 | /* On android API level 21, 'AT_EACCESS' is not declared although |
| 38 | * HAVE_FACCESSAT is defined. */ |
| 39 | #ifdef __ANDROID__ |
| 40 | #undef HAVE_FACCESSAT |
| 41 | #endif |
| 42 | |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | fa76eee | 2016-05-28 21:03:48 +0000 | [diff] [blame] | 43 | #include <stdio.h> /* needed for ctermid() */ |
| 44 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 45 | #ifdef __cplusplus |
| 46 | extern "C" { |
| 47 | #endif |
| 48 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 49 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 50 | "This module provides access to operating system functionality that is\n\ |
| 51 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 52 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 53 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 55 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 56 | #ifdef HAVE_SYS_UIO_H |
| 57 | #include <sys/uio.h> |
| 58 | #endif |
| 59 | |
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 60 | #ifdef HAVE_SYS_SYSMACROS_H |
| 61 | /* GNU C Library: major(), minor(), makedev() */ |
| 62 | #include <sys/sysmacros.h> |
| 63 | #endif |
| 64 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 65 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 66 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 67 | #endif /* HAVE_SYS_TYPES_H */ |
| 68 | |
| 69 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 70 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 71 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 73 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 74 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 75 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 76 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 78 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 79 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 81 | #ifdef HAVE_FCNTL_H |
| 82 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 83 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 85 | #ifdef HAVE_GRP_H |
| 86 | #include <grp.h> |
| 87 | #endif |
| 88 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 89 | #ifdef HAVE_SYSEXITS_H |
| 90 | #include <sysexits.h> |
| 91 | #endif /* HAVE_SYSEXITS_H */ |
| 92 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 93 | #ifdef HAVE_SYS_LOADAVG_H |
| 94 | #include <sys/loadavg.h> |
| 95 | #endif |
| 96 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 97 | #ifdef HAVE_SYS_SENDFILE_H |
| 98 | #include <sys/sendfile.h> |
| 99 | #endif |
| 100 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 101 | #if defined(__APPLE__) |
| 102 | #include <copyfile.h> |
| 103 | #endif |
| 104 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 105 | #ifdef HAVE_SCHED_H |
| 106 | #include <sched.h> |
| 107 | #endif |
| 108 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 109 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 110 | #undef HAVE_SCHED_SETAFFINITY |
| 111 | #endif |
| 112 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 113 | #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] | 114 | #define USE_XATTRS |
| 115 | #endif |
| 116 | |
| 117 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 118 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 119 | #endif |
| 120 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 121 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 122 | #ifdef HAVE_SYS_SOCKET_H |
| 123 | #include <sys/socket.h> |
| 124 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 125 | #endif |
| 126 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 127 | #ifdef HAVE_DLFCN_H |
| 128 | #include <dlfcn.h> |
| 129 | #endif |
| 130 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 131 | #ifdef __hpux |
| 132 | #include <sys/mpctl.h> |
| 133 | #endif |
| 134 | |
| 135 | #if defined(__DragonFly__) || \ |
| 136 | defined(__OpenBSD__) || \ |
| 137 | defined(__FreeBSD__) || \ |
| 138 | defined(__NetBSD__) || \ |
| 139 | defined(__APPLE__) |
| 140 | #include <sys/sysctl.h> |
| 141 | #endif |
| 142 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 143 | #ifdef HAVE_LINUX_RANDOM_H |
| 144 | # include <linux/random.h> |
| 145 | #endif |
| 146 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 147 | # include <sys/syscall.h> |
| 148 | #endif |
| 149 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 150 | #if defined(MS_WINDOWS) |
| 151 | # define TERMSIZE_USE_CONIO |
| 152 | #elif defined(HAVE_SYS_IOCTL_H) |
| 153 | # include <sys/ioctl.h> |
| 154 | # if defined(HAVE_TERMIOS_H) |
| 155 | # include <termios.h> |
| 156 | # endif |
| 157 | # if defined(TIOCGWINSZ) |
| 158 | # define TERMSIZE_USE_IOCTL |
| 159 | # endif |
| 160 | #endif /* MS_WINDOWS */ |
| 161 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 162 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 163 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 164 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 165 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 166 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 167 | #include <process.h> |
| 168 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 169 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 170 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 171 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 172 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #define HAVE_EXECV 1 |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 174 | #define HAVE_WSPAWNV 1 |
| 175 | #define HAVE_WEXECV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 176 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 177 | #define HAVE_SYSTEM 1 |
| 178 | #define HAVE_CWAIT 1 |
| 179 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 180 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 181 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 182 | /* Unix functions that the configure script doesn't check for */ |
| 183 | #define HAVE_EXECV 1 |
| 184 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 185 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 186 | #define HAVE_FORK1 1 |
| 187 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 188 | #define HAVE_GETEGID 1 |
| 189 | #define HAVE_GETEUID 1 |
| 190 | #define HAVE_GETGID 1 |
| 191 | #define HAVE_GETPPID 1 |
| 192 | #define HAVE_GETUID 1 |
| 193 | #define HAVE_KILL 1 |
| 194 | #define HAVE_OPENDIR 1 |
| 195 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 196 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 197 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 198 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 199 | #endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 200 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 201 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 202 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 203 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 204 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 205 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 206 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 207 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 208 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 209 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 210 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 211 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 212 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 213 | (default) */ |
| 214 | extern char *ctermid_r(char *); |
| 215 | #endif |
| 216 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 217 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 218 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 219 | #ifdef HAVE_POSIX_SPAWN |
| 220 | #include <spawn.h> |
| 221 | #endif |
| 222 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 223 | #ifdef HAVE_UTIME_H |
| 224 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 225 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 227 | #ifdef HAVE_SYS_UTIME_H |
| 228 | #include <sys/utime.h> |
| 229 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 230 | #endif /* HAVE_SYS_UTIME_H */ |
| 231 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | #ifdef HAVE_SYS_TIMES_H |
| 233 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 234 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | |
| 236 | #ifdef HAVE_SYS_PARAM_H |
| 237 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 238 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | |
| 240 | #ifdef HAVE_SYS_UTSNAME_H |
| 241 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 242 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 244 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 246 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 247 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 248 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 249 | #include <direct.h> |
| 250 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 251 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 252 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 253 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 254 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 255 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 257 | #endif |
| 258 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 259 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 260 | #endif |
| 261 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 262 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 263 | #endif |
| 264 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 266 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 267 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 269 | #endif |
| 270 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 272 | #endif |
| 273 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 274 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 275 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 276 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 277 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 278 | #endif |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 279 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 280 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 281 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 282 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 283 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 284 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 285 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 286 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 287 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 288 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 289 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 290 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 291 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 292 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 293 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 294 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 295 | #define MAXPATHLEN PATH_MAX |
| 296 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 297 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 298 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 299 | #endif /* MAXPATHLEN */ |
| 300 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 301 | #ifdef UNION_WAIT |
| 302 | /* Emulate some macros on systems that have a union instead of macros */ |
| 303 | |
| 304 | #ifndef WIFEXITED |
| 305 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 306 | #endif |
| 307 | |
| 308 | #ifndef WEXITSTATUS |
| 309 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 310 | #endif |
| 311 | |
| 312 | #ifndef WTERMSIG |
| 313 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 314 | #endif |
| 315 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 316 | #define WAIT_TYPE union wait |
| 317 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 318 | |
| 319 | #else /* !UNION_WAIT */ |
| 320 | #define WAIT_TYPE int |
| 321 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 322 | #endif /* UNION_WAIT */ |
| 323 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 324 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 325 | prototype for it, at least on Solaris -- maybe others as well?). */ |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 326 | #if defined(HAVE_CTERMID_R) |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 327 | #define USE_CTERMID_R |
| 328 | #endif |
| 329 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 330 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 331 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 332 | #undef FSTAT |
| 333 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 334 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 335 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 336 | # define LSTAT win32_lstat |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 337 | # define FSTAT _Py_fstat_noraise |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 338 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 339 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 340 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 341 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 342 | # define FSTAT fstat |
| 343 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 344 | #endif |
| 345 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 346 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 347 | #include <sys/mkdev.h> |
| 348 | #else |
| 349 | #if defined(MAJOR_IN_SYSMACROS) |
| 350 | #include <sys/sysmacros.h> |
| 351 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 352 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 353 | #include <sys/mkdev.h> |
| 354 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 355 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 356 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 357 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 358 | #define INITFUNC PyInit_nt |
| 359 | #define MODNAME "nt" |
| 360 | #else |
| 361 | #define INITFUNC PyInit_posix |
| 362 | #define MODNAME "posix" |
| 363 | #endif |
| 364 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 365 | #if defined(__sun) |
| 366 | /* Something to implement in autoconf, not present in autoconf 2.69 */ |
| 367 | #define HAVE_STRUCT_STAT_ST_FSTYPE 1 |
| 368 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 369 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 370 | #ifdef _Py_MEMORY_SANITIZER |
| 371 | # include <sanitizer/msan_interface.h> |
| 372 | #endif |
| 373 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 374 | #ifdef HAVE_FORK |
| 375 | static void |
| 376 | run_at_forkers(PyObject *lst, int reverse) |
| 377 | { |
| 378 | Py_ssize_t i; |
| 379 | PyObject *cpy; |
| 380 | |
| 381 | if (lst != NULL) { |
| 382 | assert(PyList_CheckExact(lst)); |
| 383 | |
| 384 | /* Use a list copy in case register_at_fork() is called from |
| 385 | * one of the callbacks. |
| 386 | */ |
| 387 | cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst)); |
| 388 | if (cpy == NULL) |
| 389 | PyErr_WriteUnraisable(lst); |
| 390 | else { |
| 391 | if (reverse) |
| 392 | PyList_Reverse(cpy); |
| 393 | for (i = 0; i < PyList_GET_SIZE(cpy); i++) { |
| 394 | PyObject *func, *res; |
| 395 | func = PyList_GET_ITEM(cpy, i); |
| 396 | res = PyObject_CallObject(func, NULL); |
| 397 | if (res == NULL) |
| 398 | PyErr_WriteUnraisable(func); |
| 399 | else |
| 400 | Py_DECREF(res); |
| 401 | } |
| 402 | Py_DECREF(cpy); |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | void |
| 408 | PyOS_BeforeFork(void) |
| 409 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 410 | run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 411 | |
| 412 | _PyImport_AcquireLock(); |
| 413 | } |
| 414 | |
| 415 | void |
| 416 | PyOS_AfterFork_Parent(void) |
| 417 | { |
| 418 | if (_PyImport_ReleaseLock() <= 0) |
| 419 | Py_FatalError("failed releasing import lock after fork"); |
| 420 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 421 | run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void |
| 425 | PyOS_AfterFork_Child(void) |
| 426 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 427 | _PyGILState_Reinit(); |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 428 | _PyInterpreterState_DeleteExceptMain(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 429 | PyEval_ReInitThreads(); |
| 430 | _PyImport_ReInitLock(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 431 | _PySignal_AfterFork(); |
| 432 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 433 | run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static int |
| 437 | register_at_forker(PyObject **lst, PyObject *func) |
| 438 | { |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 439 | if (func == NULL) /* nothing to register? do nothing. */ |
| 440 | return 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 441 | if (*lst == NULL) { |
| 442 | *lst = PyList_New(0); |
| 443 | if (*lst == NULL) |
| 444 | return -1; |
| 445 | } |
| 446 | return PyList_Append(*lst, func); |
| 447 | } |
| 448 | #endif |
| 449 | |
| 450 | /* Legacy wrapper */ |
| 451 | void |
| 452 | PyOS_AfterFork(void) |
| 453 | { |
| 454 | #ifdef HAVE_FORK |
| 455 | PyOS_AfterFork_Child(); |
| 456 | #endif |
| 457 | } |
| 458 | |
| 459 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 460 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 461 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 462 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 463 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 464 | ULONG, struct _Py_stat_struct *); |
| 465 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 466 | |
| 467 | #ifdef MS_WINDOWS |
| 468 | static int |
| 469 | win32_warn_bytes_api() |
| 470 | { |
| 471 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 472 | "The Windows bytes API has been deprecated, " |
| 473 | "use Unicode filenames instead", |
| 474 | 1); |
| 475 | } |
| 476 | #endif |
| 477 | |
| 478 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 479 | #ifndef MS_WINDOWS |
| 480 | PyObject * |
| 481 | _PyLong_FromUid(uid_t uid) |
| 482 | { |
| 483 | if (uid == (uid_t)-1) |
| 484 | return PyLong_FromLong(-1); |
| 485 | return PyLong_FromUnsignedLong(uid); |
| 486 | } |
| 487 | |
| 488 | PyObject * |
| 489 | _PyLong_FromGid(gid_t gid) |
| 490 | { |
| 491 | if (gid == (gid_t)-1) |
| 492 | return PyLong_FromLong(-1); |
| 493 | return PyLong_FromUnsignedLong(gid); |
| 494 | } |
| 495 | |
| 496 | int |
| 497 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 498 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 499 | uid_t uid; |
| 500 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 501 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 502 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 503 | unsigned long uresult; |
| 504 | |
| 505 | index = PyNumber_Index(obj); |
| 506 | if (index == NULL) { |
| 507 | PyErr_Format(PyExc_TypeError, |
| 508 | "uid should be integer, not %.200s", |
| 509 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 510 | return 0; |
| 511 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 512 | |
| 513 | /* |
| 514 | * Handling uid_t is complicated for two reasons: |
| 515 | * * Although uid_t is (always?) unsigned, it still |
| 516 | * accepts -1. |
| 517 | * * We don't know its size in advance--it may be |
| 518 | * bigger than an int, or it may be smaller than |
| 519 | * a long. |
| 520 | * |
| 521 | * So a bit of defensive programming is in order. |
| 522 | * Start with interpreting the value passed |
| 523 | * in as a signed long and see if it works. |
| 524 | */ |
| 525 | |
| 526 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 527 | |
| 528 | if (!overflow) { |
| 529 | uid = (uid_t)result; |
| 530 | |
| 531 | if (result == -1) { |
| 532 | if (PyErr_Occurred()) |
| 533 | goto fail; |
| 534 | /* It's a legitimate -1, we're done. */ |
| 535 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 536 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 537 | |
| 538 | /* Any other negative number is disallowed. */ |
| 539 | if (result < 0) |
| 540 | goto underflow; |
| 541 | |
| 542 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 543 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 544 | (long)uid != result) |
| 545 | goto underflow; |
| 546 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 547 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 548 | |
| 549 | if (overflow < 0) |
| 550 | goto underflow; |
| 551 | |
| 552 | /* |
| 553 | * Okay, the value overflowed a signed long. If it |
| 554 | * fits in an *unsigned* long, it may still be okay, |
| 555 | * as uid_t may be unsigned long on this platform. |
| 556 | */ |
| 557 | uresult = PyLong_AsUnsignedLong(index); |
| 558 | if (PyErr_Occurred()) { |
| 559 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 560 | goto overflow; |
| 561 | goto fail; |
| 562 | } |
| 563 | |
| 564 | uid = (uid_t)uresult; |
| 565 | |
| 566 | /* |
| 567 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 568 | * but this value would get interpreted as (uid_t)-1 by chown |
| 569 | * and its siblings. That's not what the user meant! So we |
| 570 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 571 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 572 | */ |
| 573 | if (uid == (uid_t)-1) |
| 574 | goto overflow; |
| 575 | |
| 576 | /* Ensure the value wasn't truncated. */ |
| 577 | if (sizeof(uid_t) < sizeof(long) && |
| 578 | (unsigned long)uid != uresult) |
| 579 | goto overflow; |
| 580 | /* fallthrough */ |
| 581 | |
| 582 | success: |
| 583 | Py_DECREF(index); |
| 584 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 585 | return 1; |
| 586 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 587 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 588 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 589 | "uid is less than minimum"); |
| 590 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 591 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 592 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 593 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 594 | "uid is greater than maximum"); |
| 595 | /* fallthrough */ |
| 596 | |
| 597 | fail: |
| 598 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | int |
| 603 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 604 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 605 | gid_t gid; |
| 606 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 607 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 608 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 609 | unsigned long uresult; |
| 610 | |
| 611 | index = PyNumber_Index(obj); |
| 612 | if (index == NULL) { |
| 613 | PyErr_Format(PyExc_TypeError, |
| 614 | "gid should be integer, not %.200s", |
| 615 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 616 | return 0; |
| 617 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 618 | |
| 619 | /* |
| 620 | * Handling gid_t is complicated for two reasons: |
| 621 | * * Although gid_t is (always?) unsigned, it still |
| 622 | * accepts -1. |
| 623 | * * We don't know its size in advance--it may be |
| 624 | * bigger than an int, or it may be smaller than |
| 625 | * a long. |
| 626 | * |
| 627 | * So a bit of defensive programming is in order. |
| 628 | * Start with interpreting the value passed |
| 629 | * in as a signed long and see if it works. |
| 630 | */ |
| 631 | |
| 632 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 633 | |
| 634 | if (!overflow) { |
| 635 | gid = (gid_t)result; |
| 636 | |
| 637 | if (result == -1) { |
| 638 | if (PyErr_Occurred()) |
| 639 | goto fail; |
| 640 | /* It's a legitimate -1, we're done. */ |
| 641 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 642 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 643 | |
| 644 | /* Any other negative number is disallowed. */ |
| 645 | if (result < 0) { |
| 646 | goto underflow; |
| 647 | } |
| 648 | |
| 649 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 650 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 651 | (long)gid != result) |
| 652 | goto underflow; |
| 653 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 654 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 655 | |
| 656 | if (overflow < 0) |
| 657 | goto underflow; |
| 658 | |
| 659 | /* |
| 660 | * Okay, the value overflowed a signed long. If it |
| 661 | * fits in an *unsigned* long, it may still be okay, |
| 662 | * as gid_t may be unsigned long on this platform. |
| 663 | */ |
| 664 | uresult = PyLong_AsUnsignedLong(index); |
| 665 | if (PyErr_Occurred()) { |
| 666 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 667 | goto overflow; |
| 668 | goto fail; |
| 669 | } |
| 670 | |
| 671 | gid = (gid_t)uresult; |
| 672 | |
| 673 | /* |
| 674 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 675 | * but this value would get interpreted as (gid_t)-1 by chown |
| 676 | * and its siblings. That's not what the user meant! So we |
| 677 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 678 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 679 | */ |
| 680 | if (gid == (gid_t)-1) |
| 681 | goto overflow; |
| 682 | |
| 683 | /* Ensure the value wasn't truncated. */ |
| 684 | if (sizeof(gid_t) < sizeof(long) && |
| 685 | (unsigned long)gid != uresult) |
| 686 | goto overflow; |
| 687 | /* fallthrough */ |
| 688 | |
| 689 | success: |
| 690 | Py_DECREF(index); |
| 691 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 692 | return 1; |
| 693 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 694 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 695 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 696 | "gid is less than minimum"); |
| 697 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 698 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 699 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 700 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 701 | "gid is greater than maximum"); |
| 702 | /* fallthrough */ |
| 703 | |
| 704 | fail: |
| 705 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | #endif /* MS_WINDOWS */ |
| 709 | |
| 710 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 711 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 712 | |
| 713 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 714 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 715 | static int |
| 716 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 717 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 718 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 719 | if (PyErr_Occurred()) |
| 720 | return 0; |
| 721 | return 1; |
| 722 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 723 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 724 | |
| 725 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 726 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 727 | /* |
| 728 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 729 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 730 | * which doesn't play nicely with all the initializer lines in this file that |
| 731 | * look like this: |
| 732 | * int dir_fd = DEFAULT_DIR_FD; |
| 733 | */ |
| 734 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 735 | #else |
| 736 | #define DEFAULT_DIR_FD (-100) |
| 737 | #endif |
| 738 | |
| 739 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 740 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 741 | { |
| 742 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 743 | long long_value; |
| 744 | |
| 745 | PyObject *index = PyNumber_Index(o); |
| 746 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 747 | return 0; |
| 748 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 749 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 750 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 751 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 752 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 753 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 754 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 755 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 756 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 757 | return 0; |
| 758 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 759 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 760 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 761 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 762 | return 0; |
| 763 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 764 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 765 | *p = (int)long_value; |
| 766 | return 1; |
| 767 | } |
| 768 | |
| 769 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 770 | dir_fd_converter(PyObject *o, void *p) |
| 771 | { |
| 772 | if (o == Py_None) { |
| 773 | *(int *)p = DEFAULT_DIR_FD; |
| 774 | return 1; |
| 775 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 776 | else if (PyIndex_Check(o)) { |
| 777 | return _fd_converter(o, (int *)p); |
| 778 | } |
| 779 | else { |
| 780 | PyErr_Format(PyExc_TypeError, |
| 781 | "argument should be integer or None, not %.200s", |
| 782 | Py_TYPE(o)->tp_name); |
| 783 | return 0; |
| 784 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 788 | /* |
| 789 | * A PyArg_ParseTuple "converter" function |
| 790 | * that handles filesystem paths in the manner |
| 791 | * preferred by the os module. |
| 792 | * |
| 793 | * path_converter accepts (Unicode) strings and their |
| 794 | * subclasses, and bytes and their subclasses. What |
| 795 | * it does with the argument depends on the platform: |
| 796 | * |
| 797 | * * On Windows, if we get a (Unicode) string we |
| 798 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 799 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 800 | * |
| 801 | * * On all other platforms, strings are encoded |
| 802 | * to bytes using PyUnicode_FSConverter, then we |
| 803 | * extract the char * from the bytes object and |
| 804 | * return that. |
| 805 | * |
| 806 | * path_converter also optionally accepts signed |
| 807 | * integers (representing open file descriptors) instead |
| 808 | * of path strings. |
| 809 | * |
| 810 | * Input fields: |
| 811 | * path.nullable |
| 812 | * If nonzero, the path is permitted to be None. |
| 813 | * path.allow_fd |
| 814 | * If nonzero, the path is permitted to be a file handle |
| 815 | * (a signed int) instead of a string. |
| 816 | * path.function_name |
| 817 | * If non-NULL, path_converter will use that as the name |
| 818 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 819 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 820 | * path.argument_name |
| 821 | * If non-NULL, path_converter will use that as the name |
| 822 | * of the parameter in error messages. |
| 823 | * (If path.argument_name is NULL it uses "path".) |
| 824 | * |
| 825 | * Output fields: |
| 826 | * path.wide |
| 827 | * Points to the path if it was expressed as Unicode |
| 828 | * and was not encoded. (Only used on Windows.) |
| 829 | * path.narrow |
| 830 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 831 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 832 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 833 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 834 | * path.fd |
| 835 | * Contains a file descriptor if path.accept_fd was true |
| 836 | * and the caller provided a signed integer instead of any |
| 837 | * sort of string. |
| 838 | * |
| 839 | * WARNING: if your "path" parameter is optional, and is |
| 840 | * unspecified, path_converter will never get called. |
| 841 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 842 | * yourself! |
| 843 | * path.length |
| 844 | * The length of the path in characters, if specified as |
| 845 | * a string. |
| 846 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 847 | * The original object passed in (if get a PathLike object, |
| 848 | * the result of PyOS_FSPath() is treated as the original object). |
| 849 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 850 | * path.cleanup |
| 851 | * For internal use only. May point to a temporary object. |
| 852 | * (Pay no attention to the man behind the curtain.) |
| 853 | * |
| 854 | * At most one of path.wide or path.narrow will be non-NULL. |
| 855 | * If path was None and path.nullable was set, |
| 856 | * or if path was an integer and path.allow_fd was set, |
| 857 | * both path.wide and path.narrow will be NULL |
| 858 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 859 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 860 | * path_converter takes care to not write to the path_t |
| 861 | * unless it's successful. However it must reset the |
| 862 | * "cleanup" field each time it's called. |
| 863 | * |
| 864 | * Use as follows: |
| 865 | * path_t path; |
| 866 | * memset(&path, 0, sizeof(path)); |
| 867 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 868 | * // ... use values from path ... |
| 869 | * path_cleanup(&path); |
| 870 | * |
| 871 | * (Note that if PyArg_Parse fails you don't need to call |
| 872 | * path_cleanup(). However it is safe to do so.) |
| 873 | */ |
| 874 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 875 | const char *function_name; |
| 876 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 877 | int nullable; |
| 878 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 879 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 880 | #ifdef MS_WINDOWS |
| 881 | BOOL narrow; |
| 882 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 883 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 884 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 885 | int fd; |
| 886 | Py_ssize_t length; |
| 887 | PyObject *object; |
| 888 | PyObject *cleanup; |
| 889 | } path_t; |
| 890 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 891 | #ifdef MS_WINDOWS |
| 892 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 893 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 894 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 895 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 896 | {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] | 897 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 898 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 899 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 900 | path_cleanup(path_t *path) |
| 901 | { |
| 902 | Py_CLEAR(path->object); |
| 903 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 907 | path_converter(PyObject *o, void *p) |
| 908 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 909 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 910 | PyObject *bytes = NULL; |
| 911 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 912 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 913 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 914 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 915 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 916 | const wchar_t *wide; |
| 917 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 918 | |
| 919 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 920 | PyErr_Format(exc, "%s%s" fmt, \ |
| 921 | path->function_name ? path->function_name : "", \ |
| 922 | path->function_name ? ": " : "", \ |
| 923 | path->argument_name ? path->argument_name : "path") |
| 924 | |
| 925 | /* Py_CLEANUP_SUPPORTED support */ |
| 926 | if (o == NULL) { |
| 927 | path_cleanup(path); |
| 928 | return 1; |
| 929 | } |
| 930 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 931 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 932 | path->object = path->cleanup = NULL; |
| 933 | /* path->object owns a reference to the original object */ |
| 934 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 935 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 936 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 937 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 938 | #ifdef MS_WINDOWS |
| 939 | path->narrow = FALSE; |
| 940 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 941 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 942 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 943 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 944 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 947 | /* Only call this here so that we don't treat the return value of |
| 948 | os.fspath() as an fd or buffer. */ |
| 949 | is_index = path->allow_fd && PyIndex_Check(o); |
| 950 | is_buffer = PyObject_CheckBuffer(o); |
| 951 | is_bytes = PyBytes_Check(o); |
| 952 | is_unicode = PyUnicode_Check(o); |
| 953 | |
| 954 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 955 | /* Inline PyOS_FSPath() for better error messages. */ |
| 956 | _Py_IDENTIFIER(__fspath__); |
| 957 | PyObject *func = NULL; |
| 958 | |
| 959 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 960 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 961 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 962 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 963 | /* still owns a reference to the original object */ |
| 964 | Py_DECREF(o); |
| 965 | o = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 966 | Py_DECREF(func); |
| 967 | if (NULL == o) { |
| 968 | goto error_exit; |
| 969 | } |
| 970 | else if (PyUnicode_Check(o)) { |
| 971 | is_unicode = 1; |
| 972 | } |
| 973 | else if (PyBytes_Check(o)) { |
| 974 | is_bytes = 1; |
| 975 | } |
| 976 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 977 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | |
| 981 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 982 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 983 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 984 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 985 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 986 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 987 | if (length > 32767) { |
| 988 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 989 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 990 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 991 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 992 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 993 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 994 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 995 | |
| 996 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 997 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 998 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 999 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1000 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1001 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1002 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1003 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1004 | #endif |
| 1005 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1006 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1007 | bytes = o; |
| 1008 | Py_INCREF(bytes); |
| 1009 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1010 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1011 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1012 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1013 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1014 | "%s%s%s should be %s, not %.200s", |
| 1015 | path->function_name ? path->function_name : "", |
| 1016 | path->function_name ? ": " : "", |
| 1017 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1018 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1019 | "integer or None" : |
| 1020 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1021 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1022 | "string, bytes or os.PathLike", |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1023 | Py_TYPE(o)->tp_name)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1024 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1025 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1026 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1027 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1028 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1029 | } |
| 1030 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1031 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1032 | if (!_fd_converter(o, &path->fd)) { |
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 | } |
| 1035 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1036 | #ifdef MS_WINDOWS |
| 1037 | path->narrow = FALSE; |
| 1038 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1039 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1040 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1041 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1042 | } |
| 1043 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1044 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1045 | PyErr_Format(PyExc_TypeError, "%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 | 819399b | 2016-04-06 22:17:52 +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; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1058 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1059 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1060 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1061 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1062 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1065 | #ifdef MS_WINDOWS |
| 1066 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1067 | narrow, |
| 1068 | length |
| 1069 | ); |
| 1070 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1071 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1074 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1075 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1076 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1077 | } |
| 1078 | if (length > 32767) { |
| 1079 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1080 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1081 | } |
| 1082 | if (wcslen(wide) != length) { |
| 1083 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1084 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1085 | } |
| 1086 | path->wide = wide; |
| 1087 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1088 | path->cleanup = wo; |
| 1089 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1090 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1091 | path->wide = NULL; |
| 1092 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1093 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1094 | /* Still a reference owned by path->object, don't have to |
| 1095 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1096 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1097 | } |
| 1098 | else { |
| 1099 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1100 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1101 | #endif |
| 1102 | path->fd = -1; |
| 1103 | |
| 1104 | success_exit: |
| 1105 | path->length = length; |
| 1106 | path->object = o; |
| 1107 | return Py_CLEANUP_SUPPORTED; |
| 1108 | |
| 1109 | error_exit: |
| 1110 | Py_XDECREF(o); |
| 1111 | Py_XDECREF(bytes); |
| 1112 | #ifdef MS_WINDOWS |
| 1113 | Py_XDECREF(wo); |
| 1114 | #endif |
| 1115 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1119 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1120 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1121 | PyErr_Format(PyExc_NotImplementedError, |
| 1122 | "%s%s%s unavailable on this platform", |
| 1123 | (function_name != NULL) ? function_name : "", |
| 1124 | (function_name != NULL) ? ": ": "", |
| 1125 | argument_name); |
| 1126 | } |
| 1127 | |
| 1128 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1129 | dir_fd_unavailable(PyObject *o, void *p) |
| 1130 | { |
| 1131 | int dir_fd; |
| 1132 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1133 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1134 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1135 | argument_unavailable_error(NULL, "dir_fd"); |
| 1136 | return 0; |
| 1137 | } |
| 1138 | *(int *)p = dir_fd; |
| 1139 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1143 | fd_specified(const char *function_name, int fd) |
| 1144 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1145 | if (fd == -1) |
| 1146 | return 0; |
| 1147 | |
| 1148 | argument_unavailable_error(function_name, "fd"); |
| 1149 | return 1; |
| 1150 | } |
| 1151 | |
| 1152 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1153 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1154 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1155 | if (follow_symlinks) |
| 1156 | return 0; |
| 1157 | |
| 1158 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1159 | return 1; |
| 1160 | } |
| 1161 | |
| 1162 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1163 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1164 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1165 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1166 | #ifndef MS_WINDOWS |
| 1167 | && !path->narrow |
| 1168 | #endif |
| 1169 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1170 | PyErr_Format(PyExc_ValueError, |
| 1171 | "%s: can't specify dir_fd without matching path", |
| 1172 | function_name); |
| 1173 | return 1; |
| 1174 | } |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1179 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1180 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1181 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1182 | PyErr_Format(PyExc_ValueError, |
| 1183 | "%s: can't specify both dir_fd and fd", |
| 1184 | function_name); |
| 1185 | return 1; |
| 1186 | } |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1191 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1192 | int follow_symlinks) |
| 1193 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1194 | if ((fd > 0) && (!follow_symlinks)) { |
| 1195 | PyErr_Format(PyExc_ValueError, |
| 1196 | "%s: cannot use fd and follow_symlinks together", |
| 1197 | function_name); |
| 1198 | return 1; |
| 1199 | } |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1204 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1205 | int follow_symlinks) |
| 1206 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1207 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1208 | PyErr_Format(PyExc_ValueError, |
| 1209 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1210 | function_name); |
| 1211 | return 1; |
| 1212 | } |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1216 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1217 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1218 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1219 | typedef off_t Py_off_t; |
| 1220 | #endif |
| 1221 | |
| 1222 | static int |
| 1223 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1224 | { |
| 1225 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1226 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1227 | #else |
| 1228 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1229 | #endif |
| 1230 | if (PyErr_Occurred()) |
| 1231 | return 0; |
| 1232 | return 1; |
| 1233 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1234 | |
| 1235 | static PyObject * |
| 1236 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1237 | { |
| 1238 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1239 | return PyLong_FromLongLong(offset); |
| 1240 | #else |
| 1241 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1242 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1243 | } |
| 1244 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1245 | #ifdef HAVE_SIGSET_T |
| 1246 | /* Convert an iterable of integers to a sigset. |
| 1247 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1248 | int |
| 1249 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1250 | { |
| 1251 | sigset_t *mask = (sigset_t *)addr; |
| 1252 | PyObject *iterator, *item; |
| 1253 | long signum; |
| 1254 | int overflow; |
| 1255 | |
| 1256 | if (sigemptyset(mask)) { |
| 1257 | /* Probably only if mask == NULL. */ |
| 1258 | PyErr_SetFromErrno(PyExc_OSError); |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | iterator = PyObject_GetIter(obj); |
| 1263 | if (iterator == NULL) { |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1268 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1269 | Py_DECREF(item); |
| 1270 | if (signum <= 0 || signum >= NSIG) { |
| 1271 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1272 | PyErr_Format(PyExc_ValueError, |
| 1273 | "signal number %ld out of range", signum); |
| 1274 | } |
| 1275 | goto error; |
| 1276 | } |
| 1277 | if (sigaddset(mask, (int)signum)) { |
| 1278 | if (errno != EINVAL) { |
| 1279 | /* Probably impossible */ |
| 1280 | PyErr_SetFromErrno(PyExc_OSError); |
| 1281 | goto error; |
| 1282 | } |
| 1283 | /* For backwards compatibility, allow idioms such as |
| 1284 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1285 | */ |
| 1286 | const char msg[] = |
| 1287 | "invalid signal number %ld, please use valid_signals()"; |
| 1288 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1289 | goto error; |
| 1290 | } |
| 1291 | } |
| 1292 | } |
| 1293 | if (!PyErr_Occurred()) { |
| 1294 | Py_DECREF(iterator); |
| 1295 | return 1; |
| 1296 | } |
| 1297 | |
| 1298 | error: |
| 1299 | Py_DECREF(iterator); |
| 1300 | return 0; |
| 1301 | } |
| 1302 | #endif /* HAVE_SIGSET_T */ |
| 1303 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1304 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1305 | |
| 1306 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1307 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1308 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1309 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1310 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1311 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1312 | |
| 1313 | if (0 == DeviceIoControl( |
| 1314 | reparse_point_handle, |
| 1315 | FSCTL_GET_REPARSE_POINT, |
| 1316 | NULL, 0, /* in buffer */ |
| 1317 | target_buffer, sizeof(target_buffer), |
| 1318 | &n_bytes_returned, |
| 1319 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1320 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1321 | |
| 1322 | if (reparse_tag) |
| 1323 | *reparse_tag = rdb->ReparseTag; |
| 1324 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1325 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1326 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1327 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1328 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1329 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1330 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1331 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1332 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1333 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1334 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1335 | */ |
| 1336 | #include <crt_externs.h> |
| 1337 | static char **environ; |
| 1338 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1339 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1340 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1341 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1342 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1343 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1344 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1345 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1346 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1347 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1348 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1349 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1350 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1351 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1352 | d = PyDict_New(); |
| 1353 | if (d == NULL) |
| 1354 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1355 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1356 | if (environ == NULL) |
| 1357 | environ = *_NSGetEnviron(); |
| 1358 | #endif |
| 1359 | #ifdef MS_WINDOWS |
| 1360 | /* _wenviron must be initialized in this way if the program is started |
| 1361 | through main() instead of wmain(). */ |
| 1362 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1363 | e = _wenviron; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1364 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1365 | e = environ; |
| 1366 | #endif |
| 1367 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1368 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1369 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1370 | PyObject *k; |
| 1371 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1372 | #ifdef MS_WINDOWS |
| 1373 | const wchar_t *p = wcschr(*e, L'='); |
| 1374 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1375 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1376 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1377 | if (p == NULL) |
| 1378 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1379 | #ifdef MS_WINDOWS |
| 1380 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1381 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1382 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1383 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1384 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1385 | Py_DECREF(d); |
| 1386 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1387 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1388 | #ifdef MS_WINDOWS |
| 1389 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1390 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1391 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1392 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1393 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1394 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1395 | Py_DECREF(d); |
| 1396 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1397 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1398 | if (PyDict_GetItemWithError(d, k) == NULL) { |
| 1399 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { |
| 1400 | Py_DECREF(v); |
| 1401 | Py_DECREF(k); |
| 1402 | Py_DECREF(d); |
| 1403 | return NULL; |
| 1404 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1405 | } |
| 1406 | Py_DECREF(k); |
| 1407 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1408 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1409 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1412 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1413 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1414 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1415 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1416 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1417 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1418 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1419 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1420 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1421 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1422 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1423 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1424 | /* XXX We should pass the function name along in the future. |
| 1425 | (winreg.c also wants to pass the function name.) |
| 1426 | This would however require an additional param to the |
| 1427 | Windows error object, which is non-trivial. |
| 1428 | */ |
| 1429 | errno = GetLastError(); |
| 1430 | if (filename) |
| 1431 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1432 | else |
| 1433 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1434 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1435 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1436 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1437 | win32_error_object(const char* function, PyObject* filename) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1438 | { |
| 1439 | /* XXX - see win32_error for comments on 'function' */ |
| 1440 | errno = GetLastError(); |
| 1441 | if (filename) |
| 1442 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1443 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1444 | errno, |
| 1445 | filename); |
| 1446 | else |
| 1447 | return PyErr_SetFromWindowsErr(errno); |
| 1448 | } |
| 1449 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1450 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1451 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1452 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1453 | posix_path_object_error(PyObject *path) |
| 1454 | { |
| 1455 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1456 | } |
| 1457 | |
| 1458 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1459 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1460 | { |
| 1461 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1462 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1463 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1464 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1465 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1466 | #endif |
| 1467 | } |
| 1468 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1469 | static PyObject * |
| 1470 | path_object_error2(PyObject *path, PyObject *path2) |
| 1471 | { |
| 1472 | #ifdef MS_WINDOWS |
| 1473 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1474 | PyExc_OSError, 0, path, path2); |
| 1475 | #else |
| 1476 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1477 | #endif |
| 1478 | } |
| 1479 | |
| 1480 | static PyObject * |
| 1481 | path_error(path_t *path) |
| 1482 | { |
| 1483 | return path_object_error(path->object); |
| 1484 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1485 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1486 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1487 | posix_path_error(path_t *path) |
| 1488 | { |
| 1489 | return posix_path_object_error(path->object); |
| 1490 | } |
| 1491 | |
| 1492 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1493 | path_error2(path_t *path, path_t *path2) |
| 1494 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1495 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1499 | /* POSIX generic methods */ |
| 1500 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1501 | static int |
| 1502 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1503 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1504 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1505 | int *pointer = (int *)p; |
| 1506 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1507 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1508 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1509 | *pointer = fd; |
| 1510 | return 1; |
| 1511 | } |
| 1512 | |
| 1513 | static PyObject * |
| 1514 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1515 | { |
| 1516 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1517 | int async_err = 0; |
| 1518 | |
| 1519 | do { |
| 1520 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1521 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1522 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1523 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1524 | Py_END_ALLOW_THREADS |
| 1525 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1526 | if (res != 0) |
| 1527 | return (!async_err) ? posix_error() : NULL; |
| 1528 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1529 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1530 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1531 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1532 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1533 | /* This is a reimplementation of the C library's chdir function, |
| 1534 | but one that produces Win32 errors instead of DOS error codes. |
| 1535 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1536 | it also needs to set "magic" environment variables indicating |
| 1537 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1538 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1539 | win32_wchdir(LPCWSTR path) |
| 1540 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1541 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1542 | int result; |
| 1543 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1544 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1545 | if(!SetCurrentDirectoryW(path)) |
| 1546 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1547 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1548 | if (!result) |
| 1549 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1550 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1551 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1552 | if (!new_path) { |
| 1553 | SetLastError(ERROR_OUTOFMEMORY); |
| 1554 | return FALSE; |
| 1555 | } |
| 1556 | result = GetCurrentDirectoryW(result, new_path); |
| 1557 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1558 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1559 | return FALSE; |
| 1560 | } |
| 1561 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1562 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1563 | wcsncmp(new_path, L"//", 2) == 0); |
| 1564 | if (!is_unc_like_path) { |
| 1565 | env[1] = new_path[0]; |
| 1566 | result = SetEnvironmentVariableW(env, new_path); |
| 1567 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1568 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1569 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1570 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1571 | } |
| 1572 | #endif |
| 1573 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1574 | #ifdef MS_WINDOWS |
| 1575 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1576 | - time stamps are restricted to second resolution |
| 1577 | - file modification times suffer from forth-and-back conversions between |
| 1578 | UTC and local time |
| 1579 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1580 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1581 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1582 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1583 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1584 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1585 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1586 | BY_HANDLE_FILE_INFORMATION *info, |
| 1587 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1588 | { |
| 1589 | memset(info, 0, sizeof(*info)); |
| 1590 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1591 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1592 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1593 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1594 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1595 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1596 | /* info->nNumberOfLinks = 1; */ |
| 1597 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1598 | *reparse_tag = pFileData->dwReserved0; |
| 1599 | else |
| 1600 | *reparse_tag = 0; |
| 1601 | } |
| 1602 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1603 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1604 | 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] | 1605 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1606 | HANDLE hFindFile; |
| 1607 | WIN32_FIND_DATAW FileData; |
| 1608 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1609 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1610 | return FALSE; |
| 1611 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1612 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1613 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1616 | static BOOL |
| 1617 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1618 | { |
| 1619 | int buf_size, result_length; |
| 1620 | wchar_t *buf; |
| 1621 | |
| 1622 | /* We have a good handle to the target, use it to determine |
| 1623 | the target path name (then we'll call lstat on it). */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1624 | buf_size = GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1625 | VOLUME_NAME_DOS); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1626 | if(!buf_size) |
| 1627 | return FALSE; |
| 1628 | |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1629 | buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1630 | if (!buf) { |
| 1631 | SetLastError(ERROR_OUTOFMEMORY); |
| 1632 | return FALSE; |
| 1633 | } |
| 1634 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1635 | result_length = GetFinalPathNameByHandleW(hdl, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1636 | buf, buf_size, VOLUME_NAME_DOS); |
| 1637 | |
| 1638 | if(!result_length) { |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1639 | PyMem_RawFree(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1640 | return FALSE; |
| 1641 | } |
| 1642 | |
| 1643 | if(!CloseHandle(hdl)) { |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1644 | PyMem_RawFree(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1645 | return FALSE; |
| 1646 | } |
| 1647 | |
| 1648 | buf[result_length] = 0; |
| 1649 | |
| 1650 | *target_path = buf; |
| 1651 | return TRUE; |
| 1652 | } |
| 1653 | |
| 1654 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1655 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1656 | BOOL traverse) |
| 1657 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1658 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1659 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1660 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1661 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1662 | wchar_t *target_path; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1663 | const wchar_t *dot; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1664 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1665 | hFile = CreateFileW( |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1666 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1667 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1668 | 0, /* share mode */ |
| 1669 | NULL, /* security attributes */ |
| 1670 | OPEN_EXISTING, |
| 1671 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1672 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1673 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1674 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1675 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1676 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1677 | NULL); |
| 1678 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1679 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1680 | /* Either the target doesn't exist, or we don't have access to |
| 1681 | get a handle to it. If the former, we need to return an error. |
| 1682 | If the latter, we can use attributes_from_dir. */ |
Berker Peksag | 0b4dc48 | 2016-09-17 15:49:59 +0300 | [diff] [blame] | 1683 | DWORD lastError = GetLastError(); |
| 1684 | if (lastError != ERROR_ACCESS_DENIED && |
| 1685 | lastError != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1686 | return -1; |
| 1687 | /* Could not get attributes on open file. Fall back to |
| 1688 | reading the directory. */ |
| 1689 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1690 | /* Very strange. This should not fail now */ |
| 1691 | return -1; |
| 1692 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1693 | if (traverse) { |
| 1694 | /* Should traverse, but could not open reparse point handle */ |
Berker Peksag | 0b4dc48 | 2016-09-17 15:49:59 +0300 | [diff] [blame] | 1695 | SetLastError(lastError); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1696 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1697 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1698 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1699 | } else { |
| 1700 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1701 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1702 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1703 | } |
| 1704 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1705 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1706 | return -1; |
| 1707 | |
| 1708 | /* Close the outer open file handle now that we're about to |
| 1709 | reopen it with different flags. */ |
| 1710 | if (!CloseHandle(hFile)) |
| 1711 | return -1; |
| 1712 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1713 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1714 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1715 | the file without the reparse handling flag set. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1716 | hFile2 = CreateFileW( |
| 1717 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1718 | NULL, OPEN_EXISTING, |
| 1719 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1720 | NULL); |
| 1721 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1722 | return -1; |
| 1723 | |
| 1724 | if (!get_target_path(hFile2, &target_path)) |
| 1725 | return -1; |
| 1726 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1727 | code = win32_xstat_impl(target_path, result, FALSE); |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1728 | PyMem_RawFree(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1729 | return code; |
| 1730 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1731 | } else |
| 1732 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1733 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1734 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1735 | |
| 1736 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1737 | dot = wcsrchr(path, '.'); |
| 1738 | if (dot) { |
| 1739 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1740 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1741 | result->st_mode |= 0111; |
| 1742 | } |
| 1743 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1747 | 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] | 1748 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1749 | /* Protocol violation: we explicitly clear errno, instead of |
| 1750 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1751 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1752 | errno = 0; |
| 1753 | return code; |
| 1754 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1755 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1756 | |
| 1757 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1758 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1759 | default does not traverse symlinks and instead returns attributes for |
| 1760 | the symlink. |
| 1761 | |
| 1762 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1763 | win32_stat will first explicitly resolve the symlink target and then will |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1764 | call win32_lstat on that result. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1765 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1766 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1767 | 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] | 1768 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1769 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1772 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1773 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1774 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1775 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1778 | #endif /* MS_WINDOWS */ |
| 1779 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1780 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1781 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1782 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1783 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1784 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1785 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1786 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1787 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1788 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1789 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1790 | |
| 1791 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1792 | {"st_mode", "protection bits"}, |
| 1793 | {"st_ino", "inode"}, |
| 1794 | {"st_dev", "device"}, |
| 1795 | {"st_nlink", "number of hard links"}, |
| 1796 | {"st_uid", "user ID of owner"}, |
| 1797 | {"st_gid", "group ID of owner"}, |
| 1798 | {"st_size", "total size, in bytes"}, |
| 1799 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1800 | {NULL, "integer time of last access"}, |
| 1801 | {NULL, "integer time of last modification"}, |
| 1802 | {NULL, "integer time of last change"}, |
| 1803 | {"st_atime", "time of last access"}, |
| 1804 | {"st_mtime", "time of last modification"}, |
| 1805 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1806 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1807 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1808 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1809 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1810 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1811 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1812 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1813 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1814 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1815 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1816 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1817 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1818 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1819 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1820 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1821 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1822 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1823 | #endif |
| 1824 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1825 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1826 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1827 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1828 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1829 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1830 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1831 | {"st_fstype", "Type of filesystem"}, |
| 1832 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1833 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1834 | }; |
| 1835 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1836 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1837 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1838 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1839 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1840 | #endif |
| 1841 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1842 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1843 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1844 | #else |
| 1845 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1846 | #endif |
| 1847 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1848 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1849 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1850 | #else |
| 1851 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1852 | #endif |
| 1853 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1854 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1855 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1856 | #else |
| 1857 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1858 | #endif |
| 1859 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1860 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1861 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1862 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1863 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1864 | #endif |
| 1865 | |
| 1866 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1867 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1868 | #else |
| 1869 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1870 | #endif |
| 1871 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1872 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1873 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1874 | #else |
| 1875 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 1876 | #endif |
| 1877 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1878 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1879 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 1880 | #else |
| 1881 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 1882 | #endif |
| 1883 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1884 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1885 | "stat_result", /* name */ |
| 1886 | stat_result__doc__, /* doc */ |
| 1887 | stat_result_fields, |
| 1888 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1889 | }; |
| 1890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1891 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1892 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1893 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1894 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1895 | 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] | 1896 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1897 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1898 | |
| 1899 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1900 | {"f_bsize", }, |
| 1901 | {"f_frsize", }, |
| 1902 | {"f_blocks", }, |
| 1903 | {"f_bfree", }, |
| 1904 | {"f_bavail", }, |
| 1905 | {"f_files", }, |
| 1906 | {"f_ffree", }, |
| 1907 | {"f_favail", }, |
| 1908 | {"f_flag", }, |
| 1909 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 1910 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1911 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1912 | }; |
| 1913 | |
| 1914 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1915 | "statvfs_result", /* name */ |
| 1916 | statvfs_result__doc__, /* doc */ |
| 1917 | statvfs_result_fields, |
| 1918 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1919 | }; |
| 1920 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1921 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1922 | PyDoc_STRVAR(waitid_result__doc__, |
| 1923 | "waitid_result: Result from waitid.\n\n\ |
| 1924 | This object may be accessed either as a tuple of\n\ |
| 1925 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1926 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1927 | \n\ |
| 1928 | See os.waitid for more information."); |
| 1929 | |
| 1930 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1931 | {"si_pid", }, |
| 1932 | {"si_uid", }, |
| 1933 | {"si_signo", }, |
| 1934 | {"si_status", }, |
| 1935 | {"si_code", }, |
| 1936 | {0} |
| 1937 | }; |
| 1938 | |
| 1939 | static PyStructSequence_Desc waitid_result_desc = { |
| 1940 | "waitid_result", /* name */ |
| 1941 | waitid_result__doc__, /* doc */ |
| 1942 | waitid_result_fields, |
| 1943 | 5 |
| 1944 | }; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 1945 | static PyTypeObject* WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1946 | #endif |
| 1947 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1948 | static int initialized; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 1949 | static PyTypeObject* StatResultType; |
| 1950 | static PyTypeObject* StatVFSResultType; |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 1951 | #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] | 1952 | static PyTypeObject* SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1953 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1954 | static newfunc structseq_new; |
| 1955 | |
| 1956 | static PyObject * |
| 1957 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1958 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1959 | PyStructSequence *result; |
| 1960 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1961 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1962 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1963 | if (!result) |
| 1964 | return NULL; |
| 1965 | /* If we have been initialized from a tuple, |
| 1966 | st_?time might be set to None. Initialize it |
| 1967 | from the int slots. */ |
| 1968 | for (i = 7; i <= 9; i++) { |
| 1969 | if (result->ob_item[i+3] == Py_None) { |
| 1970 | Py_DECREF(Py_None); |
| 1971 | Py_INCREF(result->ob_item[i]); |
| 1972 | result->ob_item[i+3] = result->ob_item[i]; |
| 1973 | } |
| 1974 | } |
| 1975 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1979 | static PyObject *billion = NULL; |
| 1980 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1981 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1982 | 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] | 1983 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1984 | PyObject *s = _PyLong_FromTime_t(sec); |
| 1985 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 1986 | PyObject *s_in_ns = NULL; |
| 1987 | PyObject *ns_total = NULL; |
| 1988 | PyObject *float_s = NULL; |
| 1989 | |
| 1990 | if (!(s && ns_fractional)) |
| 1991 | goto exit; |
| 1992 | |
| 1993 | s_in_ns = PyNumber_Multiply(s, billion); |
| 1994 | if (!s_in_ns) |
| 1995 | goto exit; |
| 1996 | |
| 1997 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 1998 | if (!ns_total) |
| 1999 | goto exit; |
| 2000 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2001 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2002 | if (!float_s) { |
| 2003 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | PyStructSequence_SET_ITEM(v, index, s); |
| 2007 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2008 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2009 | s = NULL; |
| 2010 | float_s = NULL; |
| 2011 | ns_total = NULL; |
| 2012 | exit: |
| 2013 | Py_XDECREF(s); |
| 2014 | Py_XDECREF(ns_fractional); |
| 2015 | Py_XDECREF(s_in_ns); |
| 2016 | Py_XDECREF(ns_total); |
| 2017 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2020 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2021 | (used by posix_stat() and posix_fstat()) */ |
| 2022 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2023 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2024 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2025 | unsigned long ansec, mnsec, cnsec; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2026 | PyObject *v = PyStructSequence_New(StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2027 | if (v == NULL) |
| 2028 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2029 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2030 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2031 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2032 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2033 | #ifdef MS_WINDOWS |
| 2034 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2035 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2036 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2037 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2038 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2039 | #if defined(MS_WINDOWS) |
| 2040 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2041 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2042 | #else |
| 2043 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2044 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2045 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2046 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2047 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2048 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2049 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2050 | ansec = st->st_atim.tv_nsec; |
| 2051 | mnsec = st->st_mtim.tv_nsec; |
| 2052 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2053 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2054 | ansec = st->st_atimespec.tv_nsec; |
| 2055 | mnsec = st->st_mtimespec.tv_nsec; |
| 2056 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2057 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2058 | ansec = st->st_atime_nsec; |
| 2059 | mnsec = st->st_mtime_nsec; |
| 2060 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2061 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2062 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2063 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2064 | fill_time(v, 7, st->st_atime, ansec); |
| 2065 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2066 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2067 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2068 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2069 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2070 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2071 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2072 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2073 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2074 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2075 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2076 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2077 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2078 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2079 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2080 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2081 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2082 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2083 | #endif |
| 2084 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2085 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2086 | PyObject *val; |
| 2087 | unsigned long bsec,bnsec; |
| 2088 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2089 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2090 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2091 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2092 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2093 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2094 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2095 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2096 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2097 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2098 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2099 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2100 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2101 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2102 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2103 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2104 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2105 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2106 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2107 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2108 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2109 | PyUnicode_FromString(st->st_fstype)); |
| 2110 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2111 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2112 | if (PyErr_Occurred()) { |
| 2113 | Py_DECREF(v); |
| 2114 | return NULL; |
| 2115 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2116 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2117 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2120 | /* POSIX methods */ |
| 2121 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2122 | |
| 2123 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2124 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2125 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2126 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2127 | STRUCT_STAT st; |
| 2128 | int result; |
| 2129 | |
| 2130 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2131 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2132 | return NULL; |
| 2133 | #endif |
| 2134 | |
| 2135 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2136 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2137 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2138 | return NULL; |
| 2139 | |
| 2140 | Py_BEGIN_ALLOW_THREADS |
| 2141 | if (path->fd != -1) |
| 2142 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2143 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2144 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2145 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2146 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2147 | result = win32_lstat(path->wide, &st); |
| 2148 | #else |
| 2149 | else |
| 2150 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2151 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2152 | result = LSTAT(path->narrow, &st); |
| 2153 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2154 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2155 | #ifdef HAVE_FSTATAT |
| 2156 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2157 | result = fstatat(dir_fd, path->narrow, &st, |
| 2158 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2159 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2160 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2161 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2162 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2163 | Py_END_ALLOW_THREADS |
| 2164 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2165 | if (result != 0) { |
| 2166 | return path_error(path); |
| 2167 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2168 | |
| 2169 | return _pystat_fromstructstat(&st); |
| 2170 | } |
| 2171 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2172 | /*[python input] |
| 2173 | |
| 2174 | for s in """ |
| 2175 | |
| 2176 | FACCESSAT |
| 2177 | FCHMODAT |
| 2178 | FCHOWNAT |
| 2179 | FSTATAT |
| 2180 | LINKAT |
| 2181 | MKDIRAT |
| 2182 | MKFIFOAT |
| 2183 | MKNODAT |
| 2184 | OPENAT |
| 2185 | READLINKAT |
| 2186 | SYMLINKAT |
| 2187 | UNLINKAT |
| 2188 | |
| 2189 | """.strip().split(): |
| 2190 | s = s.strip() |
| 2191 | print(""" |
| 2192 | #ifdef HAVE_{s} |
| 2193 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2194 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2195 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2196 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2197 | """.rstrip().format(s=s)) |
| 2198 | |
| 2199 | for s in """ |
| 2200 | |
| 2201 | FCHDIR |
| 2202 | FCHMOD |
| 2203 | FCHOWN |
| 2204 | FDOPENDIR |
| 2205 | FEXECVE |
| 2206 | FPATHCONF |
| 2207 | FSTATVFS |
| 2208 | FTRUNCATE |
| 2209 | |
| 2210 | """.strip().split(): |
| 2211 | s = s.strip() |
| 2212 | print(""" |
| 2213 | #ifdef HAVE_{s} |
| 2214 | #define PATH_HAVE_{s} 1 |
| 2215 | #else |
| 2216 | #define PATH_HAVE_{s} 0 |
| 2217 | #endif |
| 2218 | |
| 2219 | """.rstrip().format(s=s)) |
| 2220 | [python start generated code]*/ |
| 2221 | |
| 2222 | #ifdef HAVE_FACCESSAT |
| 2223 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2224 | #else |
| 2225 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2226 | #endif |
| 2227 | |
| 2228 | #ifdef HAVE_FCHMODAT |
| 2229 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2230 | #else |
| 2231 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2232 | #endif |
| 2233 | |
| 2234 | #ifdef HAVE_FCHOWNAT |
| 2235 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2236 | #else |
| 2237 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2238 | #endif |
| 2239 | |
| 2240 | #ifdef HAVE_FSTATAT |
| 2241 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2242 | #else |
| 2243 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2244 | #endif |
| 2245 | |
| 2246 | #ifdef HAVE_LINKAT |
| 2247 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2248 | #else |
| 2249 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2250 | #endif |
| 2251 | |
| 2252 | #ifdef HAVE_MKDIRAT |
| 2253 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2254 | #else |
| 2255 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2256 | #endif |
| 2257 | |
| 2258 | #ifdef HAVE_MKFIFOAT |
| 2259 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2260 | #else |
| 2261 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2262 | #endif |
| 2263 | |
| 2264 | #ifdef HAVE_MKNODAT |
| 2265 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2266 | #else |
| 2267 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2268 | #endif |
| 2269 | |
| 2270 | #ifdef HAVE_OPENAT |
| 2271 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2272 | #else |
| 2273 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2274 | #endif |
| 2275 | |
| 2276 | #ifdef HAVE_READLINKAT |
| 2277 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2278 | #else |
| 2279 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2280 | #endif |
| 2281 | |
| 2282 | #ifdef HAVE_SYMLINKAT |
| 2283 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2284 | #else |
| 2285 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2286 | #endif |
| 2287 | |
| 2288 | #ifdef HAVE_UNLINKAT |
| 2289 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2290 | #else |
| 2291 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2292 | #endif |
| 2293 | |
| 2294 | #ifdef HAVE_FCHDIR |
| 2295 | #define PATH_HAVE_FCHDIR 1 |
| 2296 | #else |
| 2297 | #define PATH_HAVE_FCHDIR 0 |
| 2298 | #endif |
| 2299 | |
| 2300 | #ifdef HAVE_FCHMOD |
| 2301 | #define PATH_HAVE_FCHMOD 1 |
| 2302 | #else |
| 2303 | #define PATH_HAVE_FCHMOD 0 |
| 2304 | #endif |
| 2305 | |
| 2306 | #ifdef HAVE_FCHOWN |
| 2307 | #define PATH_HAVE_FCHOWN 1 |
| 2308 | #else |
| 2309 | #define PATH_HAVE_FCHOWN 0 |
| 2310 | #endif |
| 2311 | |
| 2312 | #ifdef HAVE_FDOPENDIR |
| 2313 | #define PATH_HAVE_FDOPENDIR 1 |
| 2314 | #else |
| 2315 | #define PATH_HAVE_FDOPENDIR 0 |
| 2316 | #endif |
| 2317 | |
| 2318 | #ifdef HAVE_FEXECVE |
| 2319 | #define PATH_HAVE_FEXECVE 1 |
| 2320 | #else |
| 2321 | #define PATH_HAVE_FEXECVE 0 |
| 2322 | #endif |
| 2323 | |
| 2324 | #ifdef HAVE_FPATHCONF |
| 2325 | #define PATH_HAVE_FPATHCONF 1 |
| 2326 | #else |
| 2327 | #define PATH_HAVE_FPATHCONF 0 |
| 2328 | #endif |
| 2329 | |
| 2330 | #ifdef HAVE_FSTATVFS |
| 2331 | #define PATH_HAVE_FSTATVFS 1 |
| 2332 | #else |
| 2333 | #define PATH_HAVE_FSTATVFS 0 |
| 2334 | #endif |
| 2335 | |
| 2336 | #ifdef HAVE_FTRUNCATE |
| 2337 | #define PATH_HAVE_FTRUNCATE 1 |
| 2338 | #else |
| 2339 | #define PATH_HAVE_FTRUNCATE 0 |
| 2340 | #endif |
| 2341 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2342 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2343 | #ifdef MS_WINDOWS |
| 2344 | #undef PATH_HAVE_FTRUNCATE |
| 2345 | #define PATH_HAVE_FTRUNCATE 1 |
| 2346 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2347 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2348 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2349 | |
| 2350 | class path_t_converter(CConverter): |
| 2351 | |
| 2352 | type = "path_t" |
| 2353 | impl_by_reference = True |
| 2354 | parse_by_reference = True |
| 2355 | |
| 2356 | converter = 'path_converter' |
| 2357 | |
| 2358 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2359 | # right now path_t doesn't support default values. |
| 2360 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2361 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2362 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2363 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2364 | if self.c_default not in (None, 'Py_None'): |
| 2365 | 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] | 2366 | |
| 2367 | self.nullable = nullable |
| 2368 | self.allow_fd = allow_fd |
| 2369 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2370 | def pre_render(self): |
| 2371 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2372 | if isinstance(value, str): |
| 2373 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2374 | return str(int(bool(value))) |
| 2375 | |
| 2376 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2377 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2378 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2379 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2380 | strify(self.nullable), |
| 2381 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2382 | ) |
| 2383 | |
| 2384 | def cleanup(self): |
| 2385 | return "path_cleanup(&" + self.name + ");\n" |
| 2386 | |
| 2387 | |
| 2388 | class dir_fd_converter(CConverter): |
| 2389 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2390 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2391 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2392 | if self.default in (unspecified, None): |
| 2393 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2394 | if isinstance(requires, str): |
| 2395 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2396 | else: |
| 2397 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2398 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2399 | class fildes_converter(CConverter): |
| 2400 | type = 'int' |
| 2401 | converter = 'fildes_converter' |
| 2402 | |
| 2403 | class uid_t_converter(CConverter): |
| 2404 | type = "uid_t" |
| 2405 | converter = '_Py_Uid_Converter' |
| 2406 | |
| 2407 | class gid_t_converter(CConverter): |
| 2408 | type = "gid_t" |
| 2409 | converter = '_Py_Gid_Converter' |
| 2410 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2411 | class dev_t_converter(CConverter): |
| 2412 | type = 'dev_t' |
| 2413 | converter = '_Py_Dev_Converter' |
| 2414 | |
| 2415 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2416 | type = 'dev_t' |
| 2417 | conversion_fn = '_PyLong_FromDev' |
| 2418 | unsigned_cast = '(dev_t)' |
| 2419 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2420 | class FSConverter_converter(CConverter): |
| 2421 | type = 'PyObject *' |
| 2422 | converter = 'PyUnicode_FSConverter' |
| 2423 | def converter_init(self): |
| 2424 | if self.default is not unspecified: |
| 2425 | fail("FSConverter_converter does not support default values") |
| 2426 | self.c_default = 'NULL' |
| 2427 | |
| 2428 | def cleanup(self): |
| 2429 | return "Py_XDECREF(" + self.name + ");\n" |
| 2430 | |
| 2431 | class pid_t_converter(CConverter): |
| 2432 | type = 'pid_t' |
| 2433 | format_unit = '" _Py_PARSE_PID "' |
| 2434 | |
| 2435 | class idtype_t_converter(int_converter): |
| 2436 | type = 'idtype_t' |
| 2437 | |
| 2438 | class id_t_converter(CConverter): |
| 2439 | type = 'id_t' |
| 2440 | format_unit = '" _Py_PARSE_PID "' |
| 2441 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2442 | class intptr_t_converter(CConverter): |
| 2443 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2444 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2445 | |
| 2446 | class Py_off_t_converter(CConverter): |
| 2447 | type = 'Py_off_t' |
| 2448 | converter = 'Py_off_t_converter' |
| 2449 | |
| 2450 | class Py_off_t_return_converter(long_return_converter): |
| 2451 | type = 'Py_off_t' |
| 2452 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2453 | |
| 2454 | class path_confname_converter(CConverter): |
| 2455 | type="int" |
| 2456 | converter="conv_path_confname" |
| 2457 | |
| 2458 | class confstr_confname_converter(path_confname_converter): |
| 2459 | converter='conv_confstr_confname' |
| 2460 | |
| 2461 | class sysconf_confname_converter(path_confname_converter): |
| 2462 | converter="conv_sysconf_confname" |
| 2463 | |
| 2464 | class sched_param_converter(CConverter): |
| 2465 | type = 'struct sched_param' |
| 2466 | converter = 'convert_sched_param' |
| 2467 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2468 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2469 | [python start generated code]*/ |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 2470 | /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2471 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2472 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2473 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2474 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2475 | |
| 2476 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2477 | 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] | 2478 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2479 | |
| 2480 | * |
| 2481 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2482 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2483 | If not None, it should be a file descriptor open to a directory, |
| 2484 | and path should be a relative string; path will then be relative to |
| 2485 | that directory. |
| 2486 | |
| 2487 | follow_symlinks: bool = True |
| 2488 | If False, and the last element of the path is a symbolic link, |
| 2489 | stat will examine the symbolic link itself instead of the file |
| 2490 | the link points to. |
| 2491 | |
| 2492 | Perform a stat system call on the given path. |
| 2493 | |
| 2494 | dir_fd and follow_symlinks may not be implemented |
| 2495 | on your platform. If they are unavailable, using them will raise a |
| 2496 | NotImplementedError. |
| 2497 | |
| 2498 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2499 | an open file descriptor. |
| 2500 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2501 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2502 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2503 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2504 | 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] | 2505 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2506 | { |
| 2507 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2508 | } |
| 2509 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2510 | |
| 2511 | /*[clinic input] |
| 2512 | os.lstat |
| 2513 | |
| 2514 | path : path_t |
| 2515 | |
| 2516 | * |
| 2517 | |
| 2518 | dir_fd : dir_fd(requires='fstatat') = None |
| 2519 | |
| 2520 | Perform a stat system call on the given path, without following symbolic links. |
| 2521 | |
| 2522 | Like stat(), but do not follow symbolic links. |
| 2523 | Equivalent to stat(path, follow_symlinks=False). |
| 2524 | [clinic start generated code]*/ |
| 2525 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2526 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2527 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2528 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2529 | { |
| 2530 | int follow_symlinks = 0; |
| 2531 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2532 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2533 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2534 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2535 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2536 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2537 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2538 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2539 | 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] | 2540 | |
| 2541 | mode: int |
| 2542 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2543 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2544 | |
| 2545 | * |
| 2546 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2547 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2548 | If not None, it should be a file descriptor open to a directory, |
| 2549 | and path should be relative; path will then be relative to that |
| 2550 | directory. |
| 2551 | |
| 2552 | effective_ids: bool = False |
| 2553 | If True, access will use the effective uid/gid instead of |
| 2554 | the real uid/gid. |
| 2555 | |
| 2556 | follow_symlinks: bool = True |
| 2557 | If False, and the last element of the path is a symbolic link, |
| 2558 | access will examine the symbolic link itself instead of the file |
| 2559 | the link points to. |
| 2560 | |
| 2561 | Use the real uid/gid to test for access to a path. |
| 2562 | |
| 2563 | {parameters} |
| 2564 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2565 | on your platform. If they are unavailable, using them will raise a |
| 2566 | NotImplementedError. |
| 2567 | |
| 2568 | Note that most operations will use the effective uid/gid, therefore this |
| 2569 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2570 | has the specified access to the path. |
| 2571 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2572 | [clinic start generated code]*/ |
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 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2575 | 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] | 2576 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2577 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2578 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2579 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2580 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2581 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2582 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2583 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2584 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2585 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2586 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2587 | #ifndef HAVE_FACCESSAT |
| 2588 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2589 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2590 | |
| 2591 | if (effective_ids) { |
| 2592 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2593 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2594 | } |
| 2595 | #endif |
| 2596 | |
| 2597 | #ifdef MS_WINDOWS |
| 2598 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2599 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2600 | Py_END_ALLOW_THREADS |
| 2601 | |
| 2602 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2603 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2604 | * * we didn't get a -1, and |
| 2605 | * * write access wasn't requested, |
| 2606 | * * or the file isn't read-only, |
| 2607 | * * or it's a directory. |
| 2608 | * (Directories cannot be read-only on Windows.) |
| 2609 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2610 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2611 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2612 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2613 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2614 | #else |
| 2615 | |
| 2616 | Py_BEGIN_ALLOW_THREADS |
| 2617 | #ifdef HAVE_FACCESSAT |
| 2618 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2619 | effective_ids || |
| 2620 | !follow_symlinks) { |
| 2621 | int flags = 0; |
| 2622 | if (!follow_symlinks) |
| 2623 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2624 | if (effective_ids) |
| 2625 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2626 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2627 | } |
| 2628 | else |
| 2629 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2630 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2631 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2632 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2633 | #endif |
| 2634 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2635 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2638 | #ifndef F_OK |
| 2639 | #define F_OK 0 |
| 2640 | #endif |
| 2641 | #ifndef R_OK |
| 2642 | #define R_OK 4 |
| 2643 | #endif |
| 2644 | #ifndef W_OK |
| 2645 | #define W_OK 2 |
| 2646 | #endif |
| 2647 | #ifndef X_OK |
| 2648 | #define X_OK 1 |
| 2649 | #endif |
| 2650 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2651 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2652 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2653 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2654 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2655 | |
| 2656 | fd: int |
| 2657 | Integer file descriptor handle. |
| 2658 | |
| 2659 | / |
| 2660 | |
| 2661 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2662 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2663 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2664 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2665 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2666 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2667 | { |
| 2668 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2669 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2670 | ret = ttyname(fd); |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2671 | if (ret == NULL) { |
| 2672 | return posix_error(); |
| 2673 | } |
| 2674 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2675 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2676 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2677 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2678 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2679 | /*[clinic input] |
| 2680 | os.ctermid |
| 2681 | |
| 2682 | Return the name of the controlling terminal for this process. |
| 2683 | [clinic start generated code]*/ |
| 2684 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2685 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2686 | os_ctermid_impl(PyObject *module) |
| 2687 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2688 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2689 | char *ret; |
| 2690 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2691 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2692 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2693 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2694 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2695 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2696 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2697 | if (ret == NULL) |
| 2698 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2699 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2700 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2701 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2702 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2703 | |
| 2704 | /*[clinic input] |
| 2705 | os.chdir |
| 2706 | |
| 2707 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2708 | |
| 2709 | Change the current working directory to the specified path. |
| 2710 | |
| 2711 | path may always be specified as a string. |
| 2712 | On some platforms, path may also be specified as an open file descriptor. |
| 2713 | If this functionality is unavailable, using it raises an exception. |
| 2714 | [clinic start generated code]*/ |
| 2715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2716 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2717 | os_chdir_impl(PyObject *module, path_t *path) |
| 2718 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2719 | { |
| 2720 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2721 | |
| 2722 | Py_BEGIN_ALLOW_THREADS |
| 2723 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2724 | /* on unix, success = 0, on windows, success = !0 */ |
| 2725 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2726 | #else |
| 2727 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2728 | if (path->fd != -1) |
| 2729 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2730 | else |
| 2731 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2732 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2733 | #endif |
| 2734 | Py_END_ALLOW_THREADS |
| 2735 | |
| 2736 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2737 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2738 | } |
| 2739 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2740 | Py_RETURN_NONE; |
| 2741 | } |
| 2742 | |
| 2743 | |
| 2744 | #ifdef HAVE_FCHDIR |
| 2745 | /*[clinic input] |
| 2746 | os.fchdir |
| 2747 | |
| 2748 | fd: fildes |
| 2749 | |
| 2750 | Change to the directory of the given file descriptor. |
| 2751 | |
| 2752 | fd must be opened on a directory, not a file. |
| 2753 | Equivalent to os.chdir(fd). |
| 2754 | |
| 2755 | [clinic start generated code]*/ |
| 2756 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2757 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2758 | os_fchdir_impl(PyObject *module, int fd) |
| 2759 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2760 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2761 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2762 | } |
| 2763 | #endif /* HAVE_FCHDIR */ |
| 2764 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2765 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2766 | /*[clinic input] |
| 2767 | os.chmod |
| 2768 | |
| 2769 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2770 | 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] | 2771 | On some platforms, path may also be specified as an open file descriptor. |
| 2772 | If this functionality is unavailable, using it raises an exception. |
| 2773 | |
| 2774 | mode: int |
| 2775 | Operating-system mode bitfield. |
| 2776 | |
| 2777 | * |
| 2778 | |
| 2779 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2780 | If not None, it should be a file descriptor open to a directory, |
| 2781 | and path should be relative; path will then be relative to that |
| 2782 | directory. |
| 2783 | |
| 2784 | follow_symlinks: bool = True |
| 2785 | If False, and the last element of the path is a symbolic link, |
| 2786 | chmod will modify the symbolic link itself instead of the file |
| 2787 | the link points to. |
| 2788 | |
| 2789 | Change the access permissions of a file. |
| 2790 | |
| 2791 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2792 | an open file descriptor. |
| 2793 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2794 | If they are unavailable, using them will raise a NotImplementedError. |
| 2795 | |
| 2796 | [clinic start generated code]*/ |
| 2797 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2798 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2799 | 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] | 2800 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2801 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2802 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2803 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2804 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2805 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2806 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2807 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2808 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2809 | #ifdef HAVE_FCHMODAT |
| 2810 | int fchmodat_nofollow_unsupported = 0; |
| 2811 | #endif |
| 2812 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2813 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2814 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2815 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2816 | #endif |
| 2817 | |
| 2818 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2819 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2820 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2821 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2822 | result = 0; |
| 2823 | else { |
| 2824 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2825 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2826 | else |
| 2827 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2828 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2829 | } |
| 2830 | Py_END_ALLOW_THREADS |
| 2831 | |
| 2832 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2833 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2834 | } |
| 2835 | #else /* MS_WINDOWS */ |
| 2836 | Py_BEGIN_ALLOW_THREADS |
| 2837 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2838 | if (path->fd != -1) |
| 2839 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2840 | else |
| 2841 | #endif |
| 2842 | #ifdef HAVE_LCHMOD |
| 2843 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2844 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2845 | else |
| 2846 | #endif |
| 2847 | #ifdef HAVE_FCHMODAT |
| 2848 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2849 | /* |
| 2850 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2851 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2852 | * and then says it isn't implemented yet. |
| 2853 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2854 | * |
| 2855 | * Once it is supported, os.chmod will automatically |
| 2856 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2857 | * Until then, we need to be careful what exception we raise. |
| 2858 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2859 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2860 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2861 | /* |
| 2862 | * But wait! We can't throw the exception without allowing threads, |
| 2863 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2864 | */ |
| 2865 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2866 | result && |
| 2867 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2868 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2869 | } |
| 2870 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2871 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2872 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2873 | Py_END_ALLOW_THREADS |
| 2874 | |
| 2875 | if (result) { |
| 2876 | #ifdef HAVE_FCHMODAT |
| 2877 | if (fchmodat_nofollow_unsupported) { |
| 2878 | if (dir_fd != DEFAULT_DIR_FD) |
| 2879 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2880 | dir_fd, follow_symlinks); |
| 2881 | else |
| 2882 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 2883 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2884 | } |
| 2885 | else |
| 2886 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2887 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2888 | } |
| 2889 | #endif |
| 2890 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2891 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2894 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2895 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2896 | /*[clinic input] |
| 2897 | os.fchmod |
| 2898 | |
| 2899 | fd: int |
| 2900 | mode: int |
| 2901 | |
| 2902 | Change the access permissions of the file given by file descriptor fd. |
| 2903 | |
| 2904 | Equivalent to os.chmod(fd, mode). |
| 2905 | [clinic start generated code]*/ |
| 2906 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2907 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2908 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 2909 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2910 | { |
| 2911 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2912 | int async_err = 0; |
| 2913 | |
| 2914 | do { |
| 2915 | Py_BEGIN_ALLOW_THREADS |
| 2916 | res = fchmod(fd, mode); |
| 2917 | Py_END_ALLOW_THREADS |
| 2918 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 2919 | if (res != 0) |
| 2920 | return (!async_err) ? posix_error() : NULL; |
| 2921 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2922 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2923 | } |
| 2924 | #endif /* HAVE_FCHMOD */ |
| 2925 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2926 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2927 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2928 | /*[clinic input] |
| 2929 | os.lchmod |
| 2930 | |
| 2931 | path: path_t |
| 2932 | mode: int |
| 2933 | |
| 2934 | Change the access permissions of a file, without following symbolic links. |
| 2935 | |
| 2936 | If path is a symlink, this affects the link itself rather than the target. |
| 2937 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 2938 | [clinic start generated code]*/ |
| 2939 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2940 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2941 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 2942 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2943 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2944 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2945 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2946 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2947 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2948 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2949 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2950 | return NULL; |
| 2951 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2952 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2953 | } |
| 2954 | #endif /* HAVE_LCHMOD */ |
| 2955 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2956 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2957 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2958 | /*[clinic input] |
| 2959 | os.chflags |
| 2960 | |
| 2961 | path: path_t |
| 2962 | flags: unsigned_long(bitwise=True) |
| 2963 | follow_symlinks: bool=True |
| 2964 | |
| 2965 | Set file flags. |
| 2966 | |
| 2967 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 2968 | link, chflags will change flags on the symbolic link itself instead of the |
| 2969 | file the link points to. |
| 2970 | follow_symlinks may not be implemented on your platform. If it is |
| 2971 | unavailable, using it will raise a NotImplementedError. |
| 2972 | |
| 2973 | [clinic start generated code]*/ |
| 2974 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2975 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2976 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2977 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2978 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2979 | { |
| 2980 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2981 | |
| 2982 | #ifndef HAVE_LCHFLAGS |
| 2983 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2984 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2985 | #endif |
| 2986 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2987 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2988 | #ifdef HAVE_LCHFLAGS |
| 2989 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2990 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2991 | else |
| 2992 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2993 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2994 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2995 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2996 | if (result) |
| 2997 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2998 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2999 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3000 | } |
| 3001 | #endif /* HAVE_CHFLAGS */ |
| 3002 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3003 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3004 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3005 | /*[clinic input] |
| 3006 | os.lchflags |
| 3007 | |
| 3008 | path: path_t |
| 3009 | flags: unsigned_long(bitwise=True) |
| 3010 | |
| 3011 | Set file flags. |
| 3012 | |
| 3013 | This function will not follow symbolic links. |
| 3014 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3015 | [clinic start generated code]*/ |
| 3016 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3017 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3018 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3019 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3020 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3021 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3022 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3023 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3024 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3025 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3026 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3027 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3028 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3029 | } |
| 3030 | #endif /* HAVE_LCHFLAGS */ |
| 3031 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3032 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3033 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3034 | /*[clinic input] |
| 3035 | os.chroot |
| 3036 | path: path_t |
| 3037 | |
| 3038 | Change root directory to path. |
| 3039 | |
| 3040 | [clinic start generated code]*/ |
| 3041 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3042 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3043 | os_chroot_impl(PyObject *module, path_t *path) |
| 3044 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3045 | { |
| 3046 | int res; |
| 3047 | Py_BEGIN_ALLOW_THREADS |
| 3048 | res = chroot(path->narrow); |
| 3049 | Py_END_ALLOW_THREADS |
| 3050 | if (res < 0) |
| 3051 | return path_error(path); |
| 3052 | Py_RETURN_NONE; |
| 3053 | } |
| 3054 | #endif /* HAVE_CHROOT */ |
| 3055 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3056 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3057 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3058 | /*[clinic input] |
| 3059 | os.fsync |
| 3060 | |
| 3061 | fd: fildes |
| 3062 | |
| 3063 | Force write of fd to disk. |
| 3064 | [clinic start generated code]*/ |
| 3065 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3066 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3067 | os_fsync_impl(PyObject *module, int fd) |
| 3068 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3069 | { |
| 3070 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3071 | } |
| 3072 | #endif /* HAVE_FSYNC */ |
| 3073 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3074 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3075 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3076 | /*[clinic input] |
| 3077 | os.sync |
| 3078 | |
| 3079 | Force write of everything to disk. |
| 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_sync_impl(PyObject *module) |
| 3084 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3085 | { |
| 3086 | Py_BEGIN_ALLOW_THREADS |
| 3087 | sync(); |
| 3088 | Py_END_ALLOW_THREADS |
| 3089 | Py_RETURN_NONE; |
| 3090 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3091 | #endif /* HAVE_SYNC */ |
| 3092 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3093 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3094 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3095 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3096 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3097 | #endif |
| 3098 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3099 | /*[clinic input] |
| 3100 | os.fdatasync |
| 3101 | |
| 3102 | fd: fildes |
| 3103 | |
| 3104 | Force write of fd to disk without forcing update of metadata. |
| 3105 | [clinic start generated code]*/ |
| 3106 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3107 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3108 | os_fdatasync_impl(PyObject *module, int fd) |
| 3109 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3110 | { |
| 3111 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3112 | } |
| 3113 | #endif /* HAVE_FDATASYNC */ |
| 3114 | |
| 3115 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3116 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3117 | /*[clinic input] |
| 3118 | os.chown |
| 3119 | |
| 3120 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3121 | 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] | 3122 | |
| 3123 | uid: uid_t |
| 3124 | |
| 3125 | gid: gid_t |
| 3126 | |
| 3127 | * |
| 3128 | |
| 3129 | dir_fd : dir_fd(requires='fchownat') = None |
| 3130 | If not None, it should be a file descriptor open to a directory, |
| 3131 | and path should be relative; path will then be relative to that |
| 3132 | directory. |
| 3133 | |
| 3134 | follow_symlinks: bool = True |
| 3135 | If False, and the last element of the path is a symbolic link, |
| 3136 | stat will examine the symbolic link itself instead of the file |
| 3137 | the link points to. |
| 3138 | |
| 3139 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3140 | |
| 3141 | path may always be specified as a string. |
| 3142 | On some platforms, path may also be specified as an open file descriptor. |
| 3143 | If this functionality is unavailable, using it raises an exception. |
| 3144 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3145 | and path should be relative; path will then be relative to that directory. |
| 3146 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3147 | link, chown will modify the symbolic link itself instead of the file the |
| 3148 | link points to. |
| 3149 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3150 | an open file descriptor. |
| 3151 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3152 | If they are unavailable, using them will raise a NotImplementedError. |
| 3153 | |
| 3154 | [clinic start generated code]*/ |
| 3155 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3156 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3157 | os_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] | 3158 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3159 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3160 | { |
| 3161 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3162 | |
| 3163 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3164 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3165 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3166 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3167 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3168 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3169 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3170 | |
| 3171 | #ifdef __APPLE__ |
| 3172 | /* |
| 3173 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3174 | * (But we still have an lchown symbol because of weak-linking.) |
| 3175 | * It doesn't have fchownat either. So there's no possibility |
| 3176 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3177 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3178 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3179 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3180 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3181 | } |
| 3182 | #endif |
| 3183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3184 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3185 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3186 | if (path->fd != -1) |
| 3187 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3188 | else |
| 3189 | #endif |
| 3190 | #ifdef HAVE_LCHOWN |
| 3191 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3192 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3193 | else |
| 3194 | #endif |
| 3195 | #ifdef HAVE_FCHOWNAT |
| 3196 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3197 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3198 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3199 | else |
| 3200 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3201 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3202 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3203 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3204 | if (result) |
| 3205 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3206 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3207 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3208 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3209 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3210 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3211 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3212 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3213 | /*[clinic input] |
| 3214 | os.fchown |
| 3215 | |
| 3216 | fd: int |
| 3217 | uid: uid_t |
| 3218 | gid: gid_t |
| 3219 | |
| 3220 | Change the owner and group id of the file specified by file descriptor. |
| 3221 | |
| 3222 | Equivalent to os.chown(fd, uid, gid). |
| 3223 | |
| 3224 | [clinic start generated code]*/ |
| 3225 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3226 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3227 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3228 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3229 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3230 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3231 | int async_err = 0; |
| 3232 | |
| 3233 | do { |
| 3234 | Py_BEGIN_ALLOW_THREADS |
| 3235 | res = fchown(fd, uid, gid); |
| 3236 | Py_END_ALLOW_THREADS |
| 3237 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3238 | if (res != 0) |
| 3239 | return (!async_err) ? posix_error() : NULL; |
| 3240 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3241 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3242 | } |
| 3243 | #endif /* HAVE_FCHOWN */ |
| 3244 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3245 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3246 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3247 | /*[clinic input] |
| 3248 | os.lchown |
| 3249 | |
| 3250 | path : path_t |
| 3251 | uid: uid_t |
| 3252 | gid: gid_t |
| 3253 | |
| 3254 | Change the owner and group id of path to the numeric uid and gid. |
| 3255 | |
| 3256 | This function will not follow symbolic links. |
| 3257 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3258 | [clinic start generated code]*/ |
| 3259 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3260 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3261 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3262 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3263 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3264 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3265 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3266 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3267 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3268 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3269 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3270 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3271 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3272 | } |
| 3273 | #endif /* HAVE_LCHOWN */ |
| 3274 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3275 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3276 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3277 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3278 | { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3279 | char *buf, *tmpbuf; |
| 3280 | char *cwd; |
| 3281 | const size_t chunk = 1024; |
| 3282 | size_t buflen = 0; |
| 3283 | PyObject *obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3284 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3285 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3286 | if (!use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3287 | wchar_t wbuf[MAXPATHLEN]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3288 | wchar_t *wbuf2 = wbuf; |
| 3289 | PyObject *resobj; |
| 3290 | DWORD len; |
| 3291 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3292 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3293 | /* If the buffer is large enough, len does not include the |
| 3294 | terminating \0. If the buffer is too small, len includes |
| 3295 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3296 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3297 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3298 | if (wbuf2) |
| 3299 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3300 | } |
| 3301 | Py_END_ALLOW_THREADS |
| 3302 | if (!wbuf2) { |
| 3303 | PyErr_NoMemory(); |
| 3304 | return NULL; |
| 3305 | } |
| 3306 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3307 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3308 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3309 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3310 | } |
| 3311 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3312 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3313 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3314 | return resobj; |
| 3315 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3316 | |
| 3317 | if (win32_warn_bytes_api()) |
| 3318 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3319 | #endif |
| 3320 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3321 | buf = cwd = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3322 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3323 | do { |
| 3324 | buflen += chunk; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3325 | #ifdef MS_WINDOWS |
| 3326 | if (buflen > INT_MAX) { |
| 3327 | PyErr_NoMemory(); |
| 3328 | break; |
| 3329 | } |
| 3330 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3331 | tmpbuf = PyMem_RawRealloc(buf, buflen); |
| 3332 | if (tmpbuf == NULL) |
| 3333 | break; |
| 3334 | |
| 3335 | buf = tmpbuf; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3336 | #ifdef MS_WINDOWS |
| 3337 | cwd = getcwd(buf, (int)buflen); |
| 3338 | #else |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3339 | cwd = getcwd(buf, buflen); |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3340 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3341 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3342 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3343 | |
| 3344 | if (cwd == NULL) { |
| 3345 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3346 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3347 | } |
| 3348 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3349 | if (use_bytes) |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3350 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 3351 | else |
| 3352 | obj = PyUnicode_DecodeFSDefault(buf); |
| 3353 | PyMem_RawFree(buf); |
| 3354 | |
| 3355 | return obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3356 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3357 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3358 | |
| 3359 | /*[clinic input] |
| 3360 | os.getcwd |
| 3361 | |
| 3362 | Return a unicode string representing the current working directory. |
| 3363 | [clinic start generated code]*/ |
| 3364 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3365 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3366 | os_getcwd_impl(PyObject *module) |
| 3367 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3368 | { |
| 3369 | return posix_getcwd(0); |
| 3370 | } |
| 3371 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3372 | |
| 3373 | /*[clinic input] |
| 3374 | os.getcwdb |
| 3375 | |
| 3376 | Return a bytes string representing the current working directory. |
| 3377 | [clinic start generated code]*/ |
| 3378 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3379 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3380 | os_getcwdb_impl(PyObject *module) |
| 3381 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3382 | { |
| 3383 | return posix_getcwd(1); |
| 3384 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3385 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3386 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3387 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3388 | #define HAVE_LINK 1 |
| 3389 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3390 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3391 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3392 | /*[clinic input] |
| 3393 | |
| 3394 | os.link |
| 3395 | |
| 3396 | src : path_t |
| 3397 | dst : path_t |
| 3398 | * |
| 3399 | src_dir_fd : dir_fd = None |
| 3400 | dst_dir_fd : dir_fd = None |
| 3401 | follow_symlinks: bool = True |
| 3402 | |
| 3403 | Create a hard link to a file. |
| 3404 | |
| 3405 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3406 | descriptor open to a directory, and the respective path string (src or dst) |
| 3407 | should be relative; the path will then be relative to that directory. |
| 3408 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3409 | link, link will create a link to the symbolic link itself instead of the |
| 3410 | file the link points to. |
| 3411 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3412 | platform. If they are unavailable, using them will raise a |
| 3413 | NotImplementedError. |
| 3414 | [clinic start generated code]*/ |
| 3415 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3416 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3417 | 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] | 3418 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3419 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3420 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3421 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3422 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3423 | #else |
| 3424 | int result; |
| 3425 | #endif |
| 3426 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3427 | #ifndef HAVE_LINKAT |
| 3428 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3429 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3430 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3431 | } |
| 3432 | #endif |
| 3433 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3434 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3435 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3436 | PyErr_SetString(PyExc_NotImplementedError, |
| 3437 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3438 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3439 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3440 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3441 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3442 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3443 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3444 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3445 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3446 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3447 | if (!result) |
| 3448 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3449 | #else |
| 3450 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3451 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3452 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3453 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3454 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3455 | result = linkat(src_dir_fd, src->narrow, |
| 3456 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3457 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3458 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3459 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3460 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3461 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3462 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3463 | if (result) |
| 3464 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3465 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3466 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3467 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3468 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3469 | #endif |
| 3470 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3471 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3472 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3473 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3474 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3475 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3476 | PyObject *v; |
| 3477 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3478 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3479 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3480 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3481 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3482 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3483 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3484 | WIN32_FIND_DATAW wFileData; |
| 3485 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3486 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3487 | if (!path->wide) { /* Default arg: "." */ |
| 3488 | po_wchars = L"."; |
| 3489 | len = 1; |
| 3490 | } else { |
| 3491 | po_wchars = path->wide; |
| 3492 | len = wcslen(path->wide); |
| 3493 | } |
| 3494 | /* The +5 is so we can append "\\*.*\0" */ |
| 3495 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3496 | if (!wnamebuf) { |
| 3497 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3498 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3499 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3500 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3501 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3502 | wchar_t wch = wnamebuf[len-1]; |
| 3503 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3504 | wnamebuf[len++] = SEP; |
| 3505 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3506 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3507 | if ((list = PyList_New(0)) == NULL) { |
| 3508 | goto exit; |
| 3509 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3510 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3511 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3512 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3513 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3514 | int error = GetLastError(); |
| 3515 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3516 | goto exit; |
| 3517 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3518 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3519 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3520 | } |
| 3521 | do { |
| 3522 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3523 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3524 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3525 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3526 | wcslen(wFileData.cFileName)); |
| 3527 | if (path->narrow && v) { |
| 3528 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3529 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3530 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3531 | Py_DECREF(list); |
| 3532 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3533 | break; |
| 3534 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3535 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3536 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3537 | Py_DECREF(list); |
| 3538 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3539 | break; |
| 3540 | } |
| 3541 | Py_DECREF(v); |
| 3542 | } |
| 3543 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3544 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3545 | Py_END_ALLOW_THREADS |
| 3546 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3547 | it got to the end of the directory. */ |
| 3548 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3549 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3550 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3551 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3552 | } |
| 3553 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3554 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3555 | exit: |
| 3556 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3557 | if (FindClose(hFindFile) == FALSE) { |
| 3558 | if (list != NULL) { |
| 3559 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3560 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3561 | } |
| 3562 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3563 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3564 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3565 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3566 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3567 | } /* end of _listdir_windows_no_opendir */ |
| 3568 | |
| 3569 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3570 | |
| 3571 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3572 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3573 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3574 | PyObject *v; |
| 3575 | DIR *dirp = NULL; |
| 3576 | struct dirent *ep; |
| 3577 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3578 | #ifdef HAVE_FDOPENDIR |
| 3579 | int fd = -1; |
| 3580 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3581 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3582 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3583 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3584 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3585 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3586 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3587 | if (fd == -1) |
| 3588 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3589 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3590 | return_str = 1; |
| 3591 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3592 | Py_BEGIN_ALLOW_THREADS |
| 3593 | dirp = fdopendir(fd); |
| 3594 | Py_END_ALLOW_THREADS |
| 3595 | } |
| 3596 | else |
| 3597 | #endif |
| 3598 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3599 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3600 | if (path->narrow) { |
| 3601 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3602 | /* only return bytes if they specified a bytes-like object */ |
| 3603 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3604 | } |
| 3605 | else { |
| 3606 | name = "."; |
| 3607 | return_str = 1; |
| 3608 | } |
| 3609 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3610 | Py_BEGIN_ALLOW_THREADS |
| 3611 | dirp = opendir(name); |
| 3612 | Py_END_ALLOW_THREADS |
| 3613 | } |
| 3614 | |
| 3615 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3616 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3617 | #ifdef HAVE_FDOPENDIR |
| 3618 | if (fd != -1) { |
| 3619 | Py_BEGIN_ALLOW_THREADS |
| 3620 | close(fd); |
| 3621 | Py_END_ALLOW_THREADS |
| 3622 | } |
| 3623 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3624 | goto exit; |
| 3625 | } |
| 3626 | if ((list = PyList_New(0)) == NULL) { |
| 3627 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3628 | } |
| 3629 | for (;;) { |
| 3630 | errno = 0; |
| 3631 | Py_BEGIN_ALLOW_THREADS |
| 3632 | ep = readdir(dirp); |
| 3633 | Py_END_ALLOW_THREADS |
| 3634 | if (ep == NULL) { |
| 3635 | if (errno == 0) { |
| 3636 | break; |
| 3637 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3638 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3639 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3640 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3641 | } |
| 3642 | } |
| 3643 | if (ep->d_name[0] == '.' && |
| 3644 | (NAMLEN(ep) == 1 || |
| 3645 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3646 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3647 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3648 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3649 | else |
| 3650 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3651 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3652 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3653 | break; |
| 3654 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3655 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3656 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3657 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3658 | break; |
| 3659 | } |
| 3660 | Py_DECREF(v); |
| 3661 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3662 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3663 | exit: |
| 3664 | if (dirp != NULL) { |
| 3665 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3666 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3667 | if (fd > -1) |
| 3668 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3669 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3670 | closedir(dirp); |
| 3671 | Py_END_ALLOW_THREADS |
| 3672 | } |
| 3673 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3674 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3675 | } /* end of _posix_listdir */ |
| 3676 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3677 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3678 | |
| 3679 | /*[clinic input] |
| 3680 | os.listdir |
| 3681 | |
| 3682 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3683 | |
| 3684 | Return a list containing the names of the files in the directory. |
| 3685 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3686 | 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] | 3687 | the filenames returned will also be bytes; in all other circumstances |
| 3688 | the filenames returned will be str. |
| 3689 | If path is None, uses the path='.'. |
| 3690 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3691 | the file descriptor must refer to a directory. |
| 3692 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3693 | |
| 3694 | The list is in arbitrary order. It does not include the special |
| 3695 | entries '.' and '..' even if they are present in the directory. |
| 3696 | |
| 3697 | |
| 3698 | [clinic start generated code]*/ |
| 3699 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3700 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3701 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3702 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3703 | { |
| 3704 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3705 | return _listdir_windows_no_opendir(path, NULL); |
| 3706 | #else |
| 3707 | return _posix_listdir(path, NULL); |
| 3708 | #endif |
| 3709 | } |
| 3710 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3711 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3712 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3713 | /*[clinic input] |
| 3714 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3715 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3716 | path: path_t |
| 3717 | / |
| 3718 | |
| 3719 | [clinic start generated code]*/ |
| 3720 | |
| 3721 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3722 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 3723 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3724 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3725 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
| 3726 | wchar_t *wtemp; |
| 3727 | DWORD result; |
| 3728 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3729 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3730 | result = GetFullPathNameW(path->wide, |
| 3731 | Py_ARRAY_LENGTH(woutbuf), |
| 3732 | woutbuf, &wtemp); |
| 3733 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
| 3734 | woutbufp = PyMem_New(wchar_t, result); |
| 3735 | if (!woutbufp) |
| 3736 | return PyErr_NoMemory(); |
| 3737 | result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3738 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3739 | if (result) { |
| 3740 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
| 3741 | if (path->narrow) |
| 3742 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3743 | } else |
| 3744 | v = win32_error_object("GetFullPathNameW", path->object); |
| 3745 | if (woutbufp != woutbuf) |
| 3746 | PyMem_Free(woutbufp); |
| 3747 | return v; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3748 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3749 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3750 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3751 | /*[clinic input] |
| 3752 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3753 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3754 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3755 | / |
| 3756 | |
| 3757 | A helper function for samepath on windows. |
| 3758 | [clinic start generated code]*/ |
| 3759 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3760 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3761 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 3762 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3763 | { |
| 3764 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3765 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 3766 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3767 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3768 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3769 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3770 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3771 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3772 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3773 | 0, /* desired access */ |
| 3774 | 0, /* share mode */ |
| 3775 | NULL, /* security attributes */ |
| 3776 | OPEN_EXISTING, |
| 3777 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3778 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3779 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3780 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3781 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3782 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3783 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3784 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3785 | |
| 3786 | /* We have a good handle to the target, use it to determine the |
| 3787 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3788 | while (1) { |
| 3789 | Py_BEGIN_ALLOW_THREADS |
| 3790 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 3791 | buf_size, VOLUME_NAME_DOS); |
| 3792 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3793 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3794 | if (!result_length) { |
| 3795 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 3796 | path->object); |
| 3797 | goto cleanup; |
| 3798 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3799 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3800 | if (result_length < buf_size) { |
| 3801 | break; |
| 3802 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3803 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3804 | wchar_t *tmp; |
| 3805 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 3806 | result_length * sizeof(*tmp)); |
| 3807 | if (!tmp) { |
| 3808 | result = PyErr_NoMemory(); |
| 3809 | goto cleanup; |
| 3810 | } |
| 3811 | |
| 3812 | buf_size = result_length; |
| 3813 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3814 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3815 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3816 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3817 | if (path->narrow) |
| 3818 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3819 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3820 | cleanup: |
| 3821 | if (target_path != buf) { |
| 3822 | PyMem_Free(target_path); |
| 3823 | } |
| 3824 | CloseHandle(hFile); |
| 3825 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3826 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3827 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3828 | /*[clinic input] |
| 3829 | os._isdir |
| 3830 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3831 | path as arg: object |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3832 | / |
| 3833 | |
Serhiy Storchaka | 579f038 | 2016-11-08 20:21:22 +0200 | [diff] [blame] | 3834 | Return true if the pathname refers to an existing directory. |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3835 | [clinic start generated code]*/ |
| 3836 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3837 | static PyObject * |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3838 | os__isdir(PyObject *module, PyObject *arg) |
| 3839 | /*[clinic end generated code: output=404f334d85d4bf25 input=36cb6785874d479e]*/ |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3840 | { |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3841 | DWORD attributes; |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3842 | path_t path = PATH_T_INITIALIZE("_isdir", "path", 0, 0); |
| 3843 | |
| 3844 | if (!path_converter(arg, &path)) { |
| 3845 | if (PyErr_ExceptionMatches(PyExc_ValueError)) { |
| 3846 | PyErr_Clear(); |
| 3847 | Py_RETURN_FALSE; |
| 3848 | } |
| 3849 | return NULL; |
| 3850 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3851 | |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3852 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3853 | attributes = GetFileAttributesW(path.wide); |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3854 | Py_END_ALLOW_THREADS |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3855 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3856 | path_cleanup(&path); |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3857 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3858 | Py_RETURN_FALSE; |
| 3859 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3860 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3861 | Py_RETURN_TRUE; |
| 3862 | else |
| 3863 | Py_RETURN_FALSE; |
| 3864 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3865 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3866 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3867 | /*[clinic input] |
| 3868 | os._getvolumepathname |
| 3869 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3870 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3871 | |
| 3872 | A helper function for ismount on Win32. |
| 3873 | [clinic start generated code]*/ |
| 3874 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3875 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3876 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 3877 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3878 | { |
| 3879 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3880 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3881 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3882 | BOOL ret; |
| 3883 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3884 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3885 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3886 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 3887 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3888 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 3889 | return NULL; |
| 3890 | } |
| 3891 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3892 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3893 | if (mountpath == NULL) |
| 3894 | return PyErr_NoMemory(); |
| 3895 | |
| 3896 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3897 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3898 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3899 | Py_END_ALLOW_THREADS |
| 3900 | |
| 3901 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3902 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3903 | goto exit; |
| 3904 | } |
| 3905 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3906 | if (path->narrow) |
| 3907 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3908 | |
| 3909 | exit: |
| 3910 | PyMem_Free(mountpath); |
| 3911 | return result; |
| 3912 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3913 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3914 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3915 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3916 | |
| 3917 | /*[clinic input] |
| 3918 | os.mkdir |
| 3919 | |
| 3920 | path : path_t |
| 3921 | |
| 3922 | mode: int = 0o777 |
| 3923 | |
| 3924 | * |
| 3925 | |
| 3926 | dir_fd : dir_fd(requires='mkdirat') = None |
| 3927 | |
| 3928 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3929 | |
| 3930 | Create a directory. |
| 3931 | |
| 3932 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3933 | and path should be relative; path will then be relative to that directory. |
| 3934 | dir_fd may not be implemented on your platform. |
| 3935 | If it is unavailable, using it will raise a NotImplementedError. |
| 3936 | |
| 3937 | The mode argument is ignored on Windows. |
| 3938 | [clinic start generated code]*/ |
| 3939 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3940 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3941 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 3942 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3943 | { |
| 3944 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3945 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3946 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3947 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3948 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3949 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3950 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3951 | if (!result) |
| 3952 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3953 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3954 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3955 | #if HAVE_MKDIRAT |
| 3956 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3957 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3958 | else |
| 3959 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 3960 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3961 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3962 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3963 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3964 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3965 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3966 | if (result < 0) |
| 3967 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3968 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3969 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3972 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3973 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3974 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3975 | #include <sys/resource.h> |
| 3976 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3977 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3978 | |
| 3979 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3980 | /*[clinic input] |
| 3981 | os.nice |
| 3982 | |
| 3983 | increment: int |
| 3984 | / |
| 3985 | |
| 3986 | Add increment to the priority of process and return the new priority. |
| 3987 | [clinic start generated code]*/ |
| 3988 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3989 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3990 | os_nice_impl(PyObject *module, int increment) |
| 3991 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3992 | { |
| 3993 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3994 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3995 | /* There are two flavours of 'nice': one that returns the new |
| 3996 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 3997 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3998 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3999 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4000 | If we are of the nice family that returns the new priority, we |
| 4001 | need to clear errno before the call, and check if errno is filled |
| 4002 | before calling posix_error() on a returnvalue of -1, because the |
| 4003 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4004 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4005 | errno = 0; |
| 4006 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4007 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4008 | if (value == 0) |
| 4009 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4010 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4011 | if (value == -1 && errno != 0) |
| 4012 | /* either nice() or getpriority() returned an error */ |
| 4013 | return posix_error(); |
| 4014 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4015 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4016 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4017 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4018 | |
| 4019 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4020 | /*[clinic input] |
| 4021 | os.getpriority |
| 4022 | |
| 4023 | which: int |
| 4024 | who: int |
| 4025 | |
| 4026 | Return program scheduling 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_getpriority_impl(PyObject *module, int which, int who) |
| 4031 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4032 | { |
| 4033 | int retval; |
| 4034 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4035 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4036 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4037 | if (errno != 0) |
| 4038 | return posix_error(); |
| 4039 | return PyLong_FromLong((long)retval); |
| 4040 | } |
| 4041 | #endif /* HAVE_GETPRIORITY */ |
| 4042 | |
| 4043 | |
| 4044 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4045 | /*[clinic input] |
| 4046 | os.setpriority |
| 4047 | |
| 4048 | which: int |
| 4049 | who: int |
| 4050 | priority: int |
| 4051 | |
| 4052 | Set program scheduling priority. |
| 4053 | [clinic start generated code]*/ |
| 4054 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4055 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4056 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4057 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4058 | { |
| 4059 | int retval; |
| 4060 | |
| 4061 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4062 | if (retval == -1) |
| 4063 | return posix_error(); |
| 4064 | Py_RETURN_NONE; |
| 4065 | } |
| 4066 | #endif /* HAVE_SETPRIORITY */ |
| 4067 | |
| 4068 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4069 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4070 | 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] | 4071 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4072 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4073 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4074 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4075 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4076 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4077 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4078 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4079 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4080 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4081 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4082 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4083 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4084 | #ifndef HAVE_RENAMEAT |
| 4085 | if (dir_fd_specified) { |
| 4086 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4087 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4088 | } |
| 4089 | #endif |
| 4090 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4091 | #ifdef MS_WINDOWS |
| 4092 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4093 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4094 | Py_END_ALLOW_THREADS |
| 4095 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4096 | if (!result) |
| 4097 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4098 | |
| 4099 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4100 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4101 | PyErr_Format(PyExc_ValueError, |
| 4102 | "%s: src and dst must be the same type", function_name); |
| 4103 | return NULL; |
| 4104 | } |
| 4105 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4106 | Py_BEGIN_ALLOW_THREADS |
| 4107 | #ifdef HAVE_RENAMEAT |
| 4108 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4109 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4110 | else |
| 4111 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4112 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4113 | Py_END_ALLOW_THREADS |
| 4114 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4115 | if (result) |
| 4116 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4117 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4118 | Py_RETURN_NONE; |
| 4119 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4120 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4121 | |
| 4122 | /*[clinic input] |
| 4123 | os.rename |
| 4124 | |
| 4125 | src : path_t |
| 4126 | dst : path_t |
| 4127 | * |
| 4128 | src_dir_fd : dir_fd = None |
| 4129 | dst_dir_fd : dir_fd = None |
| 4130 | |
| 4131 | Rename a file or directory. |
| 4132 | |
| 4133 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4134 | descriptor open to a directory, and the respective path string (src or dst) |
| 4135 | should be relative; the path will then be relative to that directory. |
| 4136 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4137 | If they are unavailable, using them will raise a NotImplementedError. |
| 4138 | [clinic start generated code]*/ |
| 4139 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4140 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4141 | 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] | 4142 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4143 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4144 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4145 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4146 | } |
| 4147 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4148 | |
| 4149 | /*[clinic input] |
| 4150 | os.replace = os.rename |
| 4151 | |
| 4152 | Rename a file or directory, overwriting the destination. |
| 4153 | |
| 4154 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4155 | descriptor open to a directory, and the respective path string (src or dst) |
| 4156 | should be relative; the path will then be relative to that directory. |
| 4157 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4158 | If they are unavailable, using them will raise a NotImplementedError." |
| 4159 | [clinic start generated code]*/ |
| 4160 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4161 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4162 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4163 | int dst_dir_fd) |
| 4164 | /*[clinic end generated code: output=1968c02e7857422b input=25515dfb107c8421]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4165 | { |
| 4166 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4167 | } |
| 4168 | |
| 4169 | |
| 4170 | /*[clinic input] |
| 4171 | os.rmdir |
| 4172 | |
| 4173 | path: path_t |
| 4174 | * |
| 4175 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4176 | |
| 4177 | Remove a directory. |
| 4178 | |
| 4179 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4180 | and path should be relative; path will then be relative to that directory. |
| 4181 | dir_fd may not be implemented on your platform. |
| 4182 | If it is unavailable, using it will raise a NotImplementedError. |
| 4183 | [clinic start generated code]*/ |
| 4184 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4185 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4186 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4187 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4188 | { |
| 4189 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4190 | |
| 4191 | Py_BEGIN_ALLOW_THREADS |
| 4192 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4193 | /* Windows, success=1, UNIX, success=0 */ |
| 4194 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4195 | #else |
| 4196 | #ifdef HAVE_UNLINKAT |
| 4197 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4198 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4199 | else |
| 4200 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4201 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4202 | #endif |
| 4203 | Py_END_ALLOW_THREADS |
| 4204 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4205 | if (result) |
| 4206 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4207 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4208 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4211 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4212 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4213 | #ifdef MS_WINDOWS |
| 4214 | /*[clinic input] |
| 4215 | os.system -> long |
| 4216 | |
| 4217 | command: Py_UNICODE |
| 4218 | |
| 4219 | Execute the command in a subshell. |
| 4220 | [clinic start generated code]*/ |
| 4221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4222 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4223 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4224 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4225 | { |
| 4226 | long result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4227 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4228 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4229 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4230 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4231 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4232 | return result; |
| 4233 | } |
| 4234 | #else /* MS_WINDOWS */ |
| 4235 | /*[clinic input] |
| 4236 | os.system -> long |
| 4237 | |
| 4238 | command: FSConverter |
| 4239 | |
| 4240 | Execute the command in a subshell. |
| 4241 | [clinic start generated code]*/ |
| 4242 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4243 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4244 | os_system_impl(PyObject *module, PyObject *command) |
| 4245 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4246 | { |
| 4247 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4248 | const char *bytes = PyBytes_AsString(command); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4249 | Py_BEGIN_ALLOW_THREADS |
| 4250 | result = system(bytes); |
| 4251 | Py_END_ALLOW_THREADS |
| 4252 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4253 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4254 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4255 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4256 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4257 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4258 | /*[clinic input] |
| 4259 | os.umask |
| 4260 | |
| 4261 | mask: int |
| 4262 | / |
| 4263 | |
| 4264 | Set the current numeric umask and return the previous umask. |
| 4265 | [clinic start generated code]*/ |
| 4266 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4267 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4268 | os_umask_impl(PyObject *module, int mask) |
| 4269 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4270 | { |
| 4271 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4272 | if (i < 0) |
| 4273 | return posix_error(); |
| 4274 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4277 | #ifdef MS_WINDOWS |
| 4278 | |
| 4279 | /* override the default DeleteFileW behavior so that directory |
| 4280 | symlinks can be removed with this function, the same as with |
| 4281 | Unix symlinks */ |
| 4282 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4283 | { |
| 4284 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4285 | WIN32_FIND_DATAW find_data; |
| 4286 | HANDLE find_data_handle; |
| 4287 | int is_directory = 0; |
| 4288 | int is_link = 0; |
| 4289 | |
| 4290 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4291 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4292 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4293 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4294 | it is a symlink */ |
| 4295 | if(is_directory && |
| 4296 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4297 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4298 | |
| 4299 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4300 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4301 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4302 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4303 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4304 | FindClose(find_data_handle); |
| 4305 | } |
| 4306 | } |
| 4307 | } |
| 4308 | |
| 4309 | if (is_directory && is_link) |
| 4310 | return RemoveDirectoryW(lpFileName); |
| 4311 | |
| 4312 | return DeleteFileW(lpFileName); |
| 4313 | } |
| 4314 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4315 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4316 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4317 | /*[clinic input] |
| 4318 | os.unlink |
| 4319 | |
| 4320 | path: path_t |
| 4321 | * |
| 4322 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4323 | |
| 4324 | Remove a file (same as remove()). |
| 4325 | |
| 4326 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4327 | and path should be relative; path will then be relative to that directory. |
| 4328 | dir_fd may not be implemented on your platform. |
| 4329 | If it is unavailable, using it will raise a NotImplementedError. |
| 4330 | |
| 4331 | [clinic start generated code]*/ |
| 4332 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4333 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4334 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4335 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4336 | { |
| 4337 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4338 | |
| 4339 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4340 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4341 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4342 | /* Windows, success=1, UNIX, success=0 */ |
| 4343 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4344 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4345 | #ifdef HAVE_UNLINKAT |
| 4346 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4347 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4348 | else |
| 4349 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4350 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4351 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4352 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4353 | Py_END_ALLOW_THREADS |
| 4354 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4355 | if (result) |
| 4356 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4357 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4358 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4361 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4362 | /*[clinic input] |
| 4363 | os.remove = os.unlink |
| 4364 | |
| 4365 | Remove a file (same as unlink()). |
| 4366 | |
| 4367 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4368 | and path should be relative; path will then be relative to that directory. |
| 4369 | dir_fd may not be implemented on your platform. |
| 4370 | If it is unavailable, using it will raise a NotImplementedError. |
| 4371 | [clinic start generated code]*/ |
| 4372 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4373 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4374 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4375 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4376 | { |
| 4377 | return os_unlink_impl(module, path, dir_fd); |
| 4378 | } |
| 4379 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4380 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4381 | static PyStructSequence_Field uname_result_fields[] = { |
| 4382 | {"sysname", "operating system name"}, |
| 4383 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4384 | {"release", "operating system release"}, |
| 4385 | {"version", "operating system version"}, |
| 4386 | {"machine", "hardware identifier"}, |
| 4387 | {NULL} |
| 4388 | }; |
| 4389 | |
| 4390 | PyDoc_STRVAR(uname_result__doc__, |
| 4391 | "uname_result: Result from os.uname().\n\n\ |
| 4392 | This object may be accessed either as a tuple of\n\ |
| 4393 | (sysname, nodename, release, version, machine),\n\ |
| 4394 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4395 | \n\ |
| 4396 | See os.uname for more information."); |
| 4397 | |
| 4398 | static PyStructSequence_Desc uname_result_desc = { |
| 4399 | "uname_result", /* name */ |
| 4400 | uname_result__doc__, /* doc */ |
| 4401 | uname_result_fields, |
| 4402 | 5 |
| 4403 | }; |
| 4404 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 4405 | static PyTypeObject* UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4406 | |
| 4407 | |
| 4408 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4409 | /*[clinic input] |
| 4410 | os.uname |
| 4411 | |
| 4412 | Return an object identifying the current operating system. |
| 4413 | |
| 4414 | The object behaves like a named tuple with the following fields: |
| 4415 | (sysname, nodename, release, version, machine) |
| 4416 | |
| 4417 | [clinic start generated code]*/ |
| 4418 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4419 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4420 | os_uname_impl(PyObject *module) |
| 4421 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4422 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4423 | struct utsname u; |
| 4424 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4425 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4426 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4427 | Py_BEGIN_ALLOW_THREADS |
| 4428 | res = uname(&u); |
| 4429 | Py_END_ALLOW_THREADS |
| 4430 | if (res < 0) |
| 4431 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4432 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 4433 | value = PyStructSequence_New(UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4434 | if (value == NULL) |
| 4435 | return NULL; |
| 4436 | |
| 4437 | #define SET(i, field) \ |
| 4438 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4439 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4440 | if (!o) { \ |
| 4441 | Py_DECREF(value); \ |
| 4442 | return NULL; \ |
| 4443 | } \ |
| 4444 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4445 | } \ |
| 4446 | |
| 4447 | SET(0, u.sysname); |
| 4448 | SET(1, u.nodename); |
| 4449 | SET(2, u.release); |
| 4450 | SET(3, u.version); |
| 4451 | SET(4, u.machine); |
| 4452 | |
| 4453 | #undef SET |
| 4454 | |
| 4455 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4456 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4457 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4458 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4459 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4460 | |
| 4461 | typedef struct { |
| 4462 | int now; |
| 4463 | time_t atime_s; |
| 4464 | long atime_ns; |
| 4465 | time_t mtime_s; |
| 4466 | long mtime_ns; |
| 4467 | } utime_t; |
| 4468 | |
| 4469 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4470 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4471 | * they also intentionally leak the declaration of a pointer named "time" |
| 4472 | */ |
| 4473 | #define UTIME_TO_TIMESPEC \ |
| 4474 | struct timespec ts[2]; \ |
| 4475 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4476 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4477 | time = NULL; \ |
| 4478 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4479 | ts[0].tv_sec = ut->atime_s; \ |
| 4480 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4481 | ts[1].tv_sec = ut->mtime_s; \ |
| 4482 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4483 | time = ts; \ |
| 4484 | } \ |
| 4485 | |
| 4486 | #define UTIME_TO_TIMEVAL \ |
| 4487 | struct timeval tv[2]; \ |
| 4488 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4489 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4490 | time = NULL; \ |
| 4491 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4492 | tv[0].tv_sec = ut->atime_s; \ |
| 4493 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4494 | tv[1].tv_sec = ut->mtime_s; \ |
| 4495 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4496 | time = tv; \ |
| 4497 | } \ |
| 4498 | |
| 4499 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4500 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4501 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4502 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4503 | time = NULL; \ |
| 4504 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4505 | u.actime = ut->atime_s; \ |
| 4506 | u.modtime = ut->mtime_s; \ |
| 4507 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | #define UTIME_TO_TIME_T \ |
| 4511 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4512 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4513 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4514 | time = NULL; \ |
| 4515 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4516 | timet[0] = ut->atime_s; \ |
| 4517 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4518 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4519 | } \ |
| 4520 | |
| 4521 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4522 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4523 | |
| 4524 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4525 | 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] | 4526 | { |
| 4527 | #ifdef HAVE_UTIMENSAT |
| 4528 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4529 | UTIME_TO_TIMESPEC; |
| 4530 | return utimensat(dir_fd, path, time, flags); |
| 4531 | #elif defined(HAVE_FUTIMESAT) |
| 4532 | UTIME_TO_TIMEVAL; |
| 4533 | /* |
| 4534 | * follow_symlinks will never be false here; |
| 4535 | * we only allow !follow_symlinks and dir_fd together |
| 4536 | * if we have utimensat() |
| 4537 | */ |
| 4538 | assert(follow_symlinks); |
| 4539 | return futimesat(dir_fd, path, time); |
| 4540 | #endif |
| 4541 | } |
| 4542 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4543 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4544 | #else |
| 4545 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4546 | #endif |
| 4547 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4548 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4549 | |
| 4550 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4551 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4552 | { |
| 4553 | #ifdef HAVE_FUTIMENS |
| 4554 | UTIME_TO_TIMESPEC; |
| 4555 | return futimens(fd, time); |
| 4556 | #else |
| 4557 | UTIME_TO_TIMEVAL; |
| 4558 | return futimes(fd, time); |
| 4559 | #endif |
| 4560 | } |
| 4561 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4562 | #define PATH_UTIME_HAVE_FD 1 |
| 4563 | #else |
| 4564 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4565 | #endif |
| 4566 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4567 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4568 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4569 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4570 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4571 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4572 | |
| 4573 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4574 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4575 | { |
| 4576 | #ifdef HAVE_UTIMENSAT |
| 4577 | UTIME_TO_TIMESPEC; |
| 4578 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4579 | #else |
| 4580 | UTIME_TO_TIMEVAL; |
| 4581 | return lutimes(path, time); |
| 4582 | #endif |
| 4583 | } |
| 4584 | |
| 4585 | #endif |
| 4586 | |
| 4587 | #ifndef MS_WINDOWS |
| 4588 | |
| 4589 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4590 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4591 | { |
| 4592 | #ifdef HAVE_UTIMENSAT |
| 4593 | UTIME_TO_TIMESPEC; |
| 4594 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4595 | #elif defined(HAVE_UTIMES) |
| 4596 | UTIME_TO_TIMEVAL; |
| 4597 | return utimes(path, time); |
| 4598 | #elif defined(HAVE_UTIME_H) |
| 4599 | UTIME_TO_UTIMBUF; |
| 4600 | return utime(path, time); |
| 4601 | #else |
| 4602 | UTIME_TO_TIME_T; |
| 4603 | return utime(path, time); |
| 4604 | #endif |
| 4605 | } |
| 4606 | |
| 4607 | #endif |
| 4608 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4609 | static int |
| 4610 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4611 | { |
| 4612 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4613 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4614 | divmod = PyNumber_Divmod(py_long, billion); |
| 4615 | if (!divmod) |
| 4616 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4617 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 4618 | PyErr_Format(PyExc_TypeError, |
| 4619 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
| 4620 | Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name); |
| 4621 | goto exit; |
| 4622 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4623 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4624 | if ((*s == -1) && PyErr_Occurred()) |
| 4625 | goto exit; |
| 4626 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4627 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4628 | goto exit; |
| 4629 | |
| 4630 | result = 1; |
| 4631 | exit: |
| 4632 | Py_XDECREF(divmod); |
| 4633 | return result; |
| 4634 | } |
| 4635 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4636 | |
| 4637 | /*[clinic input] |
| 4638 | os.utime |
| 4639 | |
| 4640 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
| 4641 | times: object = NULL |
| 4642 | * |
| 4643 | ns: object = NULL |
| 4644 | dir_fd: dir_fd(requires='futimensat') = None |
| 4645 | follow_symlinks: bool=True |
| 4646 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4647 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4648 | |
| 4649 | Set the access and modified time of path. |
| 4650 | |
| 4651 | path may always be specified as a string. |
| 4652 | On some platforms, path may also be specified as an open file descriptor. |
| 4653 | If this functionality is unavailable, using it raises an exception. |
| 4654 | |
| 4655 | If times is not None, it must be a tuple (atime, mtime); |
| 4656 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4657 | 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] | 4658 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4659 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4660 | 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] | 4661 | Specifying tuples for both times and ns is an error. |
| 4662 | |
| 4663 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4664 | and path should be relative; path will then be relative to that directory. |
| 4665 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4666 | link, utime will modify the symbolic link itself instead of the file the |
| 4667 | link points to. |
| 4668 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4669 | as an open file descriptor. |
| 4670 | dir_fd and follow_symlinks may not be available on your platform. |
| 4671 | If they are unavailable, using them will raise a NotImplementedError. |
| 4672 | |
| 4673 | [clinic start generated code]*/ |
| 4674 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4675 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4676 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4677 | int dir_fd, int follow_symlinks) |
| 4678 | /*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4679 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4680 | #ifdef MS_WINDOWS |
| 4681 | HANDLE hFile; |
| 4682 | FILETIME atime, mtime; |
| 4683 | #else |
| 4684 | int result; |
| 4685 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4686 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4687 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4688 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4689 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4690 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4691 | if (times && (times != Py_None) && ns) { |
| 4692 | PyErr_SetString(PyExc_ValueError, |
| 4693 | "utime: you may specify either 'times'" |
| 4694 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4695 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4696 | } |
| 4697 | |
| 4698 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4699 | time_t a_sec, m_sec; |
| 4700 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4701 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4702 | PyErr_SetString(PyExc_TypeError, |
| 4703 | "utime: 'times' must be either" |
| 4704 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4705 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4706 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4707 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4708 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4709 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4710 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4711 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4712 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4713 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4714 | utime.atime_s = a_sec; |
| 4715 | utime.atime_ns = a_nsec; |
| 4716 | utime.mtime_s = m_sec; |
| 4717 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4718 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4719 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4720 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4721 | PyErr_SetString(PyExc_TypeError, |
| 4722 | "utime: 'ns' must be a tuple of two ints"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4723 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4724 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4725 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4726 | 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] | 4727 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4728 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4729 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4730 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4731 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4732 | } |
| 4733 | else { |
| 4734 | /* times and ns are both None/unspecified. use "now". */ |
| 4735 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4736 | } |
| 4737 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4738 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4739 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4740 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4741 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4742 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4743 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4744 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4745 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4746 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4747 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4748 | #if !defined(HAVE_UTIMENSAT) |
| 4749 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4750 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4751 | "utime: cannot use dir_fd and follow_symlinks " |
| 4752 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4753 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4754 | } |
| 4755 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4756 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4757 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4758 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4759 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 4760 | NULL, OPEN_EXISTING, |
| 4761 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4762 | Py_END_ALLOW_THREADS |
| 4763 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4764 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4765 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4766 | } |
| 4767 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4768 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4769 | GetSystemTimeAsFileTime(&mtime); |
| 4770 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4771 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4772 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4773 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4774 | _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] | 4775 | } |
| 4776 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4777 | /* Avoid putting the file name into the error here, |
| 4778 | as that may confuse the user into believing that |
| 4779 | something is wrong with the file, when it also |
| 4780 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4781 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4782 | CloseHandle(hFile); |
| 4783 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4784 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4785 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4786 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4787 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4788 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4789 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4790 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4791 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4792 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4793 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4794 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4795 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4796 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4797 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4798 | else |
| 4799 | #endif |
| 4800 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4801 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4802 | if (path->fd != -1) |
| 4803 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4804 | else |
| 4805 | #endif |
| 4806 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4807 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4808 | |
| 4809 | Py_END_ALLOW_THREADS |
| 4810 | |
| 4811 | if (result < 0) { |
| 4812 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4813 | posix_error(); |
| 4814 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4815 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4816 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4817 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4818 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4819 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4822 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4823 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4824 | |
| 4825 | /*[clinic input] |
| 4826 | os._exit |
| 4827 | |
| 4828 | status: int |
| 4829 | |
| 4830 | Exit to the system with specified status, without normal exit processing. |
| 4831 | [clinic start generated code]*/ |
| 4832 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4833 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4834 | os__exit_impl(PyObject *module, int status) |
| 4835 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4836 | { |
| 4837 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4838 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4839 | } |
| 4840 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4841 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4842 | #define EXECV_CHAR wchar_t |
| 4843 | #else |
| 4844 | #define EXECV_CHAR char |
| 4845 | #endif |
| 4846 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4847 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4848 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4849 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4850 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4851 | Py_ssize_t i; |
| 4852 | for (i = 0; i < count; i++) |
| 4853 | PyMem_Free(array[i]); |
| 4854 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4855 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4856 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4857 | static int |
| 4858 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4859 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4860 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4861 | PyObject *ub; |
| 4862 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4863 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4864 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4865 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4866 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 4867 | if (*out) |
| 4868 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4869 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4870 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4871 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4872 | size = PyBytes_GET_SIZE(ub); |
| 4873 | *out = PyMem_Malloc(size + 1); |
| 4874 | if (*out) { |
| 4875 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 4876 | result = 1; |
| 4877 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4878 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4879 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4880 | Py_DECREF(ub); |
| 4881 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4882 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4883 | #endif |
| 4884 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4885 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4886 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4887 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4888 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4889 | Py_ssize_t i, pos, envc; |
| 4890 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4891 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4892 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4893 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4894 | i = PyMapping_Size(env); |
| 4895 | if (i < 0) |
| 4896 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4897 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4898 | if (envlist == NULL) { |
| 4899 | PyErr_NoMemory(); |
| 4900 | return NULL; |
| 4901 | } |
| 4902 | envc = 0; |
| 4903 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4904 | if (!keys) |
| 4905 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4906 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4907 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4908 | goto error; |
| 4909 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4910 | PyErr_Format(PyExc_TypeError, |
| 4911 | "env.keys() or env.values() is not a list"); |
| 4912 | goto error; |
| 4913 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4914 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4915 | for (pos = 0; pos < i; pos++) { |
| 4916 | key = PyList_GetItem(keys, pos); |
| 4917 | val = PyList_GetItem(vals, pos); |
| 4918 | if (!key || !val) |
| 4919 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4920 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4921 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4922 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 4923 | goto error; |
| 4924 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 4925 | Py_DECREF(key2); |
| 4926 | goto error; |
| 4927 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4928 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 4929 | defining hidden environment variables. */ |
| 4930 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 4931 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 4932 | { |
| 4933 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 4934 | Py_DECREF(key2); |
| 4935 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4936 | goto error; |
| 4937 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4938 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 4939 | #else |
| 4940 | if (!PyUnicode_FSConverter(key, &key2)) |
| 4941 | goto error; |
| 4942 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 4943 | Py_DECREF(key2); |
| 4944 | goto error; |
| 4945 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4946 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 4947 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 4948 | { |
| 4949 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 4950 | Py_DECREF(key2); |
| 4951 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4952 | goto error; |
| 4953 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4954 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 4955 | PyBytes_AS_STRING(val2)); |
| 4956 | #endif |
| 4957 | Py_DECREF(key2); |
| 4958 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4959 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4960 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4961 | |
| 4962 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 4963 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4964 | goto error; |
| 4965 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4966 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4967 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4968 | } |
| 4969 | Py_DECREF(vals); |
| 4970 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4971 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4972 | envlist[envc] = 0; |
| 4973 | *envc_ptr = envc; |
| 4974 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4975 | |
| 4976 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4977 | Py_XDECREF(keys); |
| 4978 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4979 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4980 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4981 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4982 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4983 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4984 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4985 | { |
| 4986 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4987 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4988 | if (argvlist == NULL) { |
| 4989 | PyErr_NoMemory(); |
| 4990 | return NULL; |
| 4991 | } |
| 4992 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4993 | PyObject* item = PySequence_ITEM(argv, i); |
| 4994 | if (item == NULL) |
| 4995 | goto fail; |
| 4996 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4997 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4998 | goto fail; |
| 4999 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5000 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5001 | } |
| 5002 | argvlist[*argc] = NULL; |
| 5003 | return argvlist; |
| 5004 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5005 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5006 | free_string_array(argvlist, *argc); |
| 5007 | return NULL; |
| 5008 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5009 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5010 | #endif |
| 5011 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5012 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5013 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5014 | /*[clinic input] |
| 5015 | os.execv |
| 5016 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5017 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5018 | Path of executable file. |
| 5019 | argv: object |
| 5020 | Tuple or list of strings. |
| 5021 | / |
| 5022 | |
| 5023 | Execute an executable path with arguments, replacing current process. |
| 5024 | [clinic start generated code]*/ |
| 5025 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5026 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5027 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5028 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5029 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5030 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5031 | Py_ssize_t argc; |
| 5032 | |
| 5033 | /* execv has two arguments: (path, argv), where |
| 5034 | argv is a list or tuple of strings. */ |
| 5035 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5036 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5037 | PyErr_SetString(PyExc_TypeError, |
| 5038 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5039 | return NULL; |
| 5040 | } |
| 5041 | argc = PySequence_Size(argv); |
| 5042 | if (argc < 1) { |
| 5043 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5044 | return NULL; |
| 5045 | } |
| 5046 | |
| 5047 | argvlist = parse_arglist(argv, &argc); |
| 5048 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5049 | return NULL; |
| 5050 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5051 | if (!argvlist[0][0]) { |
| 5052 | PyErr_SetString(PyExc_ValueError, |
| 5053 | "execv() arg 2 first element cannot be empty"); |
| 5054 | free_string_array(argvlist, argc); |
| 5055 | return NULL; |
| 5056 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5057 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5058 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5059 | #ifdef HAVE_WEXECV |
| 5060 | _wexecv(path->wide, argvlist); |
| 5061 | #else |
| 5062 | execv(path->narrow, argvlist); |
| 5063 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5064 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5065 | |
| 5066 | /* If we get here it's definitely an error */ |
| 5067 | |
| 5068 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5069 | return posix_error(); |
| 5070 | } |
| 5071 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5072 | |
| 5073 | /*[clinic input] |
| 5074 | os.execve |
| 5075 | |
| 5076 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5077 | Path of executable file. |
| 5078 | argv: object |
| 5079 | Tuple or list of strings. |
| 5080 | env: object |
| 5081 | Dictionary of strings mapping to strings. |
| 5082 | |
| 5083 | Execute an executable path with arguments, replacing current process. |
| 5084 | [clinic start generated code]*/ |
| 5085 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5086 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5087 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5088 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5089 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5090 | EXECV_CHAR **argvlist = NULL; |
| 5091 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5092 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5093 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5094 | /* execve has three arguments: (path, argv, env), where |
| 5095 | argv is a list or tuple of strings and env is a dictionary |
| 5096 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5097 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5098 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5099 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5100 | "execve: argv must be a tuple or list"); |
| 5101 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5102 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5103 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5104 | if (argc < 1) { |
| 5105 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5106 | return NULL; |
| 5107 | } |
| 5108 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5109 | if (!PyMapping_Check(env)) { |
| 5110 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5111 | "execve: environment must be a mapping object"); |
| 5112 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5113 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5114 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5115 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5116 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5117 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5118 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5119 | if (!argvlist[0][0]) { |
| 5120 | PyErr_SetString(PyExc_ValueError, |
| 5121 | "execve: argv first element cannot be empty"); |
| 5122 | goto fail; |
| 5123 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5124 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5125 | envlist = parse_envlist(env, &envc); |
| 5126 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5127 | goto fail; |
| 5128 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5129 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5130 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5131 | if (path->fd > -1) |
| 5132 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5133 | else |
| 5134 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5135 | #ifdef HAVE_WEXECV |
| 5136 | _wexecve(path->wide, argvlist, envlist); |
| 5137 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5138 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5139 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5140 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5141 | |
| 5142 | /* If we get here it's definitely an error */ |
| 5143 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5144 | posix_path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5145 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5146 | free_string_array(envlist, envc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5147 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5148 | if (argvlist) |
| 5149 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5150 | return NULL; |
| 5151 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5152 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5153 | #endif /* HAVE_EXECV */ |
| 5154 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5155 | #ifdef HAVE_POSIX_SPAWN |
| 5156 | |
| 5157 | enum posix_spawn_file_actions_identifier { |
| 5158 | POSIX_SPAWN_OPEN, |
| 5159 | POSIX_SPAWN_CLOSE, |
| 5160 | POSIX_SPAWN_DUP2 |
| 5161 | }; |
| 5162 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5163 | #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] | 5164 | static int |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5165 | convert_sched_param(PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5166 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5167 | |
| 5168 | static int |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame^] | 5169 | parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup, |
| 5170 | int resetids, int setsid, PyObject *setsigmask, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5171 | PyObject *setsigdef, PyObject *scheduler, |
| 5172 | posix_spawnattr_t *attrp) |
| 5173 | { |
| 5174 | long all_flags = 0; |
| 5175 | |
| 5176 | errno = posix_spawnattr_init(attrp); |
| 5177 | if (errno) { |
| 5178 | posix_error(); |
| 5179 | return -1; |
| 5180 | } |
| 5181 | |
| 5182 | if (setpgroup) { |
| 5183 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5184 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5185 | goto fail; |
| 5186 | } |
| 5187 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5188 | if (errno) { |
| 5189 | posix_error(); |
| 5190 | goto fail; |
| 5191 | } |
| 5192 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5193 | } |
| 5194 | |
| 5195 | if (resetids) { |
| 5196 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5197 | } |
| 5198 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5199 | if (setsid) { |
| 5200 | #ifdef POSIX_SPAWN_SETSID |
| 5201 | all_flags |= POSIX_SPAWN_SETSID; |
| 5202 | #elif defined(POSIX_SPAWN_SETSID_NP) |
| 5203 | all_flags |= POSIX_SPAWN_SETSID_NP; |
| 5204 | #else |
| 5205 | argument_unavailable_error(func_name, "setsid"); |
| 5206 | return -1; |
| 5207 | #endif |
| 5208 | } |
| 5209 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5210 | if (setsigmask) { |
| 5211 | sigset_t set; |
| 5212 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5213 | goto fail; |
| 5214 | } |
| 5215 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5216 | if (errno) { |
| 5217 | posix_error(); |
| 5218 | goto fail; |
| 5219 | } |
| 5220 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5221 | } |
| 5222 | |
| 5223 | if (setsigdef) { |
| 5224 | sigset_t set; |
| 5225 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5226 | goto fail; |
| 5227 | } |
| 5228 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5229 | if (errno) { |
| 5230 | posix_error(); |
| 5231 | goto fail; |
| 5232 | } |
| 5233 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5234 | } |
| 5235 | |
| 5236 | if (scheduler) { |
| 5237 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5238 | PyObject *py_schedpolicy; |
| 5239 | struct sched_param schedparam; |
| 5240 | |
| 5241 | if (!PyArg_ParseTuple(scheduler, "OO&" |
| 5242 | ";A scheduler tuple must have two elements", |
| 5243 | &py_schedpolicy, convert_sched_param, &schedparam)) { |
| 5244 | goto fail; |
| 5245 | } |
| 5246 | if (py_schedpolicy != Py_None) { |
| 5247 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5248 | |
| 5249 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5250 | goto fail; |
| 5251 | } |
| 5252 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5253 | if (errno) { |
| 5254 | posix_error(); |
| 5255 | goto fail; |
| 5256 | } |
| 5257 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5258 | } |
| 5259 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5260 | if (errno) { |
| 5261 | posix_error(); |
| 5262 | goto fail; |
| 5263 | } |
| 5264 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5265 | #else |
| 5266 | PyErr_SetString(PyExc_NotImplementedError, |
| 5267 | "The scheduler option is not supported in this system."); |
| 5268 | goto fail; |
| 5269 | #endif |
| 5270 | } |
| 5271 | |
| 5272 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5273 | if (errno) { |
| 5274 | posix_error(); |
| 5275 | goto fail; |
| 5276 | } |
| 5277 | |
| 5278 | return 0; |
| 5279 | |
| 5280 | fail: |
| 5281 | (void)posix_spawnattr_destroy(attrp); |
| 5282 | return -1; |
| 5283 | } |
| 5284 | |
| 5285 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5286 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5287 | posix_spawn_file_actions_t *file_actionsp, |
| 5288 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5289 | { |
| 5290 | PyObject *seq; |
| 5291 | PyObject *file_action = NULL; |
| 5292 | PyObject *tag_obj; |
| 5293 | |
| 5294 | seq = PySequence_Fast(file_actions, |
| 5295 | "file_actions must be a sequence or None"); |
| 5296 | if (seq == NULL) { |
| 5297 | return -1; |
| 5298 | } |
| 5299 | |
| 5300 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5301 | if (errno) { |
| 5302 | posix_error(); |
| 5303 | Py_DECREF(seq); |
| 5304 | return -1; |
| 5305 | } |
| 5306 | |
| 5307 | for (int i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) { |
| 5308 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5309 | Py_INCREF(file_action); |
| 5310 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5311 | PyErr_SetString(PyExc_TypeError, |
| 5312 | "Each file_actions element must be a non-empty tuple"); |
| 5313 | goto fail; |
| 5314 | } |
| 5315 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5316 | if (tag == -1 && PyErr_Occurred()) { |
| 5317 | goto fail; |
| 5318 | } |
| 5319 | |
| 5320 | /* Populate the file_actions object */ |
| 5321 | switch (tag) { |
| 5322 | case POSIX_SPAWN_OPEN: { |
| 5323 | int fd, oflag; |
| 5324 | PyObject *path; |
| 5325 | unsigned long mode; |
| 5326 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5327 | ";A open file_action tuple must have 5 elements", |
| 5328 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5329 | &oflag, &mode)) |
| 5330 | { |
| 5331 | goto fail; |
| 5332 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5333 | if (PyList_Append(temp_buffer, path)) { |
| 5334 | Py_DECREF(path); |
| 5335 | goto fail; |
| 5336 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5337 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5338 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5339 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5340 | if (errno) { |
| 5341 | posix_error(); |
| 5342 | goto fail; |
| 5343 | } |
| 5344 | break; |
| 5345 | } |
| 5346 | case POSIX_SPAWN_CLOSE: { |
| 5347 | int fd; |
| 5348 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5349 | ";A close file_action tuple must have 2 elements", |
| 5350 | &tag_obj, &fd)) |
| 5351 | { |
| 5352 | goto fail; |
| 5353 | } |
| 5354 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 5355 | if (errno) { |
| 5356 | posix_error(); |
| 5357 | goto fail; |
| 5358 | } |
| 5359 | break; |
| 5360 | } |
| 5361 | case POSIX_SPAWN_DUP2: { |
| 5362 | int fd1, fd2; |
| 5363 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 5364 | ";A dup2 file_action tuple must have 3 elements", |
| 5365 | &tag_obj, &fd1, &fd2)) |
| 5366 | { |
| 5367 | goto fail; |
| 5368 | } |
| 5369 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 5370 | fd1, fd2); |
| 5371 | if (errno) { |
| 5372 | posix_error(); |
| 5373 | goto fail; |
| 5374 | } |
| 5375 | break; |
| 5376 | } |
| 5377 | default: { |
| 5378 | PyErr_SetString(PyExc_TypeError, |
| 5379 | "Unknown file_actions identifier"); |
| 5380 | goto fail; |
| 5381 | } |
| 5382 | } |
| 5383 | Py_DECREF(file_action); |
| 5384 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5385 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5386 | Py_DECREF(seq); |
| 5387 | return 0; |
| 5388 | |
| 5389 | fail: |
| 5390 | Py_DECREF(seq); |
| 5391 | Py_DECREF(file_action); |
| 5392 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 5393 | return -1; |
| 5394 | } |
| 5395 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5396 | |
| 5397 | static PyObject * |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5398 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
| 5399 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5400 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5401 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5402 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame^] | 5403 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5404 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5405 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5406 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5407 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5408 | posix_spawnattr_t attr; |
| 5409 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5410 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5411 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5412 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5413 | pid_t pid; |
| 5414 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5415 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5416 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5417 | 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] | 5418 | like posix.environ. */ |
| 5419 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5420 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame^] | 5421 | PyErr_Format(PyExc_TypeError, |
| 5422 | "%s: argv must be a tuple or list", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5423 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5424 | } |
| 5425 | argc = PySequence_Size(argv); |
| 5426 | if (argc < 1) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame^] | 5427 | PyErr_Format(PyExc_ValueError, |
| 5428 | "%s: argv must not be empty", func_name); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5429 | return NULL; |
| 5430 | } |
| 5431 | |
| 5432 | if (!PyMapping_Check(env)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame^] | 5433 | PyErr_Format(PyExc_TypeError, |
| 5434 | "%s: environment must be a mapping object", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5435 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5436 | } |
| 5437 | |
| 5438 | argvlist = parse_arglist(argv, &argc); |
| 5439 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5440 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5441 | } |
| 5442 | if (!argvlist[0][0]) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame^] | 5443 | PyErr_Format(PyExc_ValueError, |
| 5444 | "%s: argv first element cannot be empty", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5445 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5449 | if (envlist == NULL) { |
| 5450 | goto exit; |
| 5451 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5452 | |
Serhiy Storchaka | d700f97 | 2018-09-08 14:48:18 +0300 | [diff] [blame] | 5453 | if (file_actions != NULL) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5454 | /* There is a bug in old versions of glibc that makes some of the |
| 5455 | * helper functions for manipulating file actions not copy the provided |
| 5456 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 5457 | * copy the value of path for some old versions of glibc (<2.20). |
| 5458 | * The use of temp_buffer here is a workaround that keeps the |
| 5459 | * python objects that own the buffers alive until posix_spawn gets called. |
| 5460 | * Check https://bugs.python.org/issue33630 and |
| 5461 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 5462 | temp_buffer = PyList_New(0); |
| 5463 | if (!temp_buffer) { |
| 5464 | goto exit; |
| 5465 | } |
| 5466 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5467 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5468 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5469 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5470 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5471 | |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame^] | 5472 | if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid, |
| 5473 | setsigmask, setsigdef, scheduler, &attr)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5474 | goto exit; |
| 5475 | } |
| 5476 | attrp = &attr; |
| 5477 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5478 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5479 | #ifdef HAVE_POSIX_SPAWNP |
| 5480 | if (use_posix_spawnp) { |
| 5481 | err_code = posix_spawnp(&pid, path->narrow, |
| 5482 | file_actionsp, attrp, argvlist, envlist); |
| 5483 | } |
| 5484 | else |
| 5485 | #endif /* HAVE_POSIX_SPAWNP */ |
| 5486 | { |
| 5487 | err_code = posix_spawn(&pid, path->narrow, |
| 5488 | file_actionsp, attrp, argvlist, envlist); |
| 5489 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5490 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5491 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5492 | if (err_code) { |
| 5493 | errno = err_code; |
| 5494 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5495 | goto exit; |
| 5496 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 5497 | #ifdef _Py_MEMORY_SANITIZER |
| 5498 | __msan_unpoison(&pid, sizeof(pid)); |
| 5499 | #endif |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5500 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5501 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5502 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5503 | if (file_actionsp) { |
| 5504 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5505 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5506 | if (attrp) { |
| 5507 | (void)posix_spawnattr_destroy(attrp); |
| 5508 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5509 | if (envlist) { |
| 5510 | free_string_array(envlist, envc); |
| 5511 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5512 | if (argvlist) { |
| 5513 | free_string_array(argvlist, argc); |
| 5514 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5515 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5516 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5517 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5518 | |
| 5519 | |
| 5520 | /*[clinic input] |
| 5521 | |
| 5522 | os.posix_spawn |
| 5523 | path: path_t |
| 5524 | Path of executable file. |
| 5525 | argv: object |
| 5526 | Tuple or list of strings. |
| 5527 | env: object |
| 5528 | Dictionary of strings mapping to strings. |
| 5529 | / |
| 5530 | * |
| 5531 | file_actions: object(c_default='NULL') = () |
| 5532 | A sequence of file action tuples. |
| 5533 | setpgroup: object = NULL |
| 5534 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5535 | resetids: bool(accept={int}) = False |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5536 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
| 5537 | setsid: bool(accept={int}) = False |
| 5538 | 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] | 5539 | setsigmask: object(c_default='NULL') = () |
| 5540 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5541 | setsigdef: object(c_default='NULL') = () |
| 5542 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5543 | scheduler: object = NULL |
| 5544 | A tuple with the scheduler policy (optional) and parameters. |
| 5545 | |
| 5546 | Execute the program specified by path in a new process. |
| 5547 | [clinic start generated code]*/ |
| 5548 | |
| 5549 | static PyObject * |
| 5550 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5551 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5552 | PyObject *setpgroup, int resetids, int setsid, |
| 5553 | PyObject *setsigmask, PyObject *setsigdef, |
| 5554 | PyObject *scheduler) |
| 5555 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5556 | { |
| 5557 | return py_posix_spawn(0, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5558 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5559 | scheduler); |
| 5560 | } |
| 5561 | #endif /* HAVE_POSIX_SPAWN */ |
| 5562 | |
| 5563 | |
| 5564 | |
| 5565 | #ifdef HAVE_POSIX_SPAWNP |
| 5566 | /*[clinic input] |
| 5567 | |
| 5568 | os.posix_spawnp |
| 5569 | path: path_t |
| 5570 | Path of executable file. |
| 5571 | argv: object |
| 5572 | Tuple or list of strings. |
| 5573 | env: object |
| 5574 | Dictionary of strings mapping to strings. |
| 5575 | / |
| 5576 | * |
| 5577 | file_actions: object(c_default='NULL') = () |
| 5578 | A sequence of file action tuples. |
| 5579 | setpgroup: object = NULL |
| 5580 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5581 | resetids: bool(accept={int}) = False |
| 5582 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5583 | setsid: bool(accept={int}) = False |
| 5584 | 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] | 5585 | setsigmask: object(c_default='NULL') = () |
| 5586 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5587 | setsigdef: object(c_default='NULL') = () |
| 5588 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5589 | scheduler: object = NULL |
| 5590 | A tuple with the scheduler policy (optional) and parameters. |
| 5591 | |
| 5592 | Execute the program specified by path in a new process. |
| 5593 | [clinic start generated code]*/ |
| 5594 | |
| 5595 | static PyObject * |
| 5596 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5597 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5598 | PyObject *setpgroup, int resetids, int setsid, |
| 5599 | PyObject *setsigmask, PyObject *setsigdef, |
| 5600 | PyObject *scheduler) |
| 5601 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5602 | { |
| 5603 | return py_posix_spawn(1, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5604 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5605 | scheduler); |
| 5606 | } |
| 5607 | #endif /* HAVE_POSIX_SPAWNP */ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5608 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5609 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5610 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5611 | /*[clinic input] |
| 5612 | os.spawnv |
| 5613 | |
| 5614 | mode: int |
| 5615 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5616 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5617 | Path of executable file. |
| 5618 | argv: object |
| 5619 | Tuple or list of strings. |
| 5620 | / |
| 5621 | |
| 5622 | Execute the program specified by path in a new process. |
| 5623 | [clinic start generated code]*/ |
| 5624 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5625 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5626 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5627 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5628 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5629 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5630 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5631 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5632 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5633 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5634 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5635 | /* spawnv has three arguments: (mode, path, argv), where |
| 5636 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5637 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5638 | if (PyList_Check(argv)) { |
| 5639 | argc = PyList_Size(argv); |
| 5640 | getitem = PyList_GetItem; |
| 5641 | } |
| 5642 | else if (PyTuple_Check(argv)) { |
| 5643 | argc = PyTuple_Size(argv); |
| 5644 | getitem = PyTuple_GetItem; |
| 5645 | } |
| 5646 | else { |
| 5647 | PyErr_SetString(PyExc_TypeError, |
| 5648 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5649 | return NULL; |
| 5650 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5651 | if (argc == 0) { |
| 5652 | PyErr_SetString(PyExc_ValueError, |
| 5653 | "spawnv() arg 2 cannot be empty"); |
| 5654 | return NULL; |
| 5655 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5656 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5657 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5658 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5659 | return PyErr_NoMemory(); |
| 5660 | } |
| 5661 | for (i = 0; i < argc; i++) { |
| 5662 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5663 | &argvlist[i])) { |
| 5664 | free_string_array(argvlist, i); |
| 5665 | PyErr_SetString( |
| 5666 | PyExc_TypeError, |
| 5667 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5668 | return NULL; |
| 5669 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5670 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5671 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5672 | PyErr_SetString( |
| 5673 | PyExc_ValueError, |
| 5674 | "spawnv() arg 2 first element cannot be empty"); |
| 5675 | return NULL; |
| 5676 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5677 | } |
| 5678 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5679 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5680 | if (mode == _OLD_P_OVERLAY) |
| 5681 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5682 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5683 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5684 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5685 | #ifdef HAVE_WSPAWNV |
| 5686 | spawnval = _wspawnv(mode, path->wide, argvlist); |
| 5687 | #else |
| 5688 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 5689 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5690 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5691 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5692 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5693 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5694 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5695 | if (spawnval == -1) |
| 5696 | return posix_error(); |
| 5697 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5698 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5699 | } |
| 5700 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5701 | /*[clinic input] |
| 5702 | os.spawnve |
| 5703 | |
| 5704 | mode: int |
| 5705 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5706 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5707 | Path of executable file. |
| 5708 | argv: object |
| 5709 | Tuple or list of strings. |
| 5710 | env: object |
| 5711 | Dictionary of strings mapping to strings. |
| 5712 | / |
| 5713 | |
| 5714 | Execute the program specified by path in a new process. |
| 5715 | [clinic start generated code]*/ |
| 5716 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5717 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5718 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5719 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5720 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5721 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5722 | EXECV_CHAR **argvlist; |
| 5723 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5724 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5725 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5726 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5727 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5728 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5729 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5730 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5731 | argv is a list or tuple of strings and env is a dictionary |
| 5732 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5733 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5734 | if (PyList_Check(argv)) { |
| 5735 | argc = PyList_Size(argv); |
| 5736 | getitem = PyList_GetItem; |
| 5737 | } |
| 5738 | else if (PyTuple_Check(argv)) { |
| 5739 | argc = PyTuple_Size(argv); |
| 5740 | getitem = PyTuple_GetItem; |
| 5741 | } |
| 5742 | else { |
| 5743 | PyErr_SetString(PyExc_TypeError, |
| 5744 | "spawnve() arg 2 must be a tuple or list"); |
| 5745 | goto fail_0; |
| 5746 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5747 | if (argc == 0) { |
| 5748 | PyErr_SetString(PyExc_ValueError, |
| 5749 | "spawnve() arg 2 cannot be empty"); |
| 5750 | goto fail_0; |
| 5751 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5752 | if (!PyMapping_Check(env)) { |
| 5753 | PyErr_SetString(PyExc_TypeError, |
| 5754 | "spawnve() arg 3 must be a mapping object"); |
| 5755 | goto fail_0; |
| 5756 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5757 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5758 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5759 | if (argvlist == NULL) { |
| 5760 | PyErr_NoMemory(); |
| 5761 | goto fail_0; |
| 5762 | } |
| 5763 | for (i = 0; i < argc; i++) { |
| 5764 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5765 | &argvlist[i])) |
| 5766 | { |
| 5767 | lastarg = i; |
| 5768 | goto fail_1; |
| 5769 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5770 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5771 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5772 | PyErr_SetString( |
| 5773 | PyExc_ValueError, |
| 5774 | "spawnv() arg 2 first element cannot be empty"); |
| 5775 | goto fail_1; |
| 5776 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5777 | } |
| 5778 | lastarg = argc; |
| 5779 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5780 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5781 | envlist = parse_envlist(env, &envc); |
| 5782 | if (envlist == NULL) |
| 5783 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5784 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5785 | if (mode == _OLD_P_OVERLAY) |
| 5786 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5787 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5788 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5789 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5790 | #ifdef HAVE_WSPAWNV |
| 5791 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
| 5792 | #else |
| 5793 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 5794 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5795 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5796 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5797 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5798 | if (spawnval == -1) |
| 5799 | (void) posix_error(); |
| 5800 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5801 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5802 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5803 | while (--envc >= 0) |
| 5804 | PyMem_DEL(envlist[envc]); |
| 5805 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5806 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5807 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5808 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5809 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5810 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5811 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5812 | #endif /* HAVE_SPAWNV */ |
| 5813 | |
| 5814 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5815 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5816 | |
| 5817 | /* Helper function to validate arguments. |
| 5818 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 5819 | If obj is non-NULL it must be callable. */ |
| 5820 | static int |
| 5821 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 5822 | { |
| 5823 | if (obj && !PyCallable_Check(obj)) { |
| 5824 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
| 5825 | obj_name, Py_TYPE(obj)->tp_name); |
| 5826 | return -1; |
| 5827 | } |
| 5828 | return 0; |
| 5829 | } |
| 5830 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5831 | /*[clinic input] |
| 5832 | os.register_at_fork |
| 5833 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5834 | * |
| 5835 | before: object=NULL |
| 5836 | A callable to be called in the parent before the fork() syscall. |
| 5837 | after_in_child: object=NULL |
| 5838 | A callable to be called in the child after fork(). |
| 5839 | after_in_parent: object=NULL |
| 5840 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5841 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5842 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5843 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5844 | 'before' callbacks are called in reverse order. |
| 5845 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5846 | |
| 5847 | [clinic start generated code]*/ |
| 5848 | |
| 5849 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5850 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 5851 | PyObject *after_in_child, PyObject *after_in_parent) |
| 5852 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5853 | { |
| 5854 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5855 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5856 | if (!before && !after_in_child && !after_in_parent) { |
| 5857 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 5858 | return NULL; |
| 5859 | } |
| 5860 | if (check_null_or_callable(before, "before") || |
| 5861 | check_null_or_callable(after_in_child, "after_in_child") || |
| 5862 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5863 | return NULL; |
| 5864 | } |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 5865 | interp = _PyInterpreterState_Get(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5866 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5867 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5868 | return NULL; |
| 5869 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5870 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5871 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5872 | } |
| 5873 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 5874 | return NULL; |
| 5875 | } |
| 5876 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5877 | } |
| 5878 | #endif /* HAVE_FORK */ |
| 5879 | |
| 5880 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5881 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5882 | /*[clinic input] |
| 5883 | os.fork1 |
| 5884 | |
| 5885 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 5886 | |
| 5887 | Return 0 to child process and PID of child to parent process. |
| 5888 | [clinic start generated code]*/ |
| 5889 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5890 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5891 | os_fork1_impl(PyObject *module) |
| 5892 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5893 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5894 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5895 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 5896 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 5897 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 5898 | return NULL; |
| 5899 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5900 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5901 | pid = fork1(); |
| 5902 | if (pid == 0) { |
| 5903 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5904 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5905 | } else { |
| 5906 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5907 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5908 | } |
| 5909 | if (pid == -1) |
| 5910 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5911 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5912 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5913 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5914 | |
| 5915 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5916 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5917 | /*[clinic input] |
| 5918 | os.fork |
| 5919 | |
| 5920 | Fork a child process. |
| 5921 | |
| 5922 | Return 0 to child process and PID of child to parent process. |
| 5923 | [clinic start generated code]*/ |
| 5924 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5925 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5926 | os_fork_impl(PyObject *module) |
| 5927 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5928 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5929 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5930 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 5931 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 5932 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 5933 | return NULL; |
| 5934 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5935 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5936 | pid = fork(); |
| 5937 | if (pid == 0) { |
| 5938 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5939 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5940 | } else { |
| 5941 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5942 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5943 | } |
| 5944 | if (pid == -1) |
| 5945 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5946 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5947 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5948 | #endif /* HAVE_FORK */ |
| 5949 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5950 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5951 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5952 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5953 | /*[clinic input] |
| 5954 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5955 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5956 | policy: int |
| 5957 | |
| 5958 | Get the maximum scheduling priority for policy. |
| 5959 | [clinic start generated code]*/ |
| 5960 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5961 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5962 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 5963 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5964 | { |
| 5965 | int max; |
| 5966 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5967 | max = sched_get_priority_max(policy); |
| 5968 | if (max < 0) |
| 5969 | return posix_error(); |
| 5970 | return PyLong_FromLong(max); |
| 5971 | } |
| 5972 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5973 | |
| 5974 | /*[clinic input] |
| 5975 | os.sched_get_priority_min |
| 5976 | |
| 5977 | policy: int |
| 5978 | |
| 5979 | Get the minimum scheduling priority for policy. |
| 5980 | [clinic start generated code]*/ |
| 5981 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5982 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5983 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 5984 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5985 | { |
| 5986 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5987 | if (min < 0) |
| 5988 | return posix_error(); |
| 5989 | return PyLong_FromLong(min); |
| 5990 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5991 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5992 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5993 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5994 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5995 | /*[clinic input] |
| 5996 | os.sched_getscheduler |
| 5997 | pid: pid_t |
| 5998 | / |
| 5999 | |
| 6000 | Get the scheduling policy for the process identifiedy by pid. |
| 6001 | |
| 6002 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 6003 | [clinic start generated code]*/ |
| 6004 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6005 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6006 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
| 6007 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6008 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6009 | int policy; |
| 6010 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6011 | policy = sched_getscheduler(pid); |
| 6012 | if (policy < 0) |
| 6013 | return posix_error(); |
| 6014 | return PyLong_FromLong(policy); |
| 6015 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6016 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6017 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6018 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6019 | #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] | 6020 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6021 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6022 | |
| 6023 | @classmethod |
| 6024 | os.sched_param.__new__ |
| 6025 | |
| 6026 | sched_priority: object |
| 6027 | A scheduling parameter. |
| 6028 | |
| 6029 | Current has only one field: sched_priority"); |
| 6030 | [clinic start generated code]*/ |
| 6031 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6032 | static PyObject * |
| 6033 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6034 | /*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6035 | { |
| 6036 | PyObject *res; |
| 6037 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6038 | res = PyStructSequence_New(type); |
| 6039 | if (!res) |
| 6040 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6041 | Py_INCREF(sched_priority); |
| 6042 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6043 | return res; |
| 6044 | } |
| 6045 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6046 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6047 | PyDoc_VAR(os_sched_param__doc__); |
| 6048 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6049 | static PyStructSequence_Field sched_param_fields[] = { |
| 6050 | {"sched_priority", "the scheduling priority"}, |
| 6051 | {0} |
| 6052 | }; |
| 6053 | |
| 6054 | static PyStructSequence_Desc sched_param_desc = { |
| 6055 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6056 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6057 | sched_param_fields, |
| 6058 | 1 |
| 6059 | }; |
| 6060 | |
| 6061 | static int |
| 6062 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 6063 | { |
| 6064 | long priority; |
| 6065 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6066 | if (Py_TYPE(param) != SchedParamType) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6067 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 6068 | return 0; |
| 6069 | } |
| 6070 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 6071 | if (priority == -1 && PyErr_Occurred()) |
| 6072 | return 0; |
| 6073 | if (priority > INT_MAX || priority < INT_MIN) { |
| 6074 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 6075 | return 0; |
| 6076 | } |
| 6077 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 6078 | return 1; |
| 6079 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6080 | #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] | 6081 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6082 | |
| 6083 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6084 | /*[clinic input] |
| 6085 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6086 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6087 | pid: pid_t |
| 6088 | policy: int |
| 6089 | param: sched_param |
| 6090 | / |
| 6091 | |
| 6092 | Set the scheduling policy for the process identified by pid. |
| 6093 | |
| 6094 | If pid is 0, the calling process is changed. |
| 6095 | param is an instance of sched_param. |
| 6096 | [clinic start generated code]*/ |
| 6097 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6098 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6099 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6100 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6101 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6102 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6103 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6104 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6105 | ** scheduling policy under Solaris/Illumos, and others. |
| 6106 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6107 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6108 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6109 | return posix_error(); |
| 6110 | Py_RETURN_NONE; |
| 6111 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6112 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6113 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6114 | |
| 6115 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6116 | /*[clinic input] |
| 6117 | os.sched_getparam |
| 6118 | pid: pid_t |
| 6119 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6120 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6121 | Returns scheduling parameters for the process identified by pid. |
| 6122 | |
| 6123 | If pid is 0, returns parameters for the calling process. |
| 6124 | Return value is an instance of sched_param. |
| 6125 | [clinic start generated code]*/ |
| 6126 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6127 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6128 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6129 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6130 | { |
| 6131 | struct sched_param param; |
| 6132 | PyObject *result; |
| 6133 | PyObject *priority; |
| 6134 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6135 | if (sched_getparam(pid, ¶m)) |
| 6136 | return posix_error(); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6137 | result = PyStructSequence_New(SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6138 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6139 | return NULL; |
| 6140 | priority = PyLong_FromLong(param.sched_priority); |
| 6141 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6142 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6143 | return NULL; |
| 6144 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6145 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6146 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6147 | } |
| 6148 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6149 | |
| 6150 | /*[clinic input] |
| 6151 | os.sched_setparam |
| 6152 | pid: pid_t |
| 6153 | param: sched_param |
| 6154 | / |
| 6155 | |
| 6156 | Set scheduling parameters for the process identified by pid. |
| 6157 | |
| 6158 | If pid is 0, sets parameters for the calling process. |
| 6159 | param should be an instance of sched_param. |
| 6160 | [clinic start generated code]*/ |
| 6161 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6162 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6163 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6164 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6165 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6166 | { |
| 6167 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6168 | return posix_error(); |
| 6169 | Py_RETURN_NONE; |
| 6170 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6171 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6172 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6173 | |
| 6174 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6175 | /*[clinic input] |
| 6176 | os.sched_rr_get_interval -> double |
| 6177 | pid: pid_t |
| 6178 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6179 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6180 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6181 | |
| 6182 | Value returned is a float. |
| 6183 | [clinic start generated code]*/ |
| 6184 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6185 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6186 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6187 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6188 | { |
| 6189 | struct timespec interval; |
| 6190 | if (sched_rr_get_interval(pid, &interval)) { |
| 6191 | posix_error(); |
| 6192 | return -1.0; |
| 6193 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6194 | #ifdef _Py_MEMORY_SANITIZER |
| 6195 | __msan_unpoison(&interval, sizeof(interval)); |
| 6196 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6197 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6198 | } |
| 6199 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6200 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6201 | |
| 6202 | /*[clinic input] |
| 6203 | os.sched_yield |
| 6204 | |
| 6205 | Voluntarily relinquish the CPU. |
| 6206 | [clinic start generated code]*/ |
| 6207 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6208 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6209 | os_sched_yield_impl(PyObject *module) |
| 6210 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6211 | { |
| 6212 | if (sched_yield()) |
| 6213 | return posix_error(); |
| 6214 | Py_RETURN_NONE; |
| 6215 | } |
| 6216 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6217 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6218 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6219 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6220 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6221 | /*[clinic input] |
| 6222 | os.sched_setaffinity |
| 6223 | pid: pid_t |
| 6224 | mask : object |
| 6225 | / |
| 6226 | |
| 6227 | Set the CPU affinity of the process identified by pid to mask. |
| 6228 | |
| 6229 | mask should be an iterable of integers identifying CPUs. |
| 6230 | [clinic start generated code]*/ |
| 6231 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6232 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6233 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6234 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6235 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6236 | int ncpus; |
| 6237 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6238 | cpu_set_t *cpu_set = NULL; |
| 6239 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6240 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6241 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6242 | if (iterator == NULL) |
| 6243 | return NULL; |
| 6244 | |
| 6245 | ncpus = NCPUS_START; |
| 6246 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6247 | cpu_set = CPU_ALLOC(ncpus); |
| 6248 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6249 | PyErr_NoMemory(); |
| 6250 | goto error; |
| 6251 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6252 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6253 | |
| 6254 | while ((item = PyIter_Next(iterator))) { |
| 6255 | long cpu; |
| 6256 | if (!PyLong_Check(item)) { |
| 6257 | PyErr_Format(PyExc_TypeError, |
| 6258 | "expected an iterator of ints, " |
| 6259 | "but iterator yielded %R", |
| 6260 | Py_TYPE(item)); |
| 6261 | Py_DECREF(item); |
| 6262 | goto error; |
| 6263 | } |
| 6264 | cpu = PyLong_AsLong(item); |
| 6265 | Py_DECREF(item); |
| 6266 | if (cpu < 0) { |
| 6267 | if (!PyErr_Occurred()) |
| 6268 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6269 | goto error; |
| 6270 | } |
| 6271 | if (cpu > INT_MAX - 1) { |
| 6272 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6273 | goto error; |
| 6274 | } |
| 6275 | if (cpu >= ncpus) { |
| 6276 | /* Grow CPU mask to fit the CPU number */ |
| 6277 | int newncpus = ncpus; |
| 6278 | cpu_set_t *newmask; |
| 6279 | size_t newsetsize; |
| 6280 | while (newncpus <= cpu) { |
| 6281 | if (newncpus > INT_MAX / 2) |
| 6282 | newncpus = cpu + 1; |
| 6283 | else |
| 6284 | newncpus = newncpus * 2; |
| 6285 | } |
| 6286 | newmask = CPU_ALLOC(newncpus); |
| 6287 | if (newmask == NULL) { |
| 6288 | PyErr_NoMemory(); |
| 6289 | goto error; |
| 6290 | } |
| 6291 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 6292 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6293 | memcpy(newmask, cpu_set, setsize); |
| 6294 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6295 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6296 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6297 | ncpus = newncpus; |
| 6298 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6299 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6300 | } |
| 6301 | Py_CLEAR(iterator); |
| 6302 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6303 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6304 | posix_error(); |
| 6305 | goto error; |
| 6306 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6307 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6308 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6309 | |
| 6310 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6311 | if (cpu_set) |
| 6312 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6313 | Py_XDECREF(iterator); |
| 6314 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6315 | } |
| 6316 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6317 | |
| 6318 | /*[clinic input] |
| 6319 | os.sched_getaffinity |
| 6320 | pid: pid_t |
| 6321 | / |
| 6322 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6323 | 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] | 6324 | |
| 6325 | The affinity is returned as a set of CPU identifiers. |
| 6326 | [clinic start generated code]*/ |
| 6327 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6328 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6329 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6330 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6331 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6332 | int cpu, ncpus, count; |
| 6333 | size_t setsize; |
| 6334 | cpu_set_t *mask = NULL; |
| 6335 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6336 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6337 | ncpus = NCPUS_START; |
| 6338 | while (1) { |
| 6339 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6340 | mask = CPU_ALLOC(ncpus); |
| 6341 | if (mask == NULL) |
| 6342 | return PyErr_NoMemory(); |
| 6343 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6344 | break; |
| 6345 | CPU_FREE(mask); |
| 6346 | if (errno != EINVAL) |
| 6347 | return posix_error(); |
| 6348 | if (ncpus > INT_MAX / 2) { |
| 6349 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6350 | "a large enough CPU set"); |
| 6351 | return NULL; |
| 6352 | } |
| 6353 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6354 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6355 | |
| 6356 | res = PySet_New(NULL); |
| 6357 | if (res == NULL) |
| 6358 | goto error; |
| 6359 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6360 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6361 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6362 | --count; |
| 6363 | if (cpu_num == NULL) |
| 6364 | goto error; |
| 6365 | if (PySet_Add(res, cpu_num)) { |
| 6366 | Py_DECREF(cpu_num); |
| 6367 | goto error; |
| 6368 | } |
| 6369 | Py_DECREF(cpu_num); |
| 6370 | } |
| 6371 | } |
| 6372 | CPU_FREE(mask); |
| 6373 | return res; |
| 6374 | |
| 6375 | error: |
| 6376 | if (mask) |
| 6377 | CPU_FREE(mask); |
| 6378 | Py_XDECREF(res); |
| 6379 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6380 | } |
| 6381 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6382 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6383 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6384 | #endif /* HAVE_SCHED_H */ |
| 6385 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6386 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6387 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6388 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6389 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6390 | #define DEV_PTY_FILE "/dev/ptc" |
| 6391 | #define HAVE_DEV_PTMX |
| 6392 | #else |
| 6393 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6394 | #endif |
| 6395 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6396 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6397 | #ifdef HAVE_PTY_H |
| 6398 | #include <pty.h> |
| 6399 | #else |
| 6400 | #ifdef HAVE_LIBUTIL_H |
| 6401 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6402 | #else |
| 6403 | #ifdef HAVE_UTIL_H |
| 6404 | #include <util.h> |
| 6405 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6406 | #endif /* HAVE_LIBUTIL_H */ |
| 6407 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6408 | #ifdef HAVE_STROPTS_H |
| 6409 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6410 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6411 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6413 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6414 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6415 | /*[clinic input] |
| 6416 | os.openpty |
| 6417 | |
| 6418 | Open a pseudo-terminal. |
| 6419 | |
| 6420 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6421 | for both the master and slave ends. |
| 6422 | [clinic start generated code]*/ |
| 6423 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6424 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6425 | os_openpty_impl(PyObject *module) |
| 6426 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6427 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6428 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6429 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6430 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6431 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6432 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6433 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 6434 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6435 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6436 | #endif |
| 6437 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6438 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6439 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6440 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6441 | goto posix_error; |
| 6442 | |
| 6443 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6444 | goto error; |
| 6445 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6446 | goto error; |
| 6447 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6448 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6449 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6450 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6451 | goto posix_error; |
| 6452 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6453 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6454 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6455 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6456 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6457 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6458 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6459 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6460 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6461 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6462 | goto posix_error; |
| 6463 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6464 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6465 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6466 | /* change permission of slave */ |
| 6467 | if (grantpt(master_fd) < 0) { |
| 6468 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6469 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6470 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6471 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6472 | /* unlock slave */ |
| 6473 | if (unlockpt(master_fd) < 0) { |
| 6474 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6475 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6476 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6477 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6478 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6479 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6480 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6481 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6482 | goto posix_error; |
| 6483 | |
| 6484 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6485 | if (slave_fd == -1) |
| 6486 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6487 | |
| 6488 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6489 | goto posix_error; |
| 6490 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6491 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6492 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6493 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6494 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6495 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6496 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6497 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6498 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6499 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6500 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6501 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6502 | posix_error: |
| 6503 | posix_error(); |
| 6504 | error: |
| 6505 | if (master_fd != -1) |
| 6506 | close(master_fd); |
| 6507 | if (slave_fd != -1) |
| 6508 | close(slave_fd); |
| 6509 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6510 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6511 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6512 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6513 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6514 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6515 | /*[clinic input] |
| 6516 | os.forkpty |
| 6517 | |
| 6518 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6519 | |
| 6520 | Returns a tuple of (pid, master_fd). |
| 6521 | Like fork(), return pid of 0 to the child process, |
| 6522 | and pid of child to the parent process. |
| 6523 | To both, return fd of newly opened pseudo-terminal. |
| 6524 | [clinic start generated code]*/ |
| 6525 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6526 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6527 | os_forkpty_impl(PyObject *module) |
| 6528 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6529 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6530 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6531 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6532 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6533 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6534 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6535 | return NULL; |
| 6536 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6537 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6538 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6539 | if (pid == 0) { |
| 6540 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6541 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6542 | } else { |
| 6543 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6544 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6545 | } |
| 6546 | if (pid == -1) |
| 6547 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6548 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6549 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6550 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6551 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6552 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6553 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6554 | /*[clinic input] |
| 6555 | os.getegid |
| 6556 | |
| 6557 | Return the current process's effective group id. |
| 6558 | [clinic start generated code]*/ |
| 6559 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6560 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6561 | os_getegid_impl(PyObject *module) |
| 6562 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6563 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6564 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6565 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6566 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6567 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6568 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6569 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6570 | /*[clinic input] |
| 6571 | os.geteuid |
| 6572 | |
| 6573 | Return the current process's effective user id. |
| 6574 | [clinic start generated code]*/ |
| 6575 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6576 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6577 | os_geteuid_impl(PyObject *module) |
| 6578 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6579 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6580 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6581 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6582 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6583 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6584 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6585 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6586 | /*[clinic input] |
| 6587 | os.getgid |
| 6588 | |
| 6589 | Return the current process's group id. |
| 6590 | [clinic start generated code]*/ |
| 6591 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6592 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6593 | os_getgid_impl(PyObject *module) |
| 6594 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6595 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6596 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6597 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6598 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6599 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6600 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6601 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6602 | /*[clinic input] |
| 6603 | os.getpid |
| 6604 | |
| 6605 | Return the current process id. |
| 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_getpid_impl(PyObject *module) |
| 6610 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6611 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6612 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6613 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6614 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6615 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6616 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6617 | |
| 6618 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6619 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6620 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6621 | Returns a list of groups to which a user belongs.\n\n\ |
| 6622 | user: username to lookup\n\ |
| 6623 | group: base group id of the user"); |
| 6624 | |
| 6625 | static PyObject * |
| 6626 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6627 | { |
| 6628 | #ifdef NGROUPS_MAX |
| 6629 | #define MAX_GROUPS NGROUPS_MAX |
| 6630 | #else |
| 6631 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6632 | #define MAX_GROUPS 64 |
| 6633 | #endif |
| 6634 | |
| 6635 | const char *user; |
| 6636 | int i, ngroups; |
| 6637 | PyObject *list; |
| 6638 | #ifdef __APPLE__ |
| 6639 | int *groups, basegid; |
| 6640 | #else |
| 6641 | gid_t *groups, basegid; |
| 6642 | #endif |
| 6643 | ngroups = MAX_GROUPS; |
| 6644 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6645 | #ifdef __APPLE__ |
| 6646 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6647 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6648 | #else |
| 6649 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6650 | _Py_Gid_Converter, &basegid)) |
| 6651 | return NULL; |
| 6652 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6653 | |
| 6654 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6655 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6656 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6657 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6658 | #endif |
| 6659 | if (groups == NULL) |
| 6660 | return PyErr_NoMemory(); |
| 6661 | |
| 6662 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6663 | PyMem_Del(groups); |
| 6664 | return posix_error(); |
| 6665 | } |
| 6666 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6667 | #ifdef _Py_MEMORY_SANITIZER |
| 6668 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
| 6669 | __msan_unpoison(&ngroups, sizeof(ngroups)); |
| 6670 | __msan_unpoison(groups, ngroups*sizeof(*groups)); |
| 6671 | #endif |
| 6672 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6673 | list = PyList_New(ngroups); |
| 6674 | if (list == NULL) { |
| 6675 | PyMem_Del(groups); |
| 6676 | return NULL; |
| 6677 | } |
| 6678 | |
| 6679 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6680 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6681 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6682 | #else |
| 6683 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6684 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6685 | if (o == NULL) { |
| 6686 | Py_DECREF(list); |
| 6687 | PyMem_Del(groups); |
| 6688 | return NULL; |
| 6689 | } |
| 6690 | PyList_SET_ITEM(list, i, o); |
| 6691 | } |
| 6692 | |
| 6693 | PyMem_Del(groups); |
| 6694 | |
| 6695 | return list; |
| 6696 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6697 | #endif /* HAVE_GETGROUPLIST */ |
| 6698 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6699 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6700 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6701 | /*[clinic input] |
| 6702 | os.getgroups |
| 6703 | |
| 6704 | Return list of supplemental group IDs for the process. |
| 6705 | [clinic start generated code]*/ |
| 6706 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6707 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6708 | os_getgroups_impl(PyObject *module) |
| 6709 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6710 | { |
| 6711 | PyObject *result = NULL; |
| 6712 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6713 | #ifdef NGROUPS_MAX |
| 6714 | #define MAX_GROUPS NGROUPS_MAX |
| 6715 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6716 | /* 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] | 6717 | #define MAX_GROUPS 64 |
| 6718 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6719 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6720 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6721 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6722 | * This is a helper variable to store the intermediate result when |
| 6723 | * that happens. |
| 6724 | * |
| 6725 | * To keep the code readable the OSX behaviour is unconditional, |
| 6726 | * according to the POSIX spec this should be safe on all unix-y |
| 6727 | * systems. |
| 6728 | */ |
| 6729 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6730 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6731 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6732 | #ifdef __APPLE__ |
| 6733 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6734 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6735 | * always first call getgroups with length 0 to get the actual number |
| 6736 | * of groups. |
| 6737 | */ |
| 6738 | n = getgroups(0, NULL); |
| 6739 | if (n < 0) { |
| 6740 | return posix_error(); |
| 6741 | } else if (n <= MAX_GROUPS) { |
| 6742 | /* groups will fit in existing array */ |
| 6743 | alt_grouplist = grouplist; |
| 6744 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6745 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6746 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 6747 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6748 | } |
| 6749 | } |
| 6750 | |
| 6751 | n = getgroups(n, alt_grouplist); |
| 6752 | if (n == -1) { |
| 6753 | if (alt_grouplist != grouplist) { |
| 6754 | PyMem_Free(alt_grouplist); |
| 6755 | } |
| 6756 | return posix_error(); |
| 6757 | } |
| 6758 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6759 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6760 | if (n < 0) { |
| 6761 | if (errno == EINVAL) { |
| 6762 | n = getgroups(0, NULL); |
| 6763 | if (n == -1) { |
| 6764 | return posix_error(); |
| 6765 | } |
| 6766 | if (n == 0) { |
| 6767 | /* Avoid malloc(0) */ |
| 6768 | alt_grouplist = grouplist; |
| 6769 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6770 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6771 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 6772 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6773 | } |
| 6774 | n = getgroups(n, alt_grouplist); |
| 6775 | if (n == -1) { |
| 6776 | PyMem_Free(alt_grouplist); |
| 6777 | return posix_error(); |
| 6778 | } |
| 6779 | } |
| 6780 | } else { |
| 6781 | return posix_error(); |
| 6782 | } |
| 6783 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6784 | #endif |
| 6785 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6786 | result = PyList_New(n); |
| 6787 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6788 | int i; |
| 6789 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6790 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6791 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6792 | Py_DECREF(result); |
| 6793 | result = NULL; |
| 6794 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6795 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6796 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6797 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6798 | } |
| 6799 | |
| 6800 | if (alt_grouplist != grouplist) { |
| 6801 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6802 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6803 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6804 | return result; |
| 6805 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6806 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6807 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6808 | #ifdef HAVE_INITGROUPS |
| 6809 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6810 | "initgroups(username, gid) -> None\n\n\ |
| 6811 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6812 | the groups of which the specified username is a member, plus the specified\n\ |
| 6813 | group id."); |
| 6814 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6815 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6816 | static PyObject * |
| 6817 | posix_initgroups(PyObject *self, PyObject *args) |
| 6818 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6819 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6820 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6821 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6822 | #ifdef __APPLE__ |
| 6823 | int gid; |
| 6824 | #else |
| 6825 | gid_t gid; |
| 6826 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6827 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6828 | #ifdef __APPLE__ |
| 6829 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6830 | PyUnicode_FSConverter, &oname, |
| 6831 | &gid)) |
| 6832 | #else |
| 6833 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6834 | PyUnicode_FSConverter, &oname, |
| 6835 | _Py_Gid_Converter, &gid)) |
| 6836 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6837 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6838 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6839 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6840 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6841 | Py_DECREF(oname); |
| 6842 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6843 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6844 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 6845 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6846 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6847 | #endif /* HAVE_INITGROUPS */ |
| 6848 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6849 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6850 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6851 | /*[clinic input] |
| 6852 | os.getpgid |
| 6853 | |
| 6854 | pid: pid_t |
| 6855 | |
| 6856 | Call the system call getpgid(), and return the result. |
| 6857 | [clinic start generated code]*/ |
| 6858 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6859 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6860 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 6861 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6862 | { |
| 6863 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6864 | if (pgid < 0) |
| 6865 | return posix_error(); |
| 6866 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6867 | } |
| 6868 | #endif /* HAVE_GETPGID */ |
| 6869 | |
| 6870 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6871 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6872 | /*[clinic input] |
| 6873 | os.getpgrp |
| 6874 | |
| 6875 | Return the current process group id. |
| 6876 | [clinic start generated code]*/ |
| 6877 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6878 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6879 | os_getpgrp_impl(PyObject *module) |
| 6880 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6881 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6882 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6883 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6884 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6885 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6886 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6887 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6888 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6889 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6890 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6891 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6892 | /*[clinic input] |
| 6893 | os.setpgrp |
| 6894 | |
| 6895 | Make the current process the leader of its process group. |
| 6896 | [clinic start generated code]*/ |
| 6897 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6898 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6899 | os_setpgrp_impl(PyObject *module) |
| 6900 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6901 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6902 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6903 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6904 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6905 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6906 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6907 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 6908 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6909 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6910 | #endif /* HAVE_SETPGRP */ |
| 6911 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6912 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6913 | |
| 6914 | #ifdef MS_WINDOWS |
| 6915 | #include <tlhelp32.h> |
| 6916 | |
| 6917 | static PyObject* |
| 6918 | win32_getppid() |
| 6919 | { |
| 6920 | HANDLE snapshot; |
| 6921 | pid_t mypid; |
| 6922 | PyObject* result = NULL; |
| 6923 | BOOL have_record; |
| 6924 | PROCESSENTRY32 pe; |
| 6925 | |
| 6926 | mypid = getpid(); /* This function never fails */ |
| 6927 | |
| 6928 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6929 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6930 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6931 | |
| 6932 | pe.dwSize = sizeof(pe); |
| 6933 | have_record = Process32First(snapshot, &pe); |
| 6934 | while (have_record) { |
| 6935 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6936 | /* We could cache the ulong value in a static variable. */ |
| 6937 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6938 | break; |
| 6939 | } |
| 6940 | |
| 6941 | have_record = Process32Next(snapshot, &pe); |
| 6942 | } |
| 6943 | |
| 6944 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6945 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6946 | * error anyway, so let's raise it. */ |
| 6947 | if (!result) |
| 6948 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6949 | |
| 6950 | CloseHandle(snapshot); |
| 6951 | |
| 6952 | return result; |
| 6953 | } |
| 6954 | #endif /*MS_WINDOWS*/ |
| 6955 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6956 | |
| 6957 | /*[clinic input] |
| 6958 | os.getppid |
| 6959 | |
| 6960 | Return the parent's process id. |
| 6961 | |
| 6962 | If the parent process has already exited, Windows machines will still |
| 6963 | return its id; others systems will return the id of the 'init' process (1). |
| 6964 | [clinic start generated code]*/ |
| 6965 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6966 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6967 | os_getppid_impl(PyObject *module) |
| 6968 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6969 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6970 | #ifdef MS_WINDOWS |
| 6971 | return win32_getppid(); |
| 6972 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6973 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6974 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6975 | } |
| 6976 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6977 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6978 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6979 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6980 | /*[clinic input] |
| 6981 | os.getlogin |
| 6982 | |
| 6983 | Return the actual login name. |
| 6984 | [clinic start generated code]*/ |
| 6985 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6986 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6987 | os_getlogin_impl(PyObject *module) |
| 6988 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6989 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6990 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6991 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6992 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6993 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6994 | |
| 6995 | if (GetUserNameW(user_name, &num_chars)) { |
| 6996 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6997 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6998 | } |
| 6999 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7000 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7001 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7002 | char *name; |
| 7003 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7004 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7005 | errno = 0; |
| 7006 | name = getlogin(); |
| 7007 | if (name == NULL) { |
| 7008 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7009 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7010 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7011 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7012 | } |
| 7013 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7014 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7015 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7016 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7017 | return result; |
| 7018 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7019 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7020 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7021 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7022 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7023 | /*[clinic input] |
| 7024 | os.getuid |
| 7025 | |
| 7026 | Return the current process's user id. |
| 7027 | [clinic start generated code]*/ |
| 7028 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7029 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7030 | os_getuid_impl(PyObject *module) |
| 7031 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7032 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7033 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7034 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7035 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7036 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7037 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7038 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7039 | #define HAVE_KILL |
| 7040 | #endif /* MS_WINDOWS */ |
| 7041 | |
| 7042 | #ifdef HAVE_KILL |
| 7043 | /*[clinic input] |
| 7044 | os.kill |
| 7045 | |
| 7046 | pid: pid_t |
| 7047 | signal: Py_ssize_t |
| 7048 | / |
| 7049 | |
| 7050 | Kill a process with a signal. |
| 7051 | [clinic start generated code]*/ |
| 7052 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7053 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7054 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 7055 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7056 | #ifndef MS_WINDOWS |
| 7057 | { |
| 7058 | if (kill(pid, (int)signal) == -1) |
| 7059 | return posix_error(); |
| 7060 | Py_RETURN_NONE; |
| 7061 | } |
| 7062 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7063 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7064 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7065 | DWORD sig = (DWORD)signal; |
| 7066 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7067 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7068 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7069 | /* Console processes which share a common console can be sent CTRL+C or |
| 7070 | CTRL+BREAK events, provided they handle said events. */ |
| 7071 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7072 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7073 | err = GetLastError(); |
| 7074 | PyErr_SetFromWindowsErr(err); |
| 7075 | } |
| 7076 | else |
| 7077 | Py_RETURN_NONE; |
| 7078 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7079 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7080 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 7081 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7082 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7083 | if (handle == NULL) { |
| 7084 | err = GetLastError(); |
| 7085 | return PyErr_SetFromWindowsErr(err); |
| 7086 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7087 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7088 | if (TerminateProcess(handle, sig) == 0) { |
| 7089 | err = GetLastError(); |
| 7090 | result = PyErr_SetFromWindowsErr(err); |
| 7091 | } else { |
| 7092 | Py_INCREF(Py_None); |
| 7093 | result = Py_None; |
| 7094 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7095 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7096 | CloseHandle(handle); |
| 7097 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7098 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7099 | #endif /* !MS_WINDOWS */ |
| 7100 | #endif /* HAVE_KILL */ |
| 7101 | |
| 7102 | |
| 7103 | #ifdef HAVE_KILLPG |
| 7104 | /*[clinic input] |
| 7105 | os.killpg |
| 7106 | |
| 7107 | pgid: pid_t |
| 7108 | signal: int |
| 7109 | / |
| 7110 | |
| 7111 | Kill a process group with a signal. |
| 7112 | [clinic start generated code]*/ |
| 7113 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7114 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7115 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7116 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7117 | { |
| 7118 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7119 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7120 | take the same type. Moreover, pid_t is always at least as wide as |
| 7121 | int (else compilation of this module fails), which is safe. */ |
| 7122 | if (killpg(pgid, signal) == -1) |
| 7123 | return posix_error(); |
| 7124 | Py_RETURN_NONE; |
| 7125 | } |
| 7126 | #endif /* HAVE_KILLPG */ |
| 7127 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7128 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7129 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7130 | #ifdef HAVE_SYS_LOCK_H |
| 7131 | #include <sys/lock.h> |
| 7132 | #endif |
| 7133 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7134 | /*[clinic input] |
| 7135 | os.plock |
| 7136 | op: int |
| 7137 | / |
| 7138 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7139 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7140 | [clinic start generated code]*/ |
| 7141 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7142 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7143 | os_plock_impl(PyObject *module, int op) |
| 7144 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7145 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7146 | if (plock(op) == -1) |
| 7147 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7148 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7149 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7150 | #endif /* HAVE_PLOCK */ |
| 7151 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7152 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7153 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7154 | /*[clinic input] |
| 7155 | os.setuid |
| 7156 | |
| 7157 | uid: uid_t |
| 7158 | / |
| 7159 | |
| 7160 | Set the current process's user id. |
| 7161 | [clinic start generated code]*/ |
| 7162 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7163 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7164 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7165 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7166 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7167 | if (setuid(uid) < 0) |
| 7168 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7169 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7170 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7171 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7172 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7173 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7174 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7175 | /*[clinic input] |
| 7176 | os.seteuid |
| 7177 | |
| 7178 | euid: uid_t |
| 7179 | / |
| 7180 | |
| 7181 | Set the current process's effective user id. |
| 7182 | [clinic start generated code]*/ |
| 7183 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7184 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7185 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7186 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7187 | { |
| 7188 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7189 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7190 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7191 | } |
| 7192 | #endif /* HAVE_SETEUID */ |
| 7193 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7194 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7195 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7196 | /*[clinic input] |
| 7197 | os.setegid |
| 7198 | |
| 7199 | egid: gid_t |
| 7200 | / |
| 7201 | |
| 7202 | Set the current process's effective group id. |
| 7203 | [clinic start generated code]*/ |
| 7204 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7205 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7206 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7207 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7208 | { |
| 7209 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7210 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7211 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7212 | } |
| 7213 | #endif /* HAVE_SETEGID */ |
| 7214 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7215 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7216 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7217 | /*[clinic input] |
| 7218 | os.setreuid |
| 7219 | |
| 7220 | ruid: uid_t |
| 7221 | euid: uid_t |
| 7222 | / |
| 7223 | |
| 7224 | Set the current process's real and effective user ids. |
| 7225 | [clinic start generated code]*/ |
| 7226 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7227 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7228 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 7229 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7230 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7231 | if (setreuid(ruid, euid) < 0) { |
| 7232 | return posix_error(); |
| 7233 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7234 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7235 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7236 | } |
| 7237 | #endif /* HAVE_SETREUID */ |
| 7238 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7239 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7240 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7241 | /*[clinic input] |
| 7242 | os.setregid |
| 7243 | |
| 7244 | rgid: gid_t |
| 7245 | egid: gid_t |
| 7246 | / |
| 7247 | |
| 7248 | Set the current process's real and effective group ids. |
| 7249 | [clinic start generated code]*/ |
| 7250 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7251 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7252 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 7253 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7254 | { |
| 7255 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7256 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7257 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7258 | } |
| 7259 | #endif /* HAVE_SETREGID */ |
| 7260 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7261 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7262 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7263 | /*[clinic input] |
| 7264 | os.setgid |
| 7265 | gid: gid_t |
| 7266 | / |
| 7267 | |
| 7268 | Set the current process's group id. |
| 7269 | [clinic start generated code]*/ |
| 7270 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7271 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7272 | os_setgid_impl(PyObject *module, gid_t gid) |
| 7273 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7274 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7275 | if (setgid(gid) < 0) |
| 7276 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7277 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7278 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7279 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7280 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7281 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7282 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7283 | /*[clinic input] |
| 7284 | os.setgroups |
| 7285 | |
| 7286 | groups: object |
| 7287 | / |
| 7288 | |
| 7289 | Set the groups of the current process to list. |
| 7290 | [clinic start generated code]*/ |
| 7291 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7292 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7293 | os_setgroups(PyObject *module, PyObject *groups) |
| 7294 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7295 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7296 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7297 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7298 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7299 | if (!PySequence_Check(groups)) { |
| 7300 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 7301 | return NULL; |
| 7302 | } |
| 7303 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7304 | if (len < 0) { |
| 7305 | return NULL; |
| 7306 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7307 | if (len > MAX_GROUPS) { |
| 7308 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7309 | return NULL; |
| 7310 | } |
| 7311 | for(i = 0; i < len; i++) { |
| 7312 | PyObject *elem; |
| 7313 | elem = PySequence_GetItem(groups, i); |
| 7314 | if (!elem) |
| 7315 | return NULL; |
| 7316 | if (!PyLong_Check(elem)) { |
| 7317 | PyErr_SetString(PyExc_TypeError, |
| 7318 | "groups must be integers"); |
| 7319 | Py_DECREF(elem); |
| 7320 | return NULL; |
| 7321 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7322 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7323 | Py_DECREF(elem); |
| 7324 | return NULL; |
| 7325 | } |
| 7326 | } |
| 7327 | Py_DECREF(elem); |
| 7328 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7329 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7330 | if (setgroups(len, grouplist) < 0) |
| 7331 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7332 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7333 | } |
| 7334 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7335 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7336 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7337 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7338 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7339 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7340 | PyObject *result; |
| 7341 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 7342 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7343 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7344 | if (pid == -1) |
| 7345 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7346 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7347 | if (struct_rusage == NULL) { |
| 7348 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7349 | if (m == NULL) |
| 7350 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 7351 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7352 | Py_DECREF(m); |
| 7353 | if (struct_rusage == NULL) |
| 7354 | return NULL; |
| 7355 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7356 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7357 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7358 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 7359 | if (!result) |
| 7360 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7361 | |
| 7362 | #ifndef doubletime |
| 7363 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7364 | #endif |
| 7365 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7366 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7367 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7368 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7369 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7370 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7371 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7372 | SET_INT(result, 2, ru->ru_maxrss); |
| 7373 | SET_INT(result, 3, ru->ru_ixrss); |
| 7374 | SET_INT(result, 4, ru->ru_idrss); |
| 7375 | SET_INT(result, 5, ru->ru_isrss); |
| 7376 | SET_INT(result, 6, ru->ru_minflt); |
| 7377 | SET_INT(result, 7, ru->ru_majflt); |
| 7378 | SET_INT(result, 8, ru->ru_nswap); |
| 7379 | SET_INT(result, 9, ru->ru_inblock); |
| 7380 | SET_INT(result, 10, ru->ru_oublock); |
| 7381 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7382 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7383 | SET_INT(result, 13, ru->ru_nsignals); |
| 7384 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7385 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7386 | #undef SET_INT |
| 7387 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7388 | if (PyErr_Occurred()) { |
| 7389 | Py_DECREF(result); |
| 7390 | return NULL; |
| 7391 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7392 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7393 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7394 | } |
| 7395 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7396 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7397 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7398 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7399 | /*[clinic input] |
| 7400 | os.wait3 |
| 7401 | |
| 7402 | options: int |
| 7403 | Wait for completion of a child process. |
| 7404 | |
| 7405 | Returns a tuple of information about the child process: |
| 7406 | (pid, status, rusage) |
| 7407 | [clinic start generated code]*/ |
| 7408 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7409 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7410 | os_wait3_impl(PyObject *module, int options) |
| 7411 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7412 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7413 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7414 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7415 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7416 | WAIT_TYPE status; |
| 7417 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7418 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7419 | do { |
| 7420 | Py_BEGIN_ALLOW_THREADS |
| 7421 | pid = wait3(&status, options, &ru); |
| 7422 | Py_END_ALLOW_THREADS |
| 7423 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7424 | if (pid < 0) |
| 7425 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7426 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7427 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7428 | } |
| 7429 | #endif /* HAVE_WAIT3 */ |
| 7430 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7431 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7432 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7433 | /*[clinic input] |
| 7434 | |
| 7435 | os.wait4 |
| 7436 | |
| 7437 | pid: pid_t |
| 7438 | options: int |
| 7439 | |
| 7440 | Wait for completion of a specific child process. |
| 7441 | |
| 7442 | Returns a tuple of information about the child process: |
| 7443 | (pid, status, rusage) |
| 7444 | [clinic start generated code]*/ |
| 7445 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7446 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7447 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7448 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7449 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7450 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7451 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7452 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7453 | WAIT_TYPE status; |
| 7454 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7455 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7456 | do { |
| 7457 | Py_BEGIN_ALLOW_THREADS |
| 7458 | res = wait4(pid, &status, options, &ru); |
| 7459 | Py_END_ALLOW_THREADS |
| 7460 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7461 | if (res < 0) |
| 7462 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7463 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7464 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7465 | } |
| 7466 | #endif /* HAVE_WAIT4 */ |
| 7467 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7468 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7469 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7470 | /*[clinic input] |
| 7471 | os.waitid |
| 7472 | |
| 7473 | idtype: idtype_t |
| 7474 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7475 | id: id_t |
| 7476 | The id to wait on. |
| 7477 | options: int |
| 7478 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7479 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7480 | / |
| 7481 | |
| 7482 | Returns the result of waiting for a process or processes. |
| 7483 | |
| 7484 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7485 | no children in a waitable state. |
| 7486 | [clinic start generated code]*/ |
| 7487 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7488 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7489 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7490 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7491 | { |
| 7492 | PyObject *result; |
| 7493 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7494 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7495 | siginfo_t si; |
| 7496 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7497 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7498 | do { |
| 7499 | Py_BEGIN_ALLOW_THREADS |
| 7500 | res = waitid(idtype, id, &si, options); |
| 7501 | Py_END_ALLOW_THREADS |
| 7502 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7503 | if (res < 0) |
| 7504 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7505 | |
| 7506 | if (si.si_pid == 0) |
| 7507 | Py_RETURN_NONE; |
| 7508 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 7509 | result = PyStructSequence_New(WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7510 | if (!result) |
| 7511 | return NULL; |
| 7512 | |
| 7513 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7514 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7515 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7516 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7517 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7518 | if (PyErr_Occurred()) { |
| 7519 | Py_DECREF(result); |
| 7520 | return NULL; |
| 7521 | } |
| 7522 | |
| 7523 | return result; |
| 7524 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7525 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7526 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7527 | |
| 7528 | #if defined(HAVE_WAITPID) |
| 7529 | /*[clinic input] |
| 7530 | os.waitpid |
| 7531 | pid: pid_t |
| 7532 | options: int |
| 7533 | / |
| 7534 | |
| 7535 | Wait for completion of a given child process. |
| 7536 | |
| 7537 | Returns a tuple of information regarding the child process: |
| 7538 | (pid, status) |
| 7539 | |
| 7540 | The options argument is ignored on Windows. |
| 7541 | [clinic start generated code]*/ |
| 7542 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7543 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7544 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7545 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7546 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7547 | pid_t res; |
| 7548 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7549 | WAIT_TYPE status; |
| 7550 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7551 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7552 | do { |
| 7553 | Py_BEGIN_ALLOW_THREADS |
| 7554 | res = waitpid(pid, &status, options); |
| 7555 | Py_END_ALLOW_THREADS |
| 7556 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7557 | if (res < 0) |
| 7558 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7559 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7560 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7561 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7562 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7563 | /* 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] | 7564 | /*[clinic input] |
| 7565 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7566 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7567 | options: int |
| 7568 | / |
| 7569 | |
| 7570 | Wait for completion of a given process. |
| 7571 | |
| 7572 | Returns a tuple of information regarding the process: |
| 7573 | (pid, status << 8) |
| 7574 | |
| 7575 | The options argument is ignored on Windows. |
| 7576 | [clinic start generated code]*/ |
| 7577 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7578 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7579 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7580 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7581 | { |
| 7582 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7583 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7584 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7585 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7586 | do { |
| 7587 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7588 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7589 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7590 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7591 | Py_END_ALLOW_THREADS |
| 7592 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7593 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7594 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7595 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7596 | /* 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] | 7597 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7598 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7599 | #endif |
| 7600 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7601 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7602 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7603 | /*[clinic input] |
| 7604 | os.wait |
| 7605 | |
| 7606 | Wait for completion of a child process. |
| 7607 | |
| 7608 | Returns a tuple of information about the child process: |
| 7609 | (pid, status) |
| 7610 | [clinic start generated code]*/ |
| 7611 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7612 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7613 | os_wait_impl(PyObject *module) |
| 7614 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7615 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7616 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7617 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7618 | WAIT_TYPE status; |
| 7619 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7620 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7621 | do { |
| 7622 | Py_BEGIN_ALLOW_THREADS |
| 7623 | pid = wait(&status); |
| 7624 | Py_END_ALLOW_THREADS |
| 7625 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7626 | if (pid < 0) |
| 7627 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7628 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7629 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7630 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7631 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7632 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7633 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7634 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7635 | /*[clinic input] |
| 7636 | os.readlink |
| 7637 | |
| 7638 | path: path_t |
| 7639 | * |
| 7640 | dir_fd: dir_fd(requires='readlinkat') = None |
| 7641 | |
| 7642 | Return a string representing the path to which the symbolic link points. |
| 7643 | |
| 7644 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7645 | and path should be relative; path will then be relative to that directory. |
| 7646 | |
| 7647 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 7648 | using it will raise a NotImplementedError. |
| 7649 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7650 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7651 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7652 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 7653 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7654 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7655 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 7656 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7657 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7658 | |
| 7659 | Py_BEGIN_ALLOW_THREADS |
| 7660 | #ifdef HAVE_READLINKAT |
| 7661 | if (dir_fd != DEFAULT_DIR_FD) |
| 7662 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 7663 | else |
| 7664 | #endif |
| 7665 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 7666 | Py_END_ALLOW_THREADS |
| 7667 | |
| 7668 | if (length < 0) { |
| 7669 | return path_error(path); |
| 7670 | } |
| 7671 | buffer[length] = '\0'; |
| 7672 | |
| 7673 | if (PyUnicode_Check(path->object)) |
| 7674 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7675 | else |
| 7676 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7677 | #elif defined(MS_WINDOWS) |
| 7678 | DWORD n_bytes_returned; |
| 7679 | DWORD io_result; |
| 7680 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7681 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7682 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
| 7683 | const wchar_t *print_name; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7684 | PyObject *result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7685 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7686 | /* First get a handle to the reparse point */ |
| 7687 | Py_BEGIN_ALLOW_THREADS |
| 7688 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7689 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7690 | 0, |
| 7691 | 0, |
| 7692 | 0, |
| 7693 | OPEN_EXISTING, |
| 7694 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7695 | 0); |
| 7696 | Py_END_ALLOW_THREADS |
| 7697 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7698 | if (reparse_point_handle == INVALID_HANDLE_VALUE) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7699 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7700 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7701 | |
| 7702 | Py_BEGIN_ALLOW_THREADS |
| 7703 | /* New call DeviceIoControl to read the reparse point */ |
| 7704 | io_result = DeviceIoControl( |
| 7705 | reparse_point_handle, |
| 7706 | FSCTL_GET_REPARSE_POINT, |
| 7707 | 0, 0, /* in buffer */ |
| 7708 | target_buffer, sizeof(target_buffer), |
| 7709 | &n_bytes_returned, |
| 7710 | 0 /* we're not using OVERLAPPED_IO */ |
| 7711 | ); |
| 7712 | CloseHandle(reparse_point_handle); |
| 7713 | Py_END_ALLOW_THREADS |
| 7714 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7715 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7716 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7717 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7718 | |
| 7719 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7720 | { |
| 7721 | PyErr_SetString(PyExc_ValueError, |
| 7722 | "not a symbolic link"); |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7723 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7724 | } |
SSE4 | 3c34aad | 2018-02-13 00:10:35 +0700 | [diff] [blame] | 7725 | print_name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7726 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7727 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7728 | result = PyUnicode_FromWideChar(print_name, |
| 7729 | rdb->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t)); |
| 7730 | if (path->narrow) { |
| 7731 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7732 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7733 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7734 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7735 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7736 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7737 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7738 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7739 | |
| 7740 | #if defined(MS_WINDOWS) |
| 7741 | |
| 7742 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7743 | static BOOLEAN (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7744 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7745 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7746 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7747 | { |
| 7748 | HINSTANCE hKernel32; |
| 7749 | /* only recheck */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7750 | if (Py_CreateSymbolicLinkW) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7751 | return 1; |
| 7752 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7753 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7754 | "CreateSymbolicLinkW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7755 | return Py_CreateSymbolicLinkW != NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7756 | } |
| 7757 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7758 | /* Remove the last portion of the path - return 0 on success */ |
| 7759 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7760 | _dirnameW(WCHAR *path) |
| 7761 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7762 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7763 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 7764 | if (length == MAX_PATH) { |
| 7765 | return -1; |
| 7766 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7767 | |
| 7768 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7769 | for(ptr = path + length; ptr != path; ptr--) { |
| 7770 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7771 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7772 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7773 | } |
| 7774 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7775 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7776 | } |
| 7777 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7778 | /* Is this path absolute? */ |
| 7779 | static int |
| 7780 | _is_absW(const WCHAR *path) |
| 7781 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7782 | return path[0] == L'\\' || path[0] == L'/' || |
| 7783 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7784 | } |
| 7785 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7786 | /* join root and rest with a backslash - return 0 on success */ |
| 7787 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7788 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7789 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7790 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7791 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7792 | } |
| 7793 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7794 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 7795 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7796 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7797 | |
| 7798 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 7799 | return -1; |
| 7800 | } |
| 7801 | |
| 7802 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7803 | } |
| 7804 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7805 | /* Return True if the path at src relative to dest is a directory */ |
| 7806 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7807 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7808 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7809 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7810 | WCHAR dest_parent[MAX_PATH]; |
| 7811 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7812 | |
| 7813 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7814 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 7815 | _dirnameW(dest_parent)) { |
| 7816 | return 0; |
| 7817 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7818 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7819 | if (_joinW(src_resolved, dest_parent, src)) { |
| 7820 | return 0; |
| 7821 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7822 | return ( |
| 7823 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7824 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7825 | ); |
| 7826 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7827 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7828 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7829 | |
| 7830 | /*[clinic input] |
| 7831 | os.symlink |
| 7832 | src: path_t |
| 7833 | dst: path_t |
| 7834 | target_is_directory: bool = False |
| 7835 | * |
| 7836 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7837 | |
| 7838 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7839 | |
| 7840 | Create a symbolic link pointing to src named dst. |
| 7841 | |
| 7842 | target_is_directory is required on Windows if the target is to be |
| 7843 | interpreted as a directory. (On Windows, symlink requires |
| 7844 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7845 | target_is_directory is ignored on non-Windows platforms. |
| 7846 | |
| 7847 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7848 | and path should be relative; path will then be relative to that directory. |
| 7849 | dir_fd may not be implemented on your platform. |
| 7850 | If it is unavailable, using it will raise a NotImplementedError. |
| 7851 | |
| 7852 | [clinic start generated code]*/ |
| 7853 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7854 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7855 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7856 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7857 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7858 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7859 | #ifdef MS_WINDOWS |
| 7860 | DWORD result; |
| 7861 | #else |
| 7862 | int result; |
| 7863 | #endif |
| 7864 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7865 | #ifdef MS_WINDOWS |
| 7866 | if (!check_CreateSymbolicLink()) { |
| 7867 | PyErr_SetString(PyExc_NotImplementedError, |
| 7868 | "CreateSymbolicLink functions not found"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7869 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7870 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7871 | if (!win32_can_symlink) { |
| 7872 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7873 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7874 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7875 | #endif |
| 7876 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7877 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7878 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7879 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7880 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7881 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7882 | target_is_directory |= _check_dirW(src->wide, dst->wide); |
| 7883 | result = Py_CreateSymbolicLinkW(dst->wide, src->wide, |
| 7884 | target_is_directory); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7885 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7886 | Py_END_ALLOW_THREADS |
| 7887 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7888 | if (!result) |
| 7889 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7890 | |
| 7891 | #else |
| 7892 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7893 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 7894 | PyErr_SetString(PyExc_ValueError, |
| 7895 | "symlink: src and dst must be the same type"); |
| 7896 | return NULL; |
| 7897 | } |
| 7898 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7899 | Py_BEGIN_ALLOW_THREADS |
| 7900 | #if HAVE_SYMLINKAT |
| 7901 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7902 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7903 | else |
| 7904 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7905 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7906 | Py_END_ALLOW_THREADS |
| 7907 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7908 | if (result) |
| 7909 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7910 | #endif |
| 7911 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7912 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7913 | } |
| 7914 | #endif /* HAVE_SYMLINK */ |
| 7915 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7916 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7917 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7918 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7919 | static PyStructSequence_Field times_result_fields[] = { |
| 7920 | {"user", "user time"}, |
| 7921 | {"system", "system time"}, |
| 7922 | {"children_user", "user time of children"}, |
| 7923 | {"children_system", "system time of children"}, |
| 7924 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7925 | {NULL} |
| 7926 | }; |
| 7927 | |
| 7928 | PyDoc_STRVAR(times_result__doc__, |
| 7929 | "times_result: Result from os.times().\n\n\ |
| 7930 | This object may be accessed either as a tuple of\n\ |
| 7931 | (user, system, children_user, children_system, elapsed),\n\ |
| 7932 | or via the attributes user, system, children_user, children_system,\n\ |
| 7933 | and elapsed.\n\ |
| 7934 | \n\ |
| 7935 | See os.times for more information."); |
| 7936 | |
| 7937 | static PyStructSequence_Desc times_result_desc = { |
| 7938 | "times_result", /* name */ |
| 7939 | times_result__doc__, /* doc */ |
| 7940 | times_result_fields, |
| 7941 | 5 |
| 7942 | }; |
| 7943 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 7944 | static PyTypeObject* TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7945 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7946 | #ifdef MS_WINDOWS |
| 7947 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7948 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7949 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7950 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7951 | |
| 7952 | static PyObject * |
| 7953 | build_times_result(double user, double system, |
| 7954 | double children_user, double children_system, |
| 7955 | double elapsed) |
| 7956 | { |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 7957 | PyObject *value = PyStructSequence_New(TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7958 | if (value == NULL) |
| 7959 | return NULL; |
| 7960 | |
| 7961 | #define SET(i, field) \ |
| 7962 | { \ |
| 7963 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7964 | if (!o) { \ |
| 7965 | Py_DECREF(value); \ |
| 7966 | return NULL; \ |
| 7967 | } \ |
| 7968 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7969 | } \ |
| 7970 | |
| 7971 | SET(0, user); |
| 7972 | SET(1, system); |
| 7973 | SET(2, children_user); |
| 7974 | SET(3, children_system); |
| 7975 | SET(4, elapsed); |
| 7976 | |
| 7977 | #undef SET |
| 7978 | |
| 7979 | return value; |
| 7980 | } |
| 7981 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7982 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7983 | #ifndef MS_WINDOWS |
| 7984 | #define NEED_TICKS_PER_SECOND |
| 7985 | static long ticks_per_second = -1; |
| 7986 | #endif /* MS_WINDOWS */ |
| 7987 | |
| 7988 | /*[clinic input] |
| 7989 | os.times |
| 7990 | |
| 7991 | Return a collection containing process timing information. |
| 7992 | |
| 7993 | The object returned behaves like a named tuple with these fields: |
| 7994 | (utime, stime, cutime, cstime, elapsed_time) |
| 7995 | All fields are floating point numbers. |
| 7996 | [clinic start generated code]*/ |
| 7997 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7998 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7999 | os_times_impl(PyObject *module) |
| 8000 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8001 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8002 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8003 | FILETIME create, exit, kernel, user; |
| 8004 | HANDLE hProc; |
| 8005 | hProc = GetCurrentProcess(); |
| 8006 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 8007 | /* The fields of a FILETIME structure are the hi and lo part |
| 8008 | of a 64-bit value expressed in 100 nanosecond units. |
| 8009 | 1e7 is one second in such units; 1e-7 the inverse. |
| 8010 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 8011 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8012 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8013 | (double)(user.dwHighDateTime*429.4967296 + |
| 8014 | user.dwLowDateTime*1e-7), |
| 8015 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 8016 | kernel.dwLowDateTime*1e-7), |
| 8017 | (double)0, |
| 8018 | (double)0, |
| 8019 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8020 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8021 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8022 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8023 | |
| 8024 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8025 | struct tms t; |
| 8026 | clock_t c; |
| 8027 | errno = 0; |
| 8028 | c = times(&t); |
| 8029 | if (c == (clock_t) -1) |
| 8030 | return posix_error(); |
| 8031 | return build_times_result( |
| 8032 | (double)t.tms_utime / ticks_per_second, |
| 8033 | (double)t.tms_stime / ticks_per_second, |
| 8034 | (double)t.tms_cutime / ticks_per_second, |
| 8035 | (double)t.tms_cstime / ticks_per_second, |
| 8036 | (double)c / ticks_per_second); |
| 8037 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8038 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8039 | #endif /* HAVE_TIMES */ |
| 8040 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8041 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8042 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8043 | /*[clinic input] |
| 8044 | os.getsid |
| 8045 | |
| 8046 | pid: pid_t |
| 8047 | / |
| 8048 | |
| 8049 | Call the system call getsid(pid) and return the result. |
| 8050 | [clinic start generated code]*/ |
| 8051 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8052 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8053 | os_getsid_impl(PyObject *module, pid_t pid) |
| 8054 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8055 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8056 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8057 | sid = getsid(pid); |
| 8058 | if (sid < 0) |
| 8059 | return posix_error(); |
| 8060 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8061 | } |
| 8062 | #endif /* HAVE_GETSID */ |
| 8063 | |
| 8064 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8065 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8066 | /*[clinic input] |
| 8067 | os.setsid |
| 8068 | |
| 8069 | Call the system call setsid(). |
| 8070 | [clinic start generated code]*/ |
| 8071 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8072 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8073 | os_setsid_impl(PyObject *module) |
| 8074 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8075 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8076 | if (setsid() < 0) |
| 8077 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8078 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8079 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8080 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8081 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8082 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8083 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8084 | /*[clinic input] |
| 8085 | os.setpgid |
| 8086 | |
| 8087 | pid: pid_t |
| 8088 | pgrp: pid_t |
| 8089 | / |
| 8090 | |
| 8091 | Call the system call setpgid(pid, pgrp). |
| 8092 | [clinic start generated code]*/ |
| 8093 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8094 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8095 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 8096 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8097 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8098 | if (setpgid(pid, pgrp) < 0) |
| 8099 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8100 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8101 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8102 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8103 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8104 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8105 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8106 | /*[clinic input] |
| 8107 | os.tcgetpgrp |
| 8108 | |
| 8109 | fd: int |
| 8110 | / |
| 8111 | |
| 8112 | Return the process group associated with the terminal specified by fd. |
| 8113 | [clinic start generated code]*/ |
| 8114 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8115 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8116 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8117 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8118 | { |
| 8119 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8120 | if (pgid < 0) |
| 8121 | return posix_error(); |
| 8122 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8123 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8124 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8125 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8126 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8127 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8128 | /*[clinic input] |
| 8129 | os.tcsetpgrp |
| 8130 | |
| 8131 | fd: int |
| 8132 | pgid: pid_t |
| 8133 | / |
| 8134 | |
| 8135 | Set the process group associated with the terminal specified by fd. |
| 8136 | [clinic start generated code]*/ |
| 8137 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8138 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8139 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8140 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8141 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8142 | if (tcsetpgrp(fd, pgid) < 0) |
| 8143 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8144 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8145 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8146 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8147 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8148 | /* Functions acting on file descriptors */ |
| 8149 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8150 | #ifdef O_CLOEXEC |
| 8151 | extern int _Py_open_cloexec_works; |
| 8152 | #endif |
| 8153 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8154 | |
| 8155 | /*[clinic input] |
| 8156 | os.open -> int |
| 8157 | path: path_t |
| 8158 | flags: int |
| 8159 | mode: int = 0o777 |
| 8160 | * |
| 8161 | dir_fd: dir_fd(requires='openat') = None |
| 8162 | |
| 8163 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 8164 | |
| 8165 | Open a file for low level IO. Returns a file descriptor (integer). |
| 8166 | |
| 8167 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8168 | and path should be relative; path will then be relative to that directory. |
| 8169 | dir_fd may not be implemented on your platform. |
| 8170 | If it is unavailable, using it will raise a NotImplementedError. |
| 8171 | [clinic start generated code]*/ |
| 8172 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8173 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8174 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 8175 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8176 | { |
| 8177 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8178 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8179 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8180 | #ifdef O_CLOEXEC |
| 8181 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 8182 | #elif !defined(MS_WINDOWS) |
| 8183 | int *atomic_flag_works = NULL; |
| 8184 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8185 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8186 | #ifdef MS_WINDOWS |
| 8187 | flags |= O_NOINHERIT; |
| 8188 | #elif defined(O_CLOEXEC) |
| 8189 | flags |= O_CLOEXEC; |
| 8190 | #endif |
| 8191 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8192 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8193 | do { |
| 8194 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8195 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8196 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8197 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8198 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8199 | if (dir_fd != DEFAULT_DIR_FD) |
| 8200 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 8201 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8202 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8203 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8204 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8205 | Py_END_ALLOW_THREADS |
| 8206 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8207 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8208 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8209 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8210 | if (!async_err) |
| 8211 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8212 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8213 | } |
| 8214 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8215 | #ifndef MS_WINDOWS |
| 8216 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 8217 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8218 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8219 | } |
| 8220 | #endif |
| 8221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8222 | return fd; |
| 8223 | } |
| 8224 | |
| 8225 | |
| 8226 | /*[clinic input] |
| 8227 | os.close |
| 8228 | |
| 8229 | fd: int |
| 8230 | |
| 8231 | Close a file descriptor. |
| 8232 | [clinic start generated code]*/ |
| 8233 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8234 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8235 | os_close_impl(PyObject *module, int fd) |
| 8236 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8237 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8238 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8239 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 8240 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 8241 | * for more details. |
| 8242 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8243 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8244 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8245 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8246 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8247 | Py_END_ALLOW_THREADS |
| 8248 | if (res < 0) |
| 8249 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8250 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8251 | } |
| 8252 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8253 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8254 | /*[clinic input] |
| 8255 | os.closerange |
| 8256 | |
| 8257 | fd_low: int |
| 8258 | fd_high: int |
| 8259 | / |
| 8260 | |
| 8261 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 8262 | [clinic start generated code]*/ |
| 8263 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8264 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8265 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 8266 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8267 | { |
| 8268 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8269 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8270 | _Py_BEGIN_SUPPRESS_IPH |
Benjamin Peterson | 207116b | 2016-09-08 11:28:06 -0700 | [diff] [blame] | 8271 | for (i = Py_MAX(fd_low, 0); i < fd_high; i++) |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 8272 | close(i); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8273 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8274 | Py_END_ALLOW_THREADS |
| 8275 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8276 | } |
| 8277 | |
| 8278 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8279 | /*[clinic input] |
| 8280 | os.dup -> int |
| 8281 | |
| 8282 | fd: int |
| 8283 | / |
| 8284 | |
| 8285 | Return a duplicate of a file descriptor. |
| 8286 | [clinic start generated code]*/ |
| 8287 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8288 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8289 | os_dup_impl(PyObject *module, int fd) |
| 8290 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8291 | { |
| 8292 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8293 | } |
| 8294 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8295 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8296 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8297 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8298 | fd: int |
| 8299 | fd2: int |
| 8300 | inheritable: bool=True |
| 8301 | |
| 8302 | Duplicate file descriptor. |
| 8303 | [clinic start generated code]*/ |
| 8304 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8305 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8306 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8307 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8308 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8309 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8310 | #if defined(HAVE_DUP3) && \ |
| 8311 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8312 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8313 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8314 | #endif |
| 8315 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8316 | if (fd < 0 || fd2 < 0) { |
| 8317 | posix_error(); |
| 8318 | return -1; |
| 8319 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8320 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8321 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8322 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8323 | * upon close(), and therefore below. |
| 8324 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8325 | #ifdef MS_WINDOWS |
| 8326 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8327 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8328 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8329 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8330 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8331 | if (res < 0) { |
| 8332 | posix_error(); |
| 8333 | return -1; |
| 8334 | } |
| 8335 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8336 | |
| 8337 | /* Character files like console cannot be make non-inheritable */ |
| 8338 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8339 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8340 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8341 | } |
| 8342 | |
| 8343 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8344 | Py_BEGIN_ALLOW_THREADS |
| 8345 | if (!inheritable) |
| 8346 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8347 | else |
| 8348 | res = dup2(fd, fd2); |
| 8349 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8350 | if (res < 0) { |
| 8351 | posix_error(); |
| 8352 | return -1; |
| 8353 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8354 | |
| 8355 | #else |
| 8356 | |
| 8357 | #ifdef HAVE_DUP3 |
| 8358 | if (!inheritable && dup3_works != 0) { |
| 8359 | Py_BEGIN_ALLOW_THREADS |
| 8360 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8361 | Py_END_ALLOW_THREADS |
| 8362 | if (res < 0) { |
| 8363 | if (dup3_works == -1) |
| 8364 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8365 | if (dup3_works) { |
| 8366 | posix_error(); |
| 8367 | return -1; |
| 8368 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8369 | } |
| 8370 | } |
| 8371 | |
| 8372 | if (inheritable || dup3_works == 0) |
| 8373 | { |
| 8374 | #endif |
| 8375 | Py_BEGIN_ALLOW_THREADS |
| 8376 | res = dup2(fd, fd2); |
| 8377 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8378 | if (res < 0) { |
| 8379 | posix_error(); |
| 8380 | return -1; |
| 8381 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8382 | |
| 8383 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8384 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8385 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8386 | } |
| 8387 | #ifdef HAVE_DUP3 |
| 8388 | } |
| 8389 | #endif |
| 8390 | |
| 8391 | #endif |
| 8392 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8393 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8394 | } |
| 8395 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8396 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8397 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8398 | /*[clinic input] |
| 8399 | os.lockf |
| 8400 | |
| 8401 | fd: int |
| 8402 | An open file descriptor. |
| 8403 | command: int |
| 8404 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8405 | length: Py_off_t |
| 8406 | The number of bytes to lock, starting at the current position. |
| 8407 | / |
| 8408 | |
| 8409 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8410 | |
| 8411 | [clinic start generated code]*/ |
| 8412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8413 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8414 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8415 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8416 | { |
| 8417 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8418 | |
| 8419 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8420 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8421 | Py_END_ALLOW_THREADS |
| 8422 | |
| 8423 | if (res < 0) |
| 8424 | return posix_error(); |
| 8425 | |
| 8426 | Py_RETURN_NONE; |
| 8427 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8428 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8429 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8430 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8431 | /*[clinic input] |
| 8432 | os.lseek -> Py_off_t |
| 8433 | |
| 8434 | fd: int |
| 8435 | position: Py_off_t |
| 8436 | how: int |
| 8437 | / |
| 8438 | |
| 8439 | Set the position of a file descriptor. Return the new position. |
| 8440 | |
| 8441 | Return the new cursor position in number of bytes |
| 8442 | relative to the beginning of the file. |
| 8443 | [clinic start generated code]*/ |
| 8444 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8445 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8446 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8447 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8448 | { |
| 8449 | Py_off_t result; |
| 8450 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8451 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8452 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8453 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8454 | case 0: how = SEEK_SET; break; |
| 8455 | case 1: how = SEEK_CUR; break; |
| 8456 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8457 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8458 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8459 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8460 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8461 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8462 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8463 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8464 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8465 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8466 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8467 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8468 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8469 | if (result < 0) |
| 8470 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8471 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8472 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8473 | } |
| 8474 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8475 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8476 | /*[clinic input] |
| 8477 | os.read |
| 8478 | fd: int |
| 8479 | length: Py_ssize_t |
| 8480 | / |
| 8481 | |
| 8482 | Read from a file descriptor. Returns a bytes object. |
| 8483 | [clinic start generated code]*/ |
| 8484 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8485 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8486 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 8487 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8488 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8489 | Py_ssize_t n; |
| 8490 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8491 | |
| 8492 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8493 | errno = EINVAL; |
| 8494 | return posix_error(); |
| 8495 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8496 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 8497 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8498 | |
| 8499 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8500 | if (buffer == NULL) |
| 8501 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8502 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8503 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8504 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8505 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8506 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8507 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8508 | |
| 8509 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8510 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8511 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8512 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8513 | } |
| 8514 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8515 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8516 | || defined(__APPLE__))) \ |
| 8517 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 8518 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 8519 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8520 | 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] | 8521 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8522 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8523 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8524 | *iov = PyMem_New(struct iovec, cnt); |
| 8525 | if (*iov == NULL) { |
| 8526 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8527 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8528 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8529 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8530 | *buf = PyMem_New(Py_buffer, cnt); |
| 8531 | if (*buf == NULL) { |
| 8532 | PyMem_Del(*iov); |
| 8533 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8534 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8535 | } |
| 8536 | |
| 8537 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8538 | PyObject *item = PySequence_GetItem(seq, i); |
| 8539 | if (item == NULL) |
| 8540 | goto fail; |
| 8541 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8542 | Py_DECREF(item); |
| 8543 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8544 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8545 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8546 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8547 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8548 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8549 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8550 | |
| 8551 | fail: |
| 8552 | PyMem_Del(*iov); |
| 8553 | for (j = 0; j < i; j++) { |
| 8554 | PyBuffer_Release(&(*buf)[j]); |
| 8555 | } |
| 8556 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8557 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8558 | } |
| 8559 | |
| 8560 | static void |
| 8561 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8562 | { |
| 8563 | int i; |
| 8564 | PyMem_Del(iov); |
| 8565 | for (i = 0; i < cnt; i++) { |
| 8566 | PyBuffer_Release(&buf[i]); |
| 8567 | } |
| 8568 | PyMem_Del(buf); |
| 8569 | } |
| 8570 | #endif |
| 8571 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8572 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8573 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8574 | /*[clinic input] |
| 8575 | os.readv -> Py_ssize_t |
| 8576 | |
| 8577 | fd: int |
| 8578 | buffers: object |
| 8579 | / |
| 8580 | |
| 8581 | Read from a file descriptor fd into an iterable of buffers. |
| 8582 | |
| 8583 | The buffers should be mutable buffers accepting bytes. |
| 8584 | readv will transfer data into each buffer until it is full |
| 8585 | and then move on to the next buffer in the sequence to hold |
| 8586 | the rest of the data. |
| 8587 | |
| 8588 | readv returns the total number of bytes read, |
| 8589 | which may be less than the total capacity of all the buffers. |
| 8590 | [clinic start generated code]*/ |
| 8591 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8592 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8593 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 8594 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8595 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8596 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8597 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8598 | struct iovec *iov; |
| 8599 | Py_buffer *buf; |
| 8600 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8601 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8602 | PyErr_SetString(PyExc_TypeError, |
| 8603 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8604 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8605 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8606 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8607 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8608 | if (cnt < 0) |
| 8609 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8610 | |
| 8611 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 8612 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8613 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8614 | do { |
| 8615 | Py_BEGIN_ALLOW_THREADS |
| 8616 | n = readv(fd, iov, cnt); |
| 8617 | Py_END_ALLOW_THREADS |
| 8618 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8619 | |
| 8620 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8621 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8622 | if (!async_err) |
| 8623 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8624 | return -1; |
| 8625 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8626 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8627 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8628 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8629 | #endif /* HAVE_READV */ |
| 8630 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8631 | |
| 8632 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8633 | /*[clinic input] |
| 8634 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 8635 | os.pread |
| 8636 | |
| 8637 | fd: int |
| 8638 | length: int |
| 8639 | offset: Py_off_t |
| 8640 | / |
| 8641 | |
| 8642 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 8643 | |
| 8644 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 8645 | the beginning of the file. The file offset remains unchanged. |
| 8646 | [clinic start generated code]*/ |
| 8647 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8648 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8649 | os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset) |
| 8650 | /*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8651 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8652 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8653 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8654 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8655 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8656 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8657 | errno = EINVAL; |
| 8658 | return posix_error(); |
| 8659 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8660 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8661 | if (buffer == NULL) |
| 8662 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8663 | |
| 8664 | do { |
| 8665 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8666 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8667 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8668 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8669 | Py_END_ALLOW_THREADS |
| 8670 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8671 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8672 | if (n < 0) { |
| 8673 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8674 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8675 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8676 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8677 | _PyBytes_Resize(&buffer, n); |
| 8678 | return buffer; |
| 8679 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8680 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8681 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 8682 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 8683 | /*[clinic input] |
| 8684 | os.preadv -> Py_ssize_t |
| 8685 | |
| 8686 | fd: int |
| 8687 | buffers: object |
| 8688 | offset: Py_off_t |
| 8689 | flags: int = 0 |
| 8690 | / |
| 8691 | |
| 8692 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 8693 | |
| 8694 | Combines the functionality of readv() and pread(). As readv(), it will |
| 8695 | transfer data into each buffer until it is full and then move on to the next |
| 8696 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 8697 | specifies the file offset at which the input operation is to be performed. It |
| 8698 | will return the total number of bytes read (which can be less than the total |
| 8699 | capacity of all the objects). |
| 8700 | |
| 8701 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 8702 | |
| 8703 | - RWF_HIPRI |
| 8704 | - RWF_NOWAIT |
| 8705 | |
| 8706 | Using non-zero flags requires Linux 4.6 or newer. |
| 8707 | [clinic start generated code]*/ |
| 8708 | |
| 8709 | static Py_ssize_t |
| 8710 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 8711 | int flags) |
| 8712 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 8713 | { |
| 8714 | Py_ssize_t cnt, n; |
| 8715 | int async_err = 0; |
| 8716 | struct iovec *iov; |
| 8717 | Py_buffer *buf; |
| 8718 | |
| 8719 | if (!PySequence_Check(buffers)) { |
| 8720 | PyErr_SetString(PyExc_TypeError, |
| 8721 | "preadv2() arg 2 must be a sequence"); |
| 8722 | return -1; |
| 8723 | } |
| 8724 | |
| 8725 | cnt = PySequence_Size(buffers); |
| 8726 | if (cnt < 0) { |
| 8727 | return -1; |
| 8728 | } |
| 8729 | |
| 8730 | #ifndef HAVE_PREADV2 |
| 8731 | if(flags != 0) { |
| 8732 | argument_unavailable_error("preadv2", "flags"); |
| 8733 | return -1; |
| 8734 | } |
| 8735 | #endif |
| 8736 | |
| 8737 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 8738 | return -1; |
| 8739 | } |
| 8740 | #ifdef HAVE_PREADV2 |
| 8741 | do { |
| 8742 | Py_BEGIN_ALLOW_THREADS |
| 8743 | _Py_BEGIN_SUPPRESS_IPH |
| 8744 | n = preadv2(fd, iov, cnt, offset, flags); |
| 8745 | _Py_END_SUPPRESS_IPH |
| 8746 | Py_END_ALLOW_THREADS |
| 8747 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8748 | #else |
| 8749 | do { |
| 8750 | Py_BEGIN_ALLOW_THREADS |
| 8751 | _Py_BEGIN_SUPPRESS_IPH |
| 8752 | n = preadv(fd, iov, cnt, offset); |
| 8753 | _Py_END_SUPPRESS_IPH |
| 8754 | Py_END_ALLOW_THREADS |
| 8755 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8756 | #endif |
| 8757 | |
| 8758 | iov_cleanup(iov, buf, cnt); |
| 8759 | if (n < 0) { |
| 8760 | if (!async_err) { |
| 8761 | posix_error(); |
| 8762 | } |
| 8763 | return -1; |
| 8764 | } |
| 8765 | |
| 8766 | return n; |
| 8767 | } |
| 8768 | #endif /* HAVE_PREADV */ |
| 8769 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8770 | |
| 8771 | /*[clinic input] |
| 8772 | os.write -> Py_ssize_t |
| 8773 | |
| 8774 | fd: int |
| 8775 | data: Py_buffer |
| 8776 | / |
| 8777 | |
| 8778 | Write a bytes object to a file descriptor. |
| 8779 | [clinic start generated code]*/ |
| 8780 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8781 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8782 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 8783 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8784 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8785 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8786 | } |
| 8787 | |
| 8788 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8789 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8790 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8791 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8792 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8793 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8794 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8795 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8796 | static PyObject * |
| 8797 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8798 | { |
| 8799 | int in, out; |
| 8800 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8801 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8802 | off_t offset; |
| 8803 | |
| 8804 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8805 | #ifndef __APPLE__ |
| 8806 | Py_ssize_t len; |
| 8807 | #endif |
| 8808 | PyObject *headers = NULL, *trailers = NULL; |
| 8809 | Py_buffer *hbuf, *tbuf; |
| 8810 | off_t sbytes; |
| 8811 | struct sf_hdtr sf; |
| 8812 | int flags = 0; |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8813 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8814 | static char *keywords[] = {"out", "in", |
| 8815 | "offset", "count", |
| 8816 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8817 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8818 | sf.headers = NULL; |
| 8819 | sf.trailers = NULL; |
| 8820 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8821 | #ifdef __APPLE__ |
| 8822 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8823 | 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] | 8824 | #else |
| 8825 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8826 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8827 | #endif |
| 8828 | &headers, &trailers, &flags)) |
| 8829 | return NULL; |
| 8830 | if (headers != NULL) { |
| 8831 | if (!PySequence_Check(headers)) { |
| 8832 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8833 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8834 | return NULL; |
| 8835 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8836 | Py_ssize_t i = PySequence_Size(headers); |
| 8837 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8838 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8839 | if (i > INT_MAX) { |
| 8840 | PyErr_SetString(PyExc_OverflowError, |
| 8841 | "sendfile() header is too large"); |
| 8842 | return NULL; |
| 8843 | } |
| 8844 | if (i > 0) { |
| 8845 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8846 | if (iov_setup(&(sf.headers), &hbuf, |
| 8847 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8848 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8849 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8850 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 8851 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 8852 | # define OFF_T_MAX 0x7fffffffffffffff |
| 8853 | if (sbytes >= OFF_T_MAX - blen) { |
| 8854 | PyErr_SetString(PyExc_OverflowError, |
| 8855 | "sendfile() header is too large"); |
| 8856 | return NULL; |
| 8857 | } |
| 8858 | sbytes += blen; |
| 8859 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8860 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8861 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8862 | } |
| 8863 | } |
| 8864 | if (trailers != NULL) { |
| 8865 | if (!PySequence_Check(trailers)) { |
| 8866 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8867 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8868 | return NULL; |
| 8869 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8870 | Py_ssize_t i = PySequence_Size(trailers); |
| 8871 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8872 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8873 | if (i > INT_MAX) { |
| 8874 | PyErr_SetString(PyExc_OverflowError, |
| 8875 | "sendfile() trailer is too large"); |
| 8876 | return NULL; |
| 8877 | } |
| 8878 | if (i > 0) { |
| 8879 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8880 | if (iov_setup(&(sf.trailers), &tbuf, |
| 8881 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8882 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8883 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8884 | } |
| 8885 | } |
| 8886 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8887 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8888 | do { |
| 8889 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8890 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8891 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8892 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8893 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8894 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8895 | Py_END_ALLOW_THREADS |
| 8896 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8897 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8898 | |
| 8899 | if (sf.headers != NULL) |
| 8900 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8901 | if (sf.trailers != NULL) |
| 8902 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8903 | |
| 8904 | if (ret < 0) { |
| 8905 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8906 | if (sbytes != 0) { |
| 8907 | // some data has been sent |
| 8908 | goto done; |
| 8909 | } |
| 8910 | else { |
| 8911 | // no data has been sent; upper application is supposed |
| 8912 | // to retry on EAGAIN or EBUSY |
| 8913 | return posix_error(); |
| 8914 | } |
| 8915 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8916 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8917 | } |
| 8918 | goto done; |
| 8919 | |
| 8920 | done: |
| 8921 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8922 | return Py_BuildValue("l", sbytes); |
| 8923 | #else |
| 8924 | return Py_BuildValue("L", sbytes); |
| 8925 | #endif |
| 8926 | |
| 8927 | #else |
| 8928 | Py_ssize_t count; |
| 8929 | PyObject *offobj; |
| 8930 | static char *keywords[] = {"out", "in", |
| 8931 | "offset", "count", NULL}; |
| 8932 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8933 | keywords, &out, &in, &offobj, &count)) |
| 8934 | return NULL; |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 8935 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8936 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8937 | do { |
| 8938 | Py_BEGIN_ALLOW_THREADS |
| 8939 | ret = sendfile(out, in, NULL, count); |
| 8940 | Py_END_ALLOW_THREADS |
| 8941 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8942 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8943 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8944 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8945 | } |
| 8946 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8947 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8948 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8949 | |
| 8950 | do { |
| 8951 | Py_BEGIN_ALLOW_THREADS |
| 8952 | ret = sendfile(out, in, &offset, count); |
| 8953 | Py_END_ALLOW_THREADS |
| 8954 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8955 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8956 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8957 | return Py_BuildValue("n", ret); |
| 8958 | #endif |
| 8959 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8960 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8961 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8962 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 8963 | #if defined(__APPLE__) |
| 8964 | /*[clinic input] |
| 8965 | os._fcopyfile |
| 8966 | |
| 8967 | infd: int |
| 8968 | outfd: int |
| 8969 | flags: int |
| 8970 | / |
| 8971 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 8972 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 8973 | [clinic start generated code]*/ |
| 8974 | |
| 8975 | static PyObject * |
| 8976 | os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags) |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 8977 | /*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 8978 | { |
| 8979 | int ret; |
| 8980 | |
| 8981 | Py_BEGIN_ALLOW_THREADS |
| 8982 | ret = fcopyfile(infd, outfd, NULL, flags); |
| 8983 | Py_END_ALLOW_THREADS |
| 8984 | if (ret < 0) |
| 8985 | return posix_error(); |
| 8986 | Py_RETURN_NONE; |
| 8987 | } |
| 8988 | #endif |
| 8989 | |
| 8990 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8991 | /*[clinic input] |
| 8992 | os.fstat |
| 8993 | |
| 8994 | fd : int |
| 8995 | |
| 8996 | Perform a stat system call on the given file descriptor. |
| 8997 | |
| 8998 | Like stat(), but for an open file descriptor. |
| 8999 | Equivalent to os.stat(fd). |
| 9000 | [clinic start generated code]*/ |
| 9001 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9002 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9003 | os_fstat_impl(PyObject *module, int fd) |
| 9004 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9005 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9006 | STRUCT_STAT st; |
| 9007 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9008 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9009 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9010 | do { |
| 9011 | Py_BEGIN_ALLOW_THREADS |
| 9012 | res = FSTAT(fd, &st); |
| 9013 | Py_END_ALLOW_THREADS |
| 9014 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9015 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9016 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9017 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9018 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9019 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9020 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9021 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9022 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9023 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9024 | } |
| 9025 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9026 | |
| 9027 | /*[clinic input] |
| 9028 | os.isatty -> bool |
| 9029 | fd: int |
| 9030 | / |
| 9031 | |
| 9032 | Return True if the fd is connected to a terminal. |
| 9033 | |
| 9034 | Return True if the file descriptor is an open file descriptor |
| 9035 | connected to the slave end of a terminal. |
| 9036 | [clinic start generated code]*/ |
| 9037 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9038 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9039 | os_isatty_impl(PyObject *module, int fd) |
| 9040 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9041 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9042 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9043 | _Py_BEGIN_SUPPRESS_IPH |
| 9044 | return_value = isatty(fd); |
| 9045 | _Py_END_SUPPRESS_IPH |
| 9046 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9047 | } |
| 9048 | |
| 9049 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9050 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9051 | /*[clinic input] |
| 9052 | os.pipe |
| 9053 | |
| 9054 | Create a pipe. |
| 9055 | |
| 9056 | Returns a tuple of two file descriptors: |
| 9057 | (read_fd, write_fd) |
| 9058 | [clinic start generated code]*/ |
| 9059 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9060 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9061 | os_pipe_impl(PyObject *module) |
| 9062 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9063 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9064 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9065 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9066 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9067 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9068 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9069 | #else |
| 9070 | int res; |
| 9071 | #endif |
| 9072 | |
| 9073 | #ifdef MS_WINDOWS |
| 9074 | attr.nLength = sizeof(attr); |
| 9075 | attr.lpSecurityDescriptor = NULL; |
| 9076 | attr.bInheritHandle = FALSE; |
| 9077 | |
| 9078 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9079 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9080 | ok = CreatePipe(&read, &write, &attr, 0); |
| 9081 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9082 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 9083 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9084 | if (fds[0] == -1 || fds[1] == -1) { |
| 9085 | CloseHandle(read); |
| 9086 | CloseHandle(write); |
| 9087 | ok = 0; |
| 9088 | } |
| 9089 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9090 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9091 | Py_END_ALLOW_THREADS |
| 9092 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9093 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9094 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9095 | #else |
| 9096 | |
| 9097 | #ifdef HAVE_PIPE2 |
| 9098 | Py_BEGIN_ALLOW_THREADS |
| 9099 | res = pipe2(fds, O_CLOEXEC); |
| 9100 | Py_END_ALLOW_THREADS |
| 9101 | |
| 9102 | if (res != 0 && errno == ENOSYS) |
| 9103 | { |
| 9104 | #endif |
| 9105 | Py_BEGIN_ALLOW_THREADS |
| 9106 | res = pipe(fds); |
| 9107 | Py_END_ALLOW_THREADS |
| 9108 | |
| 9109 | if (res == 0) { |
| 9110 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 9111 | close(fds[0]); |
| 9112 | close(fds[1]); |
| 9113 | return NULL; |
| 9114 | } |
| 9115 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 9116 | close(fds[0]); |
| 9117 | close(fds[1]); |
| 9118 | return NULL; |
| 9119 | } |
| 9120 | } |
| 9121 | #ifdef HAVE_PIPE2 |
| 9122 | } |
| 9123 | #endif |
| 9124 | |
| 9125 | if (res != 0) |
| 9126 | return PyErr_SetFromErrno(PyExc_OSError); |
| 9127 | #endif /* !MS_WINDOWS */ |
| 9128 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9129 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9130 | #endif /* HAVE_PIPE */ |
| 9131 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9132 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9133 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9134 | /*[clinic input] |
| 9135 | os.pipe2 |
| 9136 | |
| 9137 | flags: int |
| 9138 | / |
| 9139 | |
| 9140 | Create a pipe with flags set atomically. |
| 9141 | |
| 9142 | Returns a tuple of two file descriptors: |
| 9143 | (read_fd, write_fd) |
| 9144 | |
| 9145 | flags can be constructed by ORing together one or more of these values: |
| 9146 | O_NONBLOCK, O_CLOEXEC. |
| 9147 | [clinic start generated code]*/ |
| 9148 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9149 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9150 | os_pipe2_impl(PyObject *module, int flags) |
| 9151 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9152 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9153 | int fds[2]; |
| 9154 | int res; |
| 9155 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9156 | res = pipe2(fds, flags); |
| 9157 | if (res != 0) |
| 9158 | return posix_error(); |
| 9159 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 9160 | } |
| 9161 | #endif /* HAVE_PIPE2 */ |
| 9162 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9163 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9164 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9165 | /*[clinic input] |
| 9166 | os.writev -> Py_ssize_t |
| 9167 | fd: int |
| 9168 | buffers: object |
| 9169 | / |
| 9170 | |
| 9171 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 9172 | |
| 9173 | Returns the total number of bytes written. |
| 9174 | buffers must be a sequence of bytes-like objects. |
| 9175 | [clinic start generated code]*/ |
| 9176 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9177 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9178 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 9179 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9180 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9181 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9182 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9183 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9184 | struct iovec *iov; |
| 9185 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9186 | |
| 9187 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9188 | PyErr_SetString(PyExc_TypeError, |
| 9189 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9190 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9191 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9192 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9193 | if (cnt < 0) |
| 9194 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9195 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9196 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9197 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9198 | } |
| 9199 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9200 | do { |
| 9201 | Py_BEGIN_ALLOW_THREADS |
| 9202 | result = writev(fd, iov, cnt); |
| 9203 | Py_END_ALLOW_THREADS |
| 9204 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9205 | |
| 9206 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9207 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9208 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9209 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9210 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9211 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9212 | #endif /* HAVE_WRITEV */ |
| 9213 | |
| 9214 | |
| 9215 | #ifdef HAVE_PWRITE |
| 9216 | /*[clinic input] |
| 9217 | os.pwrite -> Py_ssize_t |
| 9218 | |
| 9219 | fd: int |
| 9220 | buffer: Py_buffer |
| 9221 | offset: Py_off_t |
| 9222 | / |
| 9223 | |
| 9224 | Write bytes to a file descriptor starting at a particular offset. |
| 9225 | |
| 9226 | Write buffer to fd, starting at offset bytes from the beginning of |
| 9227 | the file. Returns the number of bytes writte. Does not change the |
| 9228 | current file offset. |
| 9229 | [clinic start generated code]*/ |
| 9230 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9231 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9232 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 9233 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9234 | { |
| 9235 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9236 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9237 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9238 | do { |
| 9239 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9240 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9241 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9242 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9243 | Py_END_ALLOW_THREADS |
| 9244 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9245 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9246 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9247 | posix_error(); |
| 9248 | return size; |
| 9249 | } |
| 9250 | #endif /* HAVE_PWRITE */ |
| 9251 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9252 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9253 | /*[clinic input] |
| 9254 | os.pwritev -> Py_ssize_t |
| 9255 | |
| 9256 | fd: int |
| 9257 | buffers: object |
| 9258 | offset: Py_off_t |
| 9259 | flags: int = 0 |
| 9260 | / |
| 9261 | |
| 9262 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 9263 | |
| 9264 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 9265 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 9266 | buffer is written before proceeding to second, and so on. The operating system may |
| 9267 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 9268 | This function writes the contents of each object to the file descriptor and returns |
| 9269 | the total number of bytes written. |
| 9270 | |
| 9271 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9272 | |
| 9273 | - RWF_DSYNC |
| 9274 | - RWF_SYNC |
| 9275 | |
| 9276 | Using non-zero flags requires Linux 4.7 or newer. |
| 9277 | [clinic start generated code]*/ |
| 9278 | |
| 9279 | static Py_ssize_t |
| 9280 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9281 | int flags) |
| 9282 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/ |
| 9283 | { |
| 9284 | Py_ssize_t cnt; |
| 9285 | Py_ssize_t result; |
| 9286 | int async_err = 0; |
| 9287 | struct iovec *iov; |
| 9288 | Py_buffer *buf; |
| 9289 | |
| 9290 | if (!PySequence_Check(buffers)) { |
| 9291 | PyErr_SetString(PyExc_TypeError, |
| 9292 | "pwritev() arg 2 must be a sequence"); |
| 9293 | return -1; |
| 9294 | } |
| 9295 | |
| 9296 | cnt = PySequence_Size(buffers); |
| 9297 | if (cnt < 0) { |
| 9298 | return -1; |
| 9299 | } |
| 9300 | |
| 9301 | #ifndef HAVE_PWRITEV2 |
| 9302 | if(flags != 0) { |
| 9303 | argument_unavailable_error("pwritev2", "flags"); |
| 9304 | return -1; |
| 9305 | } |
| 9306 | #endif |
| 9307 | |
| 9308 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9309 | return -1; |
| 9310 | } |
| 9311 | #ifdef HAVE_PWRITEV2 |
| 9312 | do { |
| 9313 | Py_BEGIN_ALLOW_THREADS |
| 9314 | _Py_BEGIN_SUPPRESS_IPH |
| 9315 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9316 | _Py_END_SUPPRESS_IPH |
| 9317 | Py_END_ALLOW_THREADS |
| 9318 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9319 | #else |
| 9320 | do { |
| 9321 | Py_BEGIN_ALLOW_THREADS |
| 9322 | _Py_BEGIN_SUPPRESS_IPH |
| 9323 | result = pwritev(fd, iov, cnt, offset); |
| 9324 | _Py_END_SUPPRESS_IPH |
| 9325 | Py_END_ALLOW_THREADS |
| 9326 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9327 | #endif |
| 9328 | |
| 9329 | iov_cleanup(iov, buf, cnt); |
| 9330 | if (result < 0) { |
| 9331 | if (!async_err) { |
| 9332 | posix_error(); |
| 9333 | } |
| 9334 | return -1; |
| 9335 | } |
| 9336 | |
| 9337 | return result; |
| 9338 | } |
| 9339 | #endif /* HAVE_PWRITEV */ |
| 9340 | |
| 9341 | |
| 9342 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9343 | |
| 9344 | #ifdef HAVE_MKFIFO |
| 9345 | /*[clinic input] |
| 9346 | os.mkfifo |
| 9347 | |
| 9348 | path: path_t |
| 9349 | mode: int=0o666 |
| 9350 | * |
| 9351 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9352 | |
| 9353 | Create a "fifo" (a POSIX named pipe). |
| 9354 | |
| 9355 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9356 | and path should be relative; path will then be relative to that directory. |
| 9357 | dir_fd may not be implemented on your platform. |
| 9358 | If it is unavailable, using it will raise a NotImplementedError. |
| 9359 | [clinic start generated code]*/ |
| 9360 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9361 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9362 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 9363 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9364 | { |
| 9365 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9366 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9367 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9368 | do { |
| 9369 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9370 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9371 | if (dir_fd != DEFAULT_DIR_FD) |
| 9372 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 9373 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9374 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9375 | result = mkfifo(path->narrow, mode); |
| 9376 | Py_END_ALLOW_THREADS |
| 9377 | } while (result != 0 && errno == EINTR && |
| 9378 | !(async_err = PyErr_CheckSignals())); |
| 9379 | if (result != 0) |
| 9380 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9381 | |
| 9382 | Py_RETURN_NONE; |
| 9383 | } |
| 9384 | #endif /* HAVE_MKFIFO */ |
| 9385 | |
| 9386 | |
| 9387 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 9388 | /*[clinic input] |
| 9389 | os.mknod |
| 9390 | |
| 9391 | path: path_t |
| 9392 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9393 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9394 | * |
| 9395 | dir_fd: dir_fd(requires='mknodat')=None |
| 9396 | |
| 9397 | Create a node in the file system. |
| 9398 | |
| 9399 | Create a node in the file system (file, device special file or named pipe) |
| 9400 | at path. mode specifies both the permissions to use and the |
| 9401 | type of node to be created, being combined (bitwise OR) with one of |
| 9402 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 9403 | device defines the newly created device special file (probably using |
| 9404 | os.makedev()). Otherwise device is ignored. |
| 9405 | |
| 9406 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9407 | and path should be relative; path will then be relative to that directory. |
| 9408 | dir_fd may not be implemented on your platform. |
| 9409 | If it is unavailable, using it will raise a NotImplementedError. |
| 9410 | [clinic start generated code]*/ |
| 9411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9412 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9413 | 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] | 9414 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9415 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9416 | { |
| 9417 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9418 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9419 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9420 | do { |
| 9421 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9422 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9423 | if (dir_fd != DEFAULT_DIR_FD) |
| 9424 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 9425 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9426 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9427 | result = mknod(path->narrow, mode, device); |
| 9428 | Py_END_ALLOW_THREADS |
| 9429 | } while (result != 0 && errno == EINTR && |
| 9430 | !(async_err = PyErr_CheckSignals())); |
| 9431 | if (result != 0) |
| 9432 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9433 | |
| 9434 | Py_RETURN_NONE; |
| 9435 | } |
| 9436 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 9437 | |
| 9438 | |
| 9439 | #ifdef HAVE_DEVICE_MACROS |
| 9440 | /*[clinic input] |
| 9441 | os.major -> unsigned_int |
| 9442 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9443 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9444 | / |
| 9445 | |
| 9446 | Extracts a device major number from a raw device number. |
| 9447 | [clinic start generated code]*/ |
| 9448 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9449 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9450 | os_major_impl(PyObject *module, dev_t device) |
| 9451 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9452 | { |
| 9453 | return major(device); |
| 9454 | } |
| 9455 | |
| 9456 | |
| 9457 | /*[clinic input] |
| 9458 | os.minor -> unsigned_int |
| 9459 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9460 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9461 | / |
| 9462 | |
| 9463 | Extracts a device minor number from a raw device number. |
| 9464 | [clinic start generated code]*/ |
| 9465 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9466 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9467 | os_minor_impl(PyObject *module, dev_t device) |
| 9468 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9469 | { |
| 9470 | return minor(device); |
| 9471 | } |
| 9472 | |
| 9473 | |
| 9474 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9475 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9476 | |
| 9477 | major: int |
| 9478 | minor: int |
| 9479 | / |
| 9480 | |
| 9481 | Composes a raw device number from the major and minor device numbers. |
| 9482 | [clinic start generated code]*/ |
| 9483 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9484 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9485 | os_makedev_impl(PyObject *module, int major, int minor) |
| 9486 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9487 | { |
| 9488 | return makedev(major, minor); |
| 9489 | } |
| 9490 | #endif /* HAVE_DEVICE_MACROS */ |
| 9491 | |
| 9492 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9493 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9494 | /*[clinic input] |
| 9495 | os.ftruncate |
| 9496 | |
| 9497 | fd: int |
| 9498 | length: Py_off_t |
| 9499 | / |
| 9500 | |
| 9501 | Truncate a file, specified by file descriptor, to a specific length. |
| 9502 | [clinic start generated code]*/ |
| 9503 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9504 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9505 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 9506 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9507 | { |
| 9508 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9509 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9510 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9511 | do { |
| 9512 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9513 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9514 | #ifdef MS_WINDOWS |
| 9515 | result = _chsize_s(fd, length); |
| 9516 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9517 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9518 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9519 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9520 | Py_END_ALLOW_THREADS |
| 9521 | } while (result != 0 && errno == EINTR && |
| 9522 | !(async_err = PyErr_CheckSignals())); |
| 9523 | if (result != 0) |
| 9524 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9525 | Py_RETURN_NONE; |
| 9526 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9527 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9528 | |
| 9529 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9530 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9531 | /*[clinic input] |
| 9532 | os.truncate |
| 9533 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 9534 | length: Py_off_t |
| 9535 | |
| 9536 | Truncate a file, specified by path, to a specific length. |
| 9537 | |
| 9538 | On some platforms, path may also be specified as an open file descriptor. |
| 9539 | If this functionality is unavailable, using it raises an exception. |
| 9540 | [clinic start generated code]*/ |
| 9541 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9542 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9543 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 9544 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9545 | { |
| 9546 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9547 | #ifdef MS_WINDOWS |
| 9548 | int fd; |
| 9549 | #endif |
| 9550 | |
| 9551 | if (path->fd != -1) |
| 9552 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9553 | |
| 9554 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9555 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9556 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 9557 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 9558 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9559 | result = -1; |
| 9560 | else { |
| 9561 | result = _chsize_s(fd, length); |
| 9562 | close(fd); |
| 9563 | if (result < 0) |
| 9564 | errno = result; |
| 9565 | } |
| 9566 | #else |
| 9567 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9568 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9569 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9570 | Py_END_ALLOW_THREADS |
| 9571 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 9572 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9573 | |
| 9574 | Py_RETURN_NONE; |
| 9575 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9576 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9577 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9578 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9579 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 9580 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 9581 | defined, which is the case in Python on AIX. AIX bug report: |
| 9582 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 9583 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 9584 | # define POSIX_FADVISE_AIX_BUG |
| 9585 | #endif |
| 9586 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9587 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9588 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9589 | /*[clinic input] |
| 9590 | os.posix_fallocate |
| 9591 | |
| 9592 | fd: int |
| 9593 | offset: Py_off_t |
| 9594 | length: Py_off_t |
| 9595 | / |
| 9596 | |
| 9597 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 9598 | |
| 9599 | Ensure that the file specified by fd encompasses a range of bytes |
| 9600 | starting at offset bytes from the beginning and continuing for length bytes. |
| 9601 | [clinic start generated code]*/ |
| 9602 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9603 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9604 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9605 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9606 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9607 | { |
| 9608 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9609 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9610 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9611 | do { |
| 9612 | Py_BEGIN_ALLOW_THREADS |
| 9613 | result = posix_fallocate(fd, offset, length); |
| 9614 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9615 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9616 | |
| 9617 | if (result == 0) |
| 9618 | Py_RETURN_NONE; |
| 9619 | |
| 9620 | if (async_err) |
| 9621 | return NULL; |
| 9622 | |
| 9623 | errno = result; |
| 9624 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9625 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9626 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9627 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9628 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9629 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9630 | /*[clinic input] |
| 9631 | os.posix_fadvise |
| 9632 | |
| 9633 | fd: int |
| 9634 | offset: Py_off_t |
| 9635 | length: Py_off_t |
| 9636 | advice: int |
| 9637 | / |
| 9638 | |
| 9639 | Announce an intention to access data in a specific pattern. |
| 9640 | |
| 9641 | Announce an intention to access data in a specific pattern, thus allowing |
| 9642 | the kernel to make optimizations. |
| 9643 | The advice applies to the region of the file specified by fd starting at |
| 9644 | offset and continuing for length bytes. |
| 9645 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 9646 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 9647 | POSIX_FADV_DONTNEED. |
| 9648 | [clinic start generated code]*/ |
| 9649 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9650 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9651 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9652 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9653 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9654 | { |
| 9655 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9656 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9657 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9658 | do { |
| 9659 | Py_BEGIN_ALLOW_THREADS |
| 9660 | result = posix_fadvise(fd, offset, length, advice); |
| 9661 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9662 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9663 | |
| 9664 | if (result == 0) |
| 9665 | Py_RETURN_NONE; |
| 9666 | |
| 9667 | if (async_err) |
| 9668 | return NULL; |
| 9669 | |
| 9670 | errno = result; |
| 9671 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9672 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9673 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9674 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9675 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9676 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 9677 | /* Save putenv() parameters as values here, so we can collect them when they |
| 9678 | * get re-set with another call for the same key. */ |
| 9679 | static PyObject *posix_putenv_garbage; |
| 9680 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9681 | static void |
| 9682 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9683 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9684 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 9685 | * this will cause previous value to be collected. This has to |
| 9686 | * happen after the real putenv() call because the old value |
| 9687 | * was still accessible until then. */ |
| 9688 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 9689 | /* really not much we can do; just leak */ |
| 9690 | PyErr_Clear(); |
| 9691 | else |
| 9692 | Py_DECREF(value); |
| 9693 | } |
| 9694 | |
| 9695 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 9696 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9697 | /*[clinic input] |
| 9698 | os.putenv |
| 9699 | |
| 9700 | name: unicode |
| 9701 | value: unicode |
| 9702 | / |
| 9703 | |
| 9704 | Change or add an environment variable. |
| 9705 | [clinic start generated code]*/ |
| 9706 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9707 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9708 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 9709 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9710 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 9711 | const wchar_t *env; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9712 | Py_ssize_t size; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9713 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9714 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 9715 | defining hidden environment variables. */ |
| 9716 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 9717 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 9718 | { |
| 9719 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 9720 | return NULL; |
| 9721 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9722 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 9723 | if (unicode == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9724 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9725 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9726 | |
| 9727 | env = PyUnicode_AsUnicodeAndSize(unicode, &size); |
| 9728 | if (env == NULL) |
| 9729 | goto error; |
| 9730 | if (size > _MAX_ENV) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9731 | PyErr_Format(PyExc_ValueError, |
| 9732 | "the environment variable is longer than %u characters", |
| 9733 | _MAX_ENV); |
| 9734 | goto error; |
| 9735 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9736 | if (wcslen(env) != (size_t)size) { |
| 9737 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9738 | goto error; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9739 | } |
| 9740 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9741 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9742 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9743 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9744 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9745 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9746 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9747 | Py_RETURN_NONE; |
| 9748 | |
| 9749 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9750 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9751 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9752 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9753 | #else /* MS_WINDOWS */ |
| 9754 | /*[clinic input] |
| 9755 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9756 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9757 | name: FSConverter |
| 9758 | value: FSConverter |
| 9759 | / |
| 9760 | |
| 9761 | Change or add an environment variable. |
| 9762 | [clinic start generated code]*/ |
| 9763 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9764 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9765 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 9766 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9767 | { |
| 9768 | PyObject *bytes = NULL; |
| 9769 | char *env; |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9770 | const char *name_string = PyBytes_AS_STRING(name); |
| 9771 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9772 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9773 | if (strchr(name_string, '=') != NULL) { |
| 9774 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 9775 | return NULL; |
| 9776 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9777 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 9778 | if (bytes == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9779 | return NULL; |
| 9780 | } |
| 9781 | |
| 9782 | env = PyBytes_AS_STRING(bytes); |
| 9783 | if (putenv(env)) { |
| 9784 | Py_DECREF(bytes); |
| 9785 | return posix_error(); |
| 9786 | } |
| 9787 | |
| 9788 | posix_putenv_garbage_setitem(name, bytes); |
| 9789 | Py_RETURN_NONE; |
| 9790 | } |
| 9791 | #endif /* MS_WINDOWS */ |
| 9792 | #endif /* HAVE_PUTENV */ |
| 9793 | |
| 9794 | |
| 9795 | #ifdef HAVE_UNSETENV |
| 9796 | /*[clinic input] |
| 9797 | os.unsetenv |
| 9798 | name: FSConverter |
| 9799 | / |
| 9800 | |
| 9801 | Delete an environment variable. |
| 9802 | [clinic start generated code]*/ |
| 9803 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9804 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9805 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 9806 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9807 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9808 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9809 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9810 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9811 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9812 | #ifdef HAVE_BROKEN_UNSETENV |
| 9813 | unsetenv(PyBytes_AS_STRING(name)); |
| 9814 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9815 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9816 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9817 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9818 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9819 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9820 | /* Remove the key from posix_putenv_garbage; |
| 9821 | * this will cause it to be collected. This has to |
| 9822 | * happen after the real unsetenv() call because the |
| 9823 | * old value was still accessible until then. |
| 9824 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9825 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9826 | /* really not much we can do; just leak */ |
| 9827 | PyErr_Clear(); |
| 9828 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9829 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9830 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9831 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9832 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9833 | |
| 9834 | /*[clinic input] |
| 9835 | os.strerror |
| 9836 | |
| 9837 | code: int |
| 9838 | / |
| 9839 | |
| 9840 | Translate an error code to a message string. |
| 9841 | [clinic start generated code]*/ |
| 9842 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9843 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9844 | os_strerror_impl(PyObject *module, int code) |
| 9845 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9846 | { |
| 9847 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9848 | if (message == NULL) { |
| 9849 | PyErr_SetString(PyExc_ValueError, |
| 9850 | "strerror() argument out of range"); |
| 9851 | return NULL; |
| 9852 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 9853 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9854 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9855 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9856 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9857 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9858 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9859 | /*[clinic input] |
| 9860 | os.WCOREDUMP -> bool |
| 9861 | |
| 9862 | status: int |
| 9863 | / |
| 9864 | |
| 9865 | Return True if the process returning status was dumped to a core file. |
| 9866 | [clinic start generated code]*/ |
| 9867 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9868 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9869 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 9870 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9871 | { |
| 9872 | WAIT_TYPE wait_status; |
| 9873 | WAIT_STATUS_INT(wait_status) = status; |
| 9874 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9875 | } |
| 9876 | #endif /* WCOREDUMP */ |
| 9877 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9878 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9879 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9880 | /*[clinic input] |
| 9881 | os.WIFCONTINUED -> bool |
| 9882 | |
| 9883 | status: int |
| 9884 | |
| 9885 | Return True if a particular process was continued from a job control stop. |
| 9886 | |
| 9887 | Return True if the process returning status was continued from a |
| 9888 | job control stop. |
| 9889 | [clinic start generated code]*/ |
| 9890 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9891 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9892 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 9893 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9894 | { |
| 9895 | WAIT_TYPE wait_status; |
| 9896 | WAIT_STATUS_INT(wait_status) = status; |
| 9897 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9898 | } |
| 9899 | #endif /* WIFCONTINUED */ |
| 9900 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9901 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9902 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9903 | /*[clinic input] |
| 9904 | os.WIFSTOPPED -> bool |
| 9905 | |
| 9906 | status: int |
| 9907 | |
| 9908 | Return True if the process returning status was stopped. |
| 9909 | [clinic start generated code]*/ |
| 9910 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9911 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9912 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 9913 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9914 | { |
| 9915 | WAIT_TYPE wait_status; |
| 9916 | WAIT_STATUS_INT(wait_status) = status; |
| 9917 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9918 | } |
| 9919 | #endif /* WIFSTOPPED */ |
| 9920 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9921 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9922 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9923 | /*[clinic input] |
| 9924 | os.WIFSIGNALED -> bool |
| 9925 | |
| 9926 | status: int |
| 9927 | |
| 9928 | Return True if the process returning status was terminated by a signal. |
| 9929 | [clinic start generated code]*/ |
| 9930 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9931 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9932 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 9933 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9934 | { |
| 9935 | WAIT_TYPE wait_status; |
| 9936 | WAIT_STATUS_INT(wait_status) = status; |
| 9937 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9938 | } |
| 9939 | #endif /* WIFSIGNALED */ |
| 9940 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9941 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9942 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9943 | /*[clinic input] |
| 9944 | os.WIFEXITED -> bool |
| 9945 | |
| 9946 | status: int |
| 9947 | |
| 9948 | Return True if the process returning status exited via the exit() system call. |
| 9949 | [clinic start generated code]*/ |
| 9950 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9951 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9952 | os_WIFEXITED_impl(PyObject *module, int status) |
| 9953 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9954 | { |
| 9955 | WAIT_TYPE wait_status; |
| 9956 | WAIT_STATUS_INT(wait_status) = status; |
| 9957 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9958 | } |
| 9959 | #endif /* WIFEXITED */ |
| 9960 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9961 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9962 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9963 | /*[clinic input] |
| 9964 | os.WEXITSTATUS -> int |
| 9965 | |
| 9966 | status: int |
| 9967 | |
| 9968 | Return the process return code from status. |
| 9969 | [clinic start generated code]*/ |
| 9970 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9971 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9972 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 9973 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9974 | { |
| 9975 | WAIT_TYPE wait_status; |
| 9976 | WAIT_STATUS_INT(wait_status) = status; |
| 9977 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9978 | } |
| 9979 | #endif /* WEXITSTATUS */ |
| 9980 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9981 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9982 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9983 | /*[clinic input] |
| 9984 | os.WTERMSIG -> int |
| 9985 | |
| 9986 | status: int |
| 9987 | |
| 9988 | Return the signal that terminated the process that provided the status value. |
| 9989 | [clinic start generated code]*/ |
| 9990 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9991 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9992 | os_WTERMSIG_impl(PyObject *module, int status) |
| 9993 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9994 | { |
| 9995 | WAIT_TYPE wait_status; |
| 9996 | WAIT_STATUS_INT(wait_status) = status; |
| 9997 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9998 | } |
| 9999 | #endif /* WTERMSIG */ |
| 10000 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10001 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10002 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10003 | /*[clinic input] |
| 10004 | os.WSTOPSIG -> int |
| 10005 | |
| 10006 | status: int |
| 10007 | |
| 10008 | Return the signal that stopped the process that provided the status value. |
| 10009 | [clinic start generated code]*/ |
| 10010 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10011 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10012 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 10013 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10014 | { |
| 10015 | WAIT_TYPE wait_status; |
| 10016 | WAIT_STATUS_INT(wait_status) = status; |
| 10017 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10018 | } |
| 10019 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10020 | #endif /* HAVE_SYS_WAIT_H */ |
| 10021 | |
| 10022 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10023 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 10024 | #ifdef _SCO_DS |
| 10025 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 10026 | needed definitions in sys/statvfs.h */ |
| 10027 | #define _SVID3 |
| 10028 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10029 | #include <sys/statvfs.h> |
| 10030 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10031 | static PyObject* |
| 10032 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 10033 | PyObject *v = PyStructSequence_New(StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10034 | if (v == NULL) |
| 10035 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10036 | |
| 10037 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10038 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10039 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10040 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 10041 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 10042 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 10043 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 10044 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 10045 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 10046 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10047 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10048 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10049 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10050 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10051 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10052 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10053 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10054 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10055 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10056 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10057 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10058 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10059 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10060 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10061 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10062 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10063 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10064 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10065 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10066 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 10067 | * (issue #32390). */ |
| 10068 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 10069 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 10070 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 10071 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10072 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 10073 | if (PyErr_Occurred()) { |
| 10074 | Py_DECREF(v); |
| 10075 | return NULL; |
| 10076 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10077 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10078 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10079 | } |
| 10080 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10081 | |
| 10082 | /*[clinic input] |
| 10083 | os.fstatvfs |
| 10084 | fd: int |
| 10085 | / |
| 10086 | |
| 10087 | Perform an fstatvfs system call on the given fd. |
| 10088 | |
| 10089 | Equivalent to statvfs(fd). |
| 10090 | [clinic start generated code]*/ |
| 10091 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10092 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10093 | os_fstatvfs_impl(PyObject *module, int fd) |
| 10094 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10095 | { |
| 10096 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10097 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10098 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10099 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10100 | do { |
| 10101 | Py_BEGIN_ALLOW_THREADS |
| 10102 | result = fstatvfs(fd, &st); |
| 10103 | Py_END_ALLOW_THREADS |
| 10104 | } while (result != 0 && errno == EINTR && |
| 10105 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10106 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10107 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10108 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10109 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10110 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10111 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10112 | |
| 10113 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10114 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10115 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10116 | /*[clinic input] |
| 10117 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10118 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10119 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 10120 | |
| 10121 | Perform a statvfs system call on the given path. |
| 10122 | |
| 10123 | path may always be specified as a string. |
| 10124 | On some platforms, path may also be specified as an open file descriptor. |
| 10125 | If this functionality is unavailable, using it raises an exception. |
| 10126 | [clinic start generated code]*/ |
| 10127 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10128 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10129 | os_statvfs_impl(PyObject *module, path_t *path) |
| 10130 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10131 | { |
| 10132 | int result; |
| 10133 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10134 | |
| 10135 | Py_BEGIN_ALLOW_THREADS |
| 10136 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10137 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10138 | #ifdef __APPLE__ |
| 10139 | /* handle weak-linking on Mac OS X 10.3 */ |
| 10140 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10141 | fd_specified("statvfs", path->fd); |
| 10142 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10143 | } |
| 10144 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10145 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10146 | } |
| 10147 | else |
| 10148 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10149 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10150 | Py_END_ALLOW_THREADS |
| 10151 | |
| 10152 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10153 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10154 | } |
| 10155 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10156 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10157 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10158 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 10159 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10160 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10161 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10162 | /*[clinic input] |
| 10163 | os._getdiskusage |
| 10164 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10165 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10166 | |
| 10167 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 10168 | [clinic start generated code]*/ |
| 10169 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10170 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10171 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 10172 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10173 | { |
| 10174 | BOOL retval; |
| 10175 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10176 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10177 | |
| 10178 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10179 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10180 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10181 | if (retval == 0) { |
| 10182 | if (GetLastError() == ERROR_DIRECTORY) { |
| 10183 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10184 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10185 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 10186 | if (dir_path == NULL) { |
| 10187 | return PyErr_NoMemory(); |
| 10188 | } |
| 10189 | |
| 10190 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 10191 | |
| 10192 | if (_dirnameW(dir_path) != -1) { |
| 10193 | Py_BEGIN_ALLOW_THREADS |
| 10194 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 10195 | Py_END_ALLOW_THREADS |
| 10196 | } |
| 10197 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 10198 | err = GetLastError(); |
| 10199 | PyMem_Free(dir_path); |
| 10200 | if (retval) { |
| 10201 | goto success; |
| 10202 | } |
| 10203 | } |
| 10204 | return PyErr_SetFromWindowsErr(err); |
| 10205 | } |
| 10206 | |
| 10207 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10208 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 10209 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10210 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10211 | |
| 10212 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10213 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 10214 | * It maps strings representing configuration variable names to |
| 10215 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10216 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10217 | * rarely-used constants. There are three separate tables that use |
| 10218 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10219 | * |
| 10220 | * This code is always included, even if none of the interfaces that |
| 10221 | * need it are included. The #if hackery needed to avoid it would be |
| 10222 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10223 | */ |
| 10224 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10225 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10226 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10227 | }; |
| 10228 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10229 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10230 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10231 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10232 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10233 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10234 | int value = _PyLong_AsInt(arg); |
| 10235 | if (value == -1 && PyErr_Occurred()) |
| 10236 | return 0; |
| 10237 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10238 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10239 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10240 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10241 | /* look up the value in the table using a binary search */ |
| 10242 | size_t lo = 0; |
| 10243 | size_t mid; |
| 10244 | size_t hi = tablesize; |
| 10245 | int cmp; |
| 10246 | const char *confname; |
| 10247 | if (!PyUnicode_Check(arg)) { |
| 10248 | PyErr_SetString(PyExc_TypeError, |
| 10249 | "configuration names must be strings or integers"); |
| 10250 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10251 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10252 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10253 | if (confname == NULL) |
| 10254 | return 0; |
| 10255 | while (lo < hi) { |
| 10256 | mid = (lo + hi) / 2; |
| 10257 | cmp = strcmp(confname, table[mid].name); |
| 10258 | if (cmp < 0) |
| 10259 | hi = mid; |
| 10260 | else if (cmp > 0) |
| 10261 | lo = mid + 1; |
| 10262 | else { |
| 10263 | *valuep = table[mid].value; |
| 10264 | return 1; |
| 10265 | } |
| 10266 | } |
| 10267 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 10268 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10269 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10270 | } |
| 10271 | |
| 10272 | |
| 10273 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 10274 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10275 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10276 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10277 | #endif |
| 10278 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10279 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10280 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10281 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10282 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10283 | #endif |
| 10284 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10285 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10286 | #endif |
| 10287 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10288 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10289 | #endif |
| 10290 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10291 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10292 | #endif |
| 10293 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10294 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10295 | #endif |
| 10296 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10297 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10298 | #endif |
| 10299 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10300 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10301 | #endif |
| 10302 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10303 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10304 | #endif |
| 10305 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10306 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10307 | #endif |
| 10308 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10309 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10310 | #endif |
| 10311 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10312 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10313 | #endif |
| 10314 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10315 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10316 | #endif |
| 10317 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10318 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10319 | #endif |
| 10320 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10321 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10322 | #endif |
| 10323 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10324 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10325 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10326 | #ifdef _PC_ACL_ENABLED |
| 10327 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10328 | #endif |
| 10329 | #ifdef _PC_MIN_HOLE_SIZE |
| 10330 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10331 | #endif |
| 10332 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10333 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10334 | #endif |
| 10335 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10336 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10337 | #endif |
| 10338 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10339 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10340 | #endif |
| 10341 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10342 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10343 | #endif |
| 10344 | #ifdef _PC_REC_XFER_ALIGN |
| 10345 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10346 | #endif |
| 10347 | #ifdef _PC_SYMLINK_MAX |
| 10348 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 10349 | #endif |
| 10350 | #ifdef _PC_XATTR_ENABLED |
| 10351 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 10352 | #endif |
| 10353 | #ifdef _PC_XATTR_EXISTS |
| 10354 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 10355 | #endif |
| 10356 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 10357 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 10358 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10359 | }; |
| 10360 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10361 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10362 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10363 | { |
| 10364 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 10365 | sizeof(posix_constants_pathconf) |
| 10366 | / sizeof(struct constdef)); |
| 10367 | } |
| 10368 | #endif |
| 10369 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10370 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10371 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10372 | /*[clinic input] |
| 10373 | os.fpathconf -> long |
| 10374 | |
| 10375 | fd: int |
| 10376 | name: path_confname |
| 10377 | / |
| 10378 | |
| 10379 | Return the configuration limit name for the file descriptor fd. |
| 10380 | |
| 10381 | If there is no limit, return -1. |
| 10382 | [clinic start generated code]*/ |
| 10383 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10384 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10385 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 10386 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10387 | { |
| 10388 | long limit; |
| 10389 | |
| 10390 | errno = 0; |
| 10391 | limit = fpathconf(fd, name); |
| 10392 | if (limit == -1 && errno != 0) |
| 10393 | posix_error(); |
| 10394 | |
| 10395 | return limit; |
| 10396 | } |
| 10397 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10398 | |
| 10399 | |
| 10400 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10401 | /*[clinic input] |
| 10402 | os.pathconf -> long |
| 10403 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 10404 | name: path_confname |
| 10405 | |
| 10406 | Return the configuration limit name for the file or directory path. |
| 10407 | |
| 10408 | If there is no limit, return -1. |
| 10409 | On some platforms, path may also be specified as an open file descriptor. |
| 10410 | If this functionality is unavailable, using it raises an exception. |
| 10411 | [clinic start generated code]*/ |
| 10412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10413 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10414 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 10415 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10416 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10417 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10418 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10419 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10420 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10421 | if (path->fd != -1) |
| 10422 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10423 | else |
| 10424 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10425 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10426 | if (limit == -1 && errno != 0) { |
| 10427 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 10428 | /* could be a path or name problem */ |
| 10429 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10430 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10431 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10432 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10433 | |
| 10434 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10435 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10436 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10437 | |
| 10438 | #ifdef HAVE_CONFSTR |
| 10439 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10440 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10441 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10442 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10443 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10444 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10445 | #endif |
| 10446 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10447 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10448 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10449 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10450 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10451 | #endif |
| 10452 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10453 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10454 | #endif |
| 10455 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10456 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10457 | #endif |
| 10458 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10459 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10460 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10461 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10462 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10463 | #endif |
| 10464 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10465 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10466 | #endif |
| 10467 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10468 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10469 | #endif |
| 10470 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10471 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10472 | #endif |
| 10473 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10474 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10475 | #endif |
| 10476 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10477 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10478 | #endif |
| 10479 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10480 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10481 | #endif |
| 10482 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10483 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10484 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10485 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10486 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10487 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10488 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10489 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10490 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10491 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10492 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10493 | #endif |
| 10494 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10495 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10496 | #endif |
| 10497 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10498 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10499 | #endif |
| 10500 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10501 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10502 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10503 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10504 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10505 | #endif |
| 10506 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10507 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10508 | #endif |
| 10509 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10510 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10511 | #endif |
| 10512 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10513 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10514 | #endif |
| 10515 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10516 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10517 | #endif |
| 10518 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10519 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10520 | #endif |
| 10521 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10522 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10523 | #endif |
| 10524 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10525 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10526 | #endif |
| 10527 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10528 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10529 | #endif |
| 10530 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10531 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10532 | #endif |
| 10533 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10534 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10535 | #endif |
| 10536 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10537 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10538 | #endif |
| 10539 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10540 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10541 | #endif |
| 10542 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10543 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10544 | #endif |
| 10545 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10546 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10547 | #endif |
| 10548 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10549 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10550 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10551 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10552 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10553 | #endif |
| 10554 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10555 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10556 | #endif |
| 10557 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10558 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10559 | #endif |
| 10560 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10561 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10562 | #endif |
| 10563 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10564 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10565 | #endif |
| 10566 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10567 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10568 | #endif |
| 10569 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10570 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10571 | #endif |
| 10572 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10573 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10574 | #endif |
| 10575 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10576 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10577 | #endif |
| 10578 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10579 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10580 | #endif |
| 10581 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10582 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10583 | #endif |
| 10584 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10585 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10586 | #endif |
| 10587 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10588 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10589 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10590 | }; |
| 10591 | |
| 10592 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10593 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10594 | { |
| 10595 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 10596 | sizeof(posix_constants_confstr) |
| 10597 | / sizeof(struct constdef)); |
| 10598 | } |
| 10599 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10600 | |
| 10601 | /*[clinic input] |
| 10602 | os.confstr |
| 10603 | |
| 10604 | name: confstr_confname |
| 10605 | / |
| 10606 | |
| 10607 | Return a string-valued system configuration variable. |
| 10608 | [clinic start generated code]*/ |
| 10609 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10610 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10611 | os_confstr_impl(PyObject *module, int name) |
| 10612 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10613 | { |
| 10614 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10615 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10616 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10617 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10618 | errno = 0; |
| 10619 | len = confstr(name, buffer, sizeof(buffer)); |
| 10620 | if (len == 0) { |
| 10621 | if (errno) { |
| 10622 | posix_error(); |
| 10623 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10624 | } |
| 10625 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10626 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10627 | } |
| 10628 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10629 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10630 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 10631 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10632 | char *buf = PyMem_Malloc(len); |
| 10633 | if (buf == NULL) |
| 10634 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 10635 | len2 = confstr(name, buf, len); |
| 10636 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 10637 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10638 | PyMem_Free(buf); |
| 10639 | } |
| 10640 | else |
| 10641 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10642 | return result; |
| 10643 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10644 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10645 | |
| 10646 | |
| 10647 | #ifdef HAVE_SYSCONF |
| 10648 | static struct constdef posix_constants_sysconf[] = { |
| 10649 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10650 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10651 | #endif |
| 10652 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10653 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10654 | #endif |
| 10655 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10656 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10657 | #endif |
| 10658 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10659 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10660 | #endif |
| 10661 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10662 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10663 | #endif |
| 10664 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10665 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10666 | #endif |
| 10667 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10668 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10669 | #endif |
| 10670 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10671 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10672 | #endif |
| 10673 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10674 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10675 | #endif |
| 10676 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10677 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10678 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10679 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10680 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10681 | #endif |
| 10682 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10683 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10684 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10685 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10686 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10687 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10688 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10689 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10690 | #endif |
| 10691 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10692 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10693 | #endif |
| 10694 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10695 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10696 | #endif |
| 10697 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10698 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10699 | #endif |
| 10700 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10701 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10702 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10703 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10704 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10705 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10706 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10707 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10708 | #endif |
| 10709 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10710 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10711 | #endif |
| 10712 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10713 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10714 | #endif |
| 10715 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10716 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10717 | #endif |
| 10718 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10719 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10720 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10721 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10722 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10723 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10724 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10725 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10726 | #endif |
| 10727 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10728 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10729 | #endif |
| 10730 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10731 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10732 | #endif |
| 10733 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10734 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10735 | #endif |
| 10736 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10737 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10738 | #endif |
| 10739 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10740 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10741 | #endif |
| 10742 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10743 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10744 | #endif |
| 10745 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10746 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10747 | #endif |
| 10748 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10749 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10750 | #endif |
| 10751 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10752 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10753 | #endif |
| 10754 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10755 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10756 | #endif |
| 10757 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10758 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10759 | #endif |
| 10760 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10761 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10762 | #endif |
| 10763 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10764 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10765 | #endif |
| 10766 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10767 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10768 | #endif |
| 10769 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10770 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10771 | #endif |
| 10772 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10773 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10774 | #endif |
| 10775 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10776 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10777 | #endif |
| 10778 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10779 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10780 | #endif |
| 10781 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10782 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10783 | #endif |
| 10784 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10785 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10786 | #endif |
| 10787 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10788 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10789 | #endif |
| 10790 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10791 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10792 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10793 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10794 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10795 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10796 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10797 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10798 | #endif |
| 10799 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10800 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10801 | #endif |
| 10802 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10803 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10804 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10805 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10806 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10807 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10808 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10809 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10810 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10811 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10812 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10813 | #endif |
| 10814 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10815 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
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_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10818 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10819 | #endif |
| 10820 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10821 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10822 | #endif |
| 10823 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10824 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10825 | #endif |
| 10826 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10827 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10828 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10829 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10830 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10831 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10832 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10833 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10834 | #endif |
| 10835 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10836 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10837 | #endif |
| 10838 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10839 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10840 | #endif |
| 10841 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10842 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10843 | #endif |
| 10844 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10845 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10846 | #endif |
| 10847 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10848 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10849 | #endif |
| 10850 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10851 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10852 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10853 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10854 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10855 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10856 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10857 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10858 | #endif |
| 10859 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10860 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10861 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10862 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10863 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10864 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10865 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10866 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10867 | #endif |
| 10868 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10869 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10870 | #endif |
| 10871 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10872 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10873 | #endif |
| 10874 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10875 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10876 | #endif |
| 10877 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10878 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10879 | #endif |
| 10880 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10881 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10882 | #endif |
| 10883 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10884 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10885 | #endif |
| 10886 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10887 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10888 | #endif |
| 10889 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10890 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10891 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10892 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10893 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10894 | #endif |
| 10895 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10896 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10897 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10898 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10899 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10900 | #endif |
| 10901 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10902 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10903 | #endif |
| 10904 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10905 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10906 | #endif |
| 10907 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10908 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10909 | #endif |
| 10910 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10911 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10912 | #endif |
| 10913 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10914 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10915 | #endif |
| 10916 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10917 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10918 | #endif |
| 10919 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10920 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10921 | #endif |
| 10922 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10923 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10924 | #endif |
| 10925 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10926 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10927 | #endif |
| 10928 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10929 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10930 | #endif |
| 10931 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10932 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10933 | #endif |
| 10934 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10935 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10936 | #endif |
| 10937 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10938 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10939 | #endif |
| 10940 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10941 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10942 | #endif |
| 10943 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10944 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10945 | #endif |
| 10946 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10947 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10948 | #endif |
| 10949 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10950 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10951 | #endif |
| 10952 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10953 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10954 | #endif |
| 10955 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10956 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10957 | #endif |
| 10958 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10959 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10960 | #endif |
| 10961 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10962 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10963 | #endif |
| 10964 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10965 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10966 | #endif |
| 10967 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10968 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10969 | #endif |
| 10970 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10971 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10972 | #endif |
| 10973 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10974 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10975 | #endif |
| 10976 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10977 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10978 | #endif |
| 10979 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10980 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10981 | #endif |
| 10982 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10983 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10984 | #endif |
| 10985 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10986 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10987 | #endif |
| 10988 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10989 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10990 | #endif |
| 10991 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10992 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10993 | #endif |
| 10994 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10995 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10996 | #endif |
| 10997 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10998 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10999 | #endif |
| 11000 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11001 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11002 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11003 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11004 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11005 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11006 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11007 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11008 | #endif |
| 11009 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11010 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11011 | #endif |
| 11012 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11013 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11014 | #endif |
| 11015 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11016 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11017 | #endif |
| 11018 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11019 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11020 | #endif |
| 11021 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11022 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11023 | #endif |
| 11024 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11025 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11026 | #endif |
| 11027 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11028 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11029 | #endif |
| 11030 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11031 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11032 | #endif |
| 11033 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11034 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11035 | #endif |
| 11036 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11037 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11038 | #endif |
| 11039 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11040 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11041 | #endif |
| 11042 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11043 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11044 | #endif |
| 11045 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11046 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11047 | #endif |
| 11048 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11049 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11050 | #endif |
| 11051 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11052 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11053 | #endif |
| 11054 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11055 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11056 | #endif |
| 11057 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11058 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11059 | #endif |
| 11060 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11061 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11062 | #endif |
| 11063 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11064 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11065 | #endif |
| 11066 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11067 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11068 | #endif |
| 11069 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11070 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11071 | #endif |
| 11072 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11073 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11074 | #endif |
| 11075 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11076 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11077 | #endif |
| 11078 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11079 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11080 | #endif |
| 11081 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11082 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11083 | #endif |
| 11084 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11085 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11086 | #endif |
| 11087 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11088 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11089 | #endif |
| 11090 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11091 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11092 | #endif |
| 11093 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11094 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11095 | #endif |
| 11096 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11097 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11098 | #endif |
| 11099 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11100 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11101 | #endif |
| 11102 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11103 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11104 | #endif |
| 11105 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11106 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11107 | #endif |
| 11108 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11109 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11110 | #endif |
| 11111 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11112 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11113 | #endif |
| 11114 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11115 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11116 | #endif |
| 11117 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11118 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11119 | #endif |
| 11120 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11121 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11122 | #endif |
| 11123 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11124 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11125 | #endif |
| 11126 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11127 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11128 | #endif |
| 11129 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11130 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11131 | #endif |
| 11132 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11133 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11134 | #endif |
| 11135 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11136 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11137 | #endif |
| 11138 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11139 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11140 | #endif |
| 11141 | }; |
| 11142 | |
| 11143 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11144 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11145 | { |
| 11146 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 11147 | sizeof(posix_constants_sysconf) |
| 11148 | / sizeof(struct constdef)); |
| 11149 | } |
| 11150 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11151 | |
| 11152 | /*[clinic input] |
| 11153 | os.sysconf -> long |
| 11154 | name: sysconf_confname |
| 11155 | / |
| 11156 | |
| 11157 | Return an integer-valued system configuration variable. |
| 11158 | [clinic start generated code]*/ |
| 11159 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11160 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11161 | os_sysconf_impl(PyObject *module, int name) |
| 11162 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11163 | { |
| 11164 | long value; |
| 11165 | |
| 11166 | errno = 0; |
| 11167 | value = sysconf(name); |
| 11168 | if (value == -1 && errno != 0) |
| 11169 | posix_error(); |
| 11170 | return value; |
| 11171 | } |
| 11172 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11173 | |
| 11174 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11175 | /* 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] | 11176 | * 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] | 11177 | * the exported dictionaries that are used to publish information about the |
| 11178 | * names available on the host platform. |
| 11179 | * |
| 11180 | * Sorting the table at runtime ensures that the table is properly ordered |
| 11181 | * when used, even for platforms we're not able to test on. It also makes |
| 11182 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11183 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11184 | |
| 11185 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11186 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11187 | { |
| 11188 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11189 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11190 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11191 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11192 | |
| 11193 | return strcmp(c1->name, c2->name); |
| 11194 | } |
| 11195 | |
| 11196 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11197 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11198 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11199 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11200 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11201 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11202 | |
| 11203 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 11204 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11205 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11206 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11207 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11208 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11209 | PyObject *o = PyLong_FromLong(table[i].value); |
| 11210 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 11211 | Py_XDECREF(o); |
| 11212 | Py_DECREF(d); |
| 11213 | return -1; |
| 11214 | } |
| 11215 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11216 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11217 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11218 | } |
| 11219 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11220 | /* Return -1 on failure, 0 on success. */ |
| 11221 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11222 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11223 | { |
| 11224 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11225 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11226 | sizeof(posix_constants_pathconf) |
| 11227 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11228 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11229 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11230 | #endif |
| 11231 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11232 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11233 | sizeof(posix_constants_confstr) |
| 11234 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11235 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11236 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11237 | #endif |
| 11238 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11239 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11240 | sizeof(posix_constants_sysconf) |
| 11241 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11242 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11243 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11244 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11245 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11246 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11247 | |
| 11248 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11249 | /*[clinic input] |
| 11250 | os.abort |
| 11251 | |
| 11252 | Abort the interpreter immediately. |
| 11253 | |
| 11254 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 11255 | on the hosting operating system. This function never returns. |
| 11256 | [clinic start generated code]*/ |
| 11257 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11258 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11259 | os_abort_impl(PyObject *module) |
| 11260 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11261 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11262 | abort(); |
| 11263 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11264 | #ifndef __clang__ |
| 11265 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 11266 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 11267 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11268 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 11269 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11270 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11271 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11272 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11273 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11274 | /* Grab ShellExecute dynamically from shell32 */ |
| 11275 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11276 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 11277 | LPCWSTR, INT); |
| 11278 | static int |
| 11279 | check_ShellExecute() |
| 11280 | { |
| 11281 | HINSTANCE hShell32; |
| 11282 | |
| 11283 | /* only recheck */ |
| 11284 | if (-1 == has_ShellExecute) { |
| 11285 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11286 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 11287 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 11288 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 11289 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11290 | hShell32 = LoadLibraryW(L"SHELL32"); |
| 11291 | Py_END_ALLOW_THREADS |
| 11292 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11293 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 11294 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11295 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11296 | } else { |
| 11297 | has_ShellExecute = 0; |
| 11298 | } |
| 11299 | } |
| 11300 | return has_ShellExecute; |
| 11301 | } |
| 11302 | |
| 11303 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11304 | /*[clinic input] |
| 11305 | os.startfile |
| 11306 | filepath: path_t |
| 11307 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11308 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11309 | startfile(filepath [, operation]) |
| 11310 | |
| 11311 | Start a file with its associated application. |
| 11312 | |
| 11313 | When "operation" is not specified or "open", this acts like |
| 11314 | double-clicking the file in Explorer, or giving the file name as an |
| 11315 | argument to the DOS "start" command: the file is opened with whatever |
| 11316 | application (if any) its extension is associated. |
| 11317 | When another "operation" is given, it specifies what should be done with |
| 11318 | the file. A typical operation is "print". |
| 11319 | |
| 11320 | startfile returns as soon as the associated application is launched. |
| 11321 | There is no option to wait for the application to close, and no way |
| 11322 | to retrieve the application's exit status. |
| 11323 | |
| 11324 | The filepath is relative to the current directory. If you want to use |
| 11325 | an absolute path, make sure the first character is not a slash ("/"); |
| 11326 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 11327 | [clinic start generated code]*/ |
| 11328 | |
| 11329 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11330 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 11331 | const Py_UNICODE *operation) |
| 11332 | /*[clinic end generated code: output=66dc311c94d50797 input=63950bf2986380d0]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11333 | { |
| 11334 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11335 | |
| 11336 | if(!check_ShellExecute()) { |
| 11337 | /* If the OS doesn't have ShellExecute, return a |
| 11338 | NotImplementedError. */ |
| 11339 | return PyErr_Format(PyExc_NotImplementedError, |
| 11340 | "startfile not available on this platform"); |
| 11341 | } |
| 11342 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11343 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11344 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11345 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11346 | Py_END_ALLOW_THREADS |
| 11347 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11348 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11349 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11350 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11351 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11352 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11353 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11354 | #endif /* MS_WINDOWS */ |
| 11355 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11356 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11357 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11358 | /*[clinic input] |
| 11359 | os.getloadavg |
| 11360 | |
| 11361 | Return average recent system load information. |
| 11362 | |
| 11363 | Return the number of processes in the system run queue averaged over |
| 11364 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 11365 | Raises OSError if the load average was unobtainable. |
| 11366 | [clinic start generated code]*/ |
| 11367 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11368 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11369 | os_getloadavg_impl(PyObject *module) |
| 11370 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11371 | { |
| 11372 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11373 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11374 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 11375 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11376 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11377 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11378 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11379 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11380 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11381 | |
| 11382 | /*[clinic input] |
| 11383 | os.device_encoding |
| 11384 | fd: int |
| 11385 | |
| 11386 | Return a string describing the encoding of a terminal's file descriptor. |
| 11387 | |
| 11388 | The file descriptor must be attached to a terminal. |
| 11389 | If the device is not a terminal, return None. |
| 11390 | [clinic start generated code]*/ |
| 11391 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11392 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11393 | os_device_encoding_impl(PyObject *module, int fd) |
| 11394 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11395 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 11396 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 11397 | } |
| 11398 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11399 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11400 | #ifdef HAVE_SETRESUID |
| 11401 | /*[clinic input] |
| 11402 | os.setresuid |
| 11403 | |
| 11404 | ruid: uid_t |
| 11405 | euid: uid_t |
| 11406 | suid: uid_t |
| 11407 | / |
| 11408 | |
| 11409 | Set the current process's real, effective, and saved user ids. |
| 11410 | [clinic start generated code]*/ |
| 11411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11412 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11413 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 11414 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11415 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11416 | if (setresuid(ruid, euid, suid) < 0) |
| 11417 | return posix_error(); |
| 11418 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11419 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11420 | #endif /* HAVE_SETRESUID */ |
| 11421 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11422 | |
| 11423 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11424 | /*[clinic input] |
| 11425 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11426 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11427 | rgid: gid_t |
| 11428 | egid: gid_t |
| 11429 | sgid: gid_t |
| 11430 | / |
| 11431 | |
| 11432 | Set the current process's real, effective, and saved group ids. |
| 11433 | [clinic start generated code]*/ |
| 11434 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11435 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11436 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 11437 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11438 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11439 | if (setresgid(rgid, egid, sgid) < 0) |
| 11440 | return posix_error(); |
| 11441 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11442 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11443 | #endif /* HAVE_SETRESGID */ |
| 11444 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11445 | |
| 11446 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11447 | /*[clinic input] |
| 11448 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11449 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11450 | Return a tuple of the current process's real, effective, and saved user ids. |
| 11451 | [clinic start generated code]*/ |
| 11452 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11453 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11454 | os_getresuid_impl(PyObject *module) |
| 11455 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11456 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11457 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11458 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 11459 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11460 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 11461 | _PyLong_FromUid(euid), |
| 11462 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11463 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11464 | #endif /* HAVE_GETRESUID */ |
| 11465 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11466 | |
| 11467 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11468 | /*[clinic input] |
| 11469 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11470 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11471 | Return a tuple of the current process's real, effective, and saved group ids. |
| 11472 | [clinic start generated code]*/ |
| 11473 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11474 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11475 | os_getresgid_impl(PyObject *module) |
| 11476 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11477 | { |
| 11478 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11479 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 11480 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11481 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 11482 | _PyLong_FromGid(egid), |
| 11483 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11484 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11485 | #endif /* HAVE_GETRESGID */ |
| 11486 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11487 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11488 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11489 | /*[clinic input] |
| 11490 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11491 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11492 | path: path_t(allow_fd=True) |
| 11493 | attribute: path_t |
| 11494 | * |
| 11495 | follow_symlinks: bool = True |
| 11496 | |
| 11497 | Return the value of extended attribute attribute on path. |
| 11498 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11499 | 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] | 11500 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11501 | link, getxattr will examine the symbolic link itself instead of the file |
| 11502 | the link points to. |
| 11503 | |
| 11504 | [clinic start generated code]*/ |
| 11505 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11506 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11507 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11508 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11509 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11510 | { |
| 11511 | Py_ssize_t i; |
| 11512 | PyObject *buffer = NULL; |
| 11513 | |
| 11514 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 11515 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11516 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11517 | for (i = 0; ; i++) { |
| 11518 | void *ptr; |
| 11519 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11520 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11521 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11522 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11523 | path_error(path); |
| 11524 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11525 | } |
| 11526 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 11527 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11528 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11529 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11530 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11531 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11532 | if (path->fd >= 0) |
| 11533 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11534 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11535 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11536 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11537 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11538 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11539 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11540 | if (result < 0) { |
| 11541 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11542 | if (errno == ERANGE) |
| 11543 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11544 | path_error(path); |
| 11545 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11546 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11547 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11548 | if (result != buffer_size) { |
| 11549 | /* Can only shrink. */ |
| 11550 | _PyBytes_Resize(&buffer, result); |
| 11551 | } |
| 11552 | break; |
| 11553 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11554 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11555 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11556 | } |
| 11557 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11558 | |
| 11559 | /*[clinic input] |
| 11560 | os.setxattr |
| 11561 | |
| 11562 | path: path_t(allow_fd=True) |
| 11563 | attribute: path_t |
| 11564 | value: Py_buffer |
| 11565 | flags: int = 0 |
| 11566 | * |
| 11567 | follow_symlinks: bool = True |
| 11568 | |
| 11569 | Set extended attribute attribute on path to value. |
| 11570 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11571 | 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] | 11572 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11573 | link, setxattr will modify the symbolic link itself instead of the file |
| 11574 | the link points to. |
| 11575 | |
| 11576 | [clinic start generated code]*/ |
| 11577 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11578 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11579 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11580 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11581 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11582 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11583 | ssize_t result; |
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 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11586 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11587 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11588 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11589 | if (path->fd > -1) |
| 11590 | result = fsetxattr(path->fd, attribute->narrow, |
| 11591 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11592 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11593 | result = setxattr(path->narrow, attribute->narrow, |
| 11594 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11595 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11596 | result = lsetxattr(path->narrow, attribute->narrow, |
| 11597 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11598 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11599 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11600 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11601 | path_error(path); |
| 11602 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11603 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11604 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11605 | Py_RETURN_NONE; |
| 11606 | } |
| 11607 | |
| 11608 | |
| 11609 | /*[clinic input] |
| 11610 | os.removexattr |
| 11611 | |
| 11612 | path: path_t(allow_fd=True) |
| 11613 | attribute: path_t |
| 11614 | * |
| 11615 | follow_symlinks: bool = True |
| 11616 | |
| 11617 | Remove extended attribute attribute on path. |
| 11618 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11619 | 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] | 11620 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11621 | link, removexattr will modify the symbolic link itself instead of the file |
| 11622 | the link points to. |
| 11623 | |
| 11624 | [clinic start generated code]*/ |
| 11625 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11626 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11627 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11628 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11629 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11630 | { |
| 11631 | ssize_t result; |
| 11632 | |
| 11633 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 11634 | return NULL; |
| 11635 | |
| 11636 | Py_BEGIN_ALLOW_THREADS; |
| 11637 | if (path->fd > -1) |
| 11638 | result = fremovexattr(path->fd, attribute->narrow); |
| 11639 | else if (follow_symlinks) |
| 11640 | result = removexattr(path->narrow, attribute->narrow); |
| 11641 | else |
| 11642 | result = lremovexattr(path->narrow, attribute->narrow); |
| 11643 | Py_END_ALLOW_THREADS; |
| 11644 | |
| 11645 | if (result) { |
| 11646 | return path_error(path); |
| 11647 | } |
| 11648 | |
| 11649 | Py_RETURN_NONE; |
| 11650 | } |
| 11651 | |
| 11652 | |
| 11653 | /*[clinic input] |
| 11654 | os.listxattr |
| 11655 | |
| 11656 | path: path_t(allow_fd=True, nullable=True) = None |
| 11657 | * |
| 11658 | follow_symlinks: bool = True |
| 11659 | |
| 11660 | Return a list of extended attributes on path. |
| 11661 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11662 | 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] | 11663 | if path is None, listxattr will examine the current directory. |
| 11664 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11665 | link, listxattr will examine the symbolic link itself instead of the file |
| 11666 | the link points to. |
| 11667 | [clinic start generated code]*/ |
| 11668 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11669 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11670 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11671 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11672 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11673 | Py_ssize_t i; |
| 11674 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11675 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11676 | char *buffer = NULL; |
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("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11679 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11680 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11681 | name = path->narrow ? path->narrow : "."; |
| 11682 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11683 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11684 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11685 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11686 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11687 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11688 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 11689 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11690 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11691 | break; |
| 11692 | } |
| 11693 | buffer = PyMem_MALLOC(buffer_size); |
| 11694 | if (!buffer) { |
| 11695 | PyErr_NoMemory(); |
| 11696 | break; |
| 11697 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11698 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11699 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11700 | if (path->fd > -1) |
| 11701 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11702 | else if (follow_symlinks) |
| 11703 | length = listxattr(name, buffer, buffer_size); |
| 11704 | else |
| 11705 | length = llistxattr(name, buffer, buffer_size); |
| 11706 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11707 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11708 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11709 | if (errno == ERANGE) { |
| 11710 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 11711 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11712 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11713 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11714 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11715 | break; |
| 11716 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11717 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11718 | result = PyList_New(0); |
| 11719 | if (!result) { |
| 11720 | goto exit; |
| 11721 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11722 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11723 | end = buffer + length; |
| 11724 | for (trace = start = buffer; trace != end; trace++) { |
| 11725 | if (!*trace) { |
| 11726 | int error; |
| 11727 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 11728 | trace - start); |
| 11729 | if (!attribute) { |
| 11730 | Py_DECREF(result); |
| 11731 | result = NULL; |
| 11732 | goto exit; |
| 11733 | } |
| 11734 | error = PyList_Append(result, attribute); |
| 11735 | Py_DECREF(attribute); |
| 11736 | if (error) { |
| 11737 | Py_DECREF(result); |
| 11738 | result = NULL; |
| 11739 | goto exit; |
| 11740 | } |
| 11741 | start = trace + 1; |
| 11742 | } |
| 11743 | } |
| 11744 | break; |
| 11745 | } |
| 11746 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11747 | if (buffer) |
| 11748 | PyMem_FREE(buffer); |
| 11749 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11750 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11751 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11752 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11753 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11754 | /*[clinic input] |
| 11755 | os.urandom |
| 11756 | |
| 11757 | size: Py_ssize_t |
| 11758 | / |
| 11759 | |
| 11760 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 11761 | [clinic start generated code]*/ |
| 11762 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11763 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11764 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 11765 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11766 | { |
| 11767 | PyObject *bytes; |
| 11768 | int result; |
| 11769 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11770 | if (size < 0) |
| 11771 | return PyErr_Format(PyExc_ValueError, |
| 11772 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11773 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 11774 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11775 | return NULL; |
| 11776 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 11777 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11778 | if (result == -1) { |
| 11779 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11780 | return NULL; |
| 11781 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11782 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11783 | } |
| 11784 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11785 | /* Terminal size querying */ |
| 11786 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 11787 | static PyTypeObject* TerminalSizeType; |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11788 | |
| 11789 | PyDoc_STRVAR(TerminalSize_docstring, |
| 11790 | "A tuple of (columns, lines) for holding terminal window size"); |
| 11791 | |
| 11792 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 11793 | {"columns", "width of the terminal window in characters"}, |
| 11794 | {"lines", "height of the terminal window in characters"}, |
| 11795 | {NULL, NULL} |
| 11796 | }; |
| 11797 | |
| 11798 | static PyStructSequence_Desc TerminalSize_desc = { |
| 11799 | "os.terminal_size", |
| 11800 | TerminalSize_docstring, |
| 11801 | TerminalSize_fields, |
| 11802 | 2, |
| 11803 | }; |
| 11804 | |
| 11805 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11806 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11807 | PyDoc_STRVAR(termsize__doc__, |
| 11808 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 11809 | "\n" \ |
| 11810 | "The optional argument fd (default standard output) specifies\n" \ |
| 11811 | "which file descriptor should be queried.\n" \ |
| 11812 | "\n" \ |
| 11813 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 11814 | "is thrown.\n" \ |
| 11815 | "\n" \ |
| 11816 | "This function will only be defined if an implementation is\n" \ |
| 11817 | "available for this system.\n" \ |
| 11818 | "\n" \ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 11819 | "shutil.get_terminal_size is the high-level function which should\n" \ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11820 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 11821 | |
| 11822 | static PyObject* |
| 11823 | get_terminal_size(PyObject *self, PyObject *args) |
| 11824 | { |
| 11825 | int columns, lines; |
| 11826 | PyObject *termsize; |
| 11827 | |
| 11828 | int fd = fileno(stdout); |
| 11829 | /* Under some conditions stdout may not be connected and |
| 11830 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 11831 | * GUI apps don't have valid standard streams by default. |
| 11832 | * |
| 11833 | * If this happens, and the optional fd argument is not present, |
| 11834 | * the ioctl below will fail returning EBADF. This is what we want. |
| 11835 | */ |
| 11836 | |
| 11837 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 11838 | return NULL; |
| 11839 | |
| 11840 | #ifdef TERMSIZE_USE_IOCTL |
| 11841 | { |
| 11842 | struct winsize w; |
| 11843 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 11844 | return PyErr_SetFromErrno(PyExc_OSError); |
| 11845 | columns = w.ws_col; |
| 11846 | lines = w.ws_row; |
| 11847 | } |
| 11848 | #endif /* TERMSIZE_USE_IOCTL */ |
| 11849 | |
| 11850 | #ifdef TERMSIZE_USE_CONIO |
| 11851 | { |
| 11852 | DWORD nhandle; |
| 11853 | HANDLE handle; |
| 11854 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 11855 | switch (fd) { |
| 11856 | case 0: nhandle = STD_INPUT_HANDLE; |
| 11857 | break; |
| 11858 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 11859 | break; |
| 11860 | case 2: nhandle = STD_ERROR_HANDLE; |
| 11861 | break; |
| 11862 | default: |
| 11863 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 11864 | } |
| 11865 | handle = GetStdHandle(nhandle); |
| 11866 | if (handle == NULL) |
| 11867 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 11868 | if (handle == INVALID_HANDLE_VALUE) |
| 11869 | return PyErr_SetFromWindowsErr(0); |
| 11870 | |
| 11871 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 11872 | return PyErr_SetFromWindowsErr(0); |
| 11873 | |
| 11874 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 11875 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 11876 | } |
| 11877 | #endif /* TERMSIZE_USE_CONIO */ |
| 11878 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 11879 | termsize = PyStructSequence_New(TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11880 | if (termsize == NULL) |
| 11881 | return NULL; |
| 11882 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 11883 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 11884 | if (PyErr_Occurred()) { |
| 11885 | Py_DECREF(termsize); |
| 11886 | return NULL; |
| 11887 | } |
| 11888 | return termsize; |
| 11889 | } |
| 11890 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 11891 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11892 | |
| 11893 | /*[clinic input] |
| 11894 | os.cpu_count |
| 11895 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 11896 | Return the number of CPUs in the system; return None if indeterminable. |
| 11897 | |
| 11898 | This number is not equivalent to the number of CPUs the current process can |
| 11899 | use. The number of usable CPUs can be obtained with |
| 11900 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11901 | [clinic start generated code]*/ |
| 11902 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11903 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11904 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 11905 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11906 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11907 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11908 | #ifdef MS_WINDOWS |
Christopher Wilcox | c67bae0 | 2017-08-30 05:01:08 -0400 | [diff] [blame] | 11909 | /* Vista is supported and the GetMaximumProcessorCount API is Win7+ |
| 11910 | Need to fallback to Vista behavior if this call isn't present */ |
| 11911 | HINSTANCE hKernel32; |
| 11912 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 11913 | |
| 11914 | static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL; |
| 11915 | *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32, |
| 11916 | "GetMaximumProcessorCount"); |
| 11917 | if (_GetMaximumProcessorCount != NULL) { |
| 11918 | ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS); |
| 11919 | } |
| 11920 | else { |
| 11921 | SYSTEM_INFO sysinfo; |
| 11922 | GetSystemInfo(&sysinfo); |
| 11923 | ncpu = sysinfo.dwNumberOfProcessors; |
| 11924 | } |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11925 | #elif defined(__hpux) |
| 11926 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 11927 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 11928 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11929 | #elif defined(__DragonFly__) || \ |
| 11930 | defined(__OpenBSD__) || \ |
| 11931 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11932 | defined(__NetBSD__) || \ |
| 11933 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 11934 | int mib[2]; |
| 11935 | size_t len = sizeof(ncpu); |
| 11936 | mib[0] = CTL_HW; |
| 11937 | mib[1] = HW_NCPU; |
| 11938 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 11939 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11940 | #endif |
| 11941 | if (ncpu >= 1) |
| 11942 | return PyLong_FromLong(ncpu); |
| 11943 | else |
| 11944 | Py_RETURN_NONE; |
| 11945 | } |
| 11946 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11947 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11948 | /*[clinic input] |
| 11949 | os.get_inheritable -> bool |
| 11950 | |
| 11951 | fd: int |
| 11952 | / |
| 11953 | |
| 11954 | Get the close-on-exe flag of the specified file descriptor. |
| 11955 | [clinic start generated code]*/ |
| 11956 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11957 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11958 | os_get_inheritable_impl(PyObject *module, int fd) |
| 11959 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11960 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11961 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11962 | _Py_BEGIN_SUPPRESS_IPH |
| 11963 | return_value = _Py_get_inheritable(fd); |
| 11964 | _Py_END_SUPPRESS_IPH |
| 11965 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11966 | } |
| 11967 | |
| 11968 | |
| 11969 | /*[clinic input] |
| 11970 | os.set_inheritable |
| 11971 | fd: int |
| 11972 | inheritable: int |
| 11973 | / |
| 11974 | |
| 11975 | Set the inheritable flag of the specified file descriptor. |
| 11976 | [clinic start generated code]*/ |
| 11977 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11978 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11979 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 11980 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11981 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11982 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11983 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11984 | _Py_BEGIN_SUPPRESS_IPH |
| 11985 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 11986 | _Py_END_SUPPRESS_IPH |
| 11987 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11988 | return NULL; |
| 11989 | Py_RETURN_NONE; |
| 11990 | } |
| 11991 | |
| 11992 | |
| 11993 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11994 | /*[clinic input] |
| 11995 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11996 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11997 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11998 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11999 | Get the close-on-exe flag of the specified file descriptor. |
| 12000 | [clinic start generated code]*/ |
| 12001 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12002 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12003 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12004 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12005 | { |
| 12006 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12007 | |
| 12008 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 12009 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12010 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12011 | } |
| 12012 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12013 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12014 | } |
| 12015 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12016 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12017 | /*[clinic input] |
| 12018 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12019 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12020 | inheritable: bool |
| 12021 | / |
| 12022 | |
| 12023 | Set the inheritable flag of the specified handle. |
| 12024 | [clinic start generated code]*/ |
| 12025 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12026 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12027 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12028 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12029 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12030 | { |
| 12031 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12032 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 12033 | PyErr_SetFromWindowsErr(0); |
| 12034 | return NULL; |
| 12035 | } |
| 12036 | Py_RETURN_NONE; |
| 12037 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12038 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12039 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12040 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12041 | /*[clinic input] |
| 12042 | os.get_blocking -> bool |
| 12043 | fd: int |
| 12044 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12045 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12046 | Get the blocking mode of the file descriptor. |
| 12047 | |
| 12048 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 12049 | [clinic start generated code]*/ |
| 12050 | |
| 12051 | static int |
| 12052 | os_get_blocking_impl(PyObject *module, int fd) |
| 12053 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12054 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12055 | int blocking; |
| 12056 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12057 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12058 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12059 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12060 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12061 | } |
| 12062 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12063 | /*[clinic input] |
| 12064 | os.set_blocking |
| 12065 | fd: int |
| 12066 | blocking: bool(accept={int}) |
| 12067 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12068 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12069 | Set the blocking mode of the specified file descriptor. |
| 12070 | |
| 12071 | Set the O_NONBLOCK flag if blocking is False, |
| 12072 | clear the O_NONBLOCK flag otherwise. |
| 12073 | [clinic start generated code]*/ |
| 12074 | |
| 12075 | static PyObject * |
| 12076 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 12077 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12078 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12079 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12080 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12081 | _Py_BEGIN_SUPPRESS_IPH |
| 12082 | result = _Py_set_blocking(fd, blocking); |
| 12083 | _Py_END_SUPPRESS_IPH |
| 12084 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12085 | return NULL; |
| 12086 | Py_RETURN_NONE; |
| 12087 | } |
| 12088 | #endif /* !MS_WINDOWS */ |
| 12089 | |
| 12090 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12091 | /*[clinic input] |
| 12092 | class os.DirEntry "DirEntry *" "&DirEntryType" |
| 12093 | [clinic start generated code]*/ |
| 12094 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12095 | |
| 12096 | typedef struct { |
| 12097 | PyObject_HEAD |
| 12098 | PyObject *name; |
| 12099 | PyObject *path; |
| 12100 | PyObject *stat; |
| 12101 | PyObject *lstat; |
| 12102 | #ifdef MS_WINDOWS |
| 12103 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12104 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12105 | int got_file_index; |
| 12106 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12107 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12108 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12109 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12110 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12111 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12112 | #endif |
| 12113 | } DirEntry; |
| 12114 | |
| 12115 | static void |
| 12116 | DirEntry_dealloc(DirEntry *entry) |
| 12117 | { |
| 12118 | Py_XDECREF(entry->name); |
| 12119 | Py_XDECREF(entry->path); |
| 12120 | Py_XDECREF(entry->stat); |
| 12121 | Py_XDECREF(entry->lstat); |
| 12122 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 12123 | } |
| 12124 | |
| 12125 | /* Forward reference */ |
| 12126 | static int |
| 12127 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 12128 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12129 | /*[clinic input] |
| 12130 | os.DirEntry.is_symlink -> bool |
| 12131 | |
| 12132 | Return True if the entry is a symbolic link; cached per entry. |
| 12133 | [clinic start generated code]*/ |
| 12134 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12135 | static int |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12136 | os_DirEntry_is_symlink_impl(DirEntry *self) |
| 12137 | /*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12138 | { |
| 12139 | #ifdef MS_WINDOWS |
| 12140 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12141 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 12142 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12143 | if (self->d_type != DT_UNKNOWN) |
| 12144 | return self->d_type == DT_LNK; |
| 12145 | else |
| 12146 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12147 | #else |
| 12148 | /* POSIX without d_type */ |
| 12149 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12150 | #endif |
| 12151 | } |
| 12152 | |
| 12153 | static PyObject * |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12154 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 12155 | { |
| 12156 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12157 | STRUCT_STAT st; |
| 12158 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12159 | |
| 12160 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12161 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 12162 | return NULL; |
| 12163 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12164 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12165 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 12166 | return NULL; |
| 12167 | const char *path = PyBytes_AS_STRING(ub); |
| 12168 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 12169 | #ifdef HAVE_FSTATAT |
| 12170 | result = fstatat(self->dir_fd, path, &st, |
| 12171 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 12172 | #else |
| 12173 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 12174 | return NULL; |
| 12175 | #endif /* HAVE_FSTATAT */ |
| 12176 | } |
| 12177 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12178 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12179 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12180 | if (follow_symlinks) |
| 12181 | result = STAT(path, &st); |
| 12182 | else |
| 12183 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12184 | } |
| 12185 | Py_DECREF(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12186 | |
| 12187 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12188 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12189 | |
| 12190 | return _pystat_fromstructstat(&st); |
| 12191 | } |
| 12192 | |
| 12193 | static PyObject * |
| 12194 | DirEntry_get_lstat(DirEntry *self) |
| 12195 | { |
| 12196 | if (!self->lstat) { |
| 12197 | #ifdef MS_WINDOWS |
| 12198 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 12199 | #else /* POSIX */ |
| 12200 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 12201 | #endif |
| 12202 | } |
| 12203 | Py_XINCREF(self->lstat); |
| 12204 | return self->lstat; |
| 12205 | } |
| 12206 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12207 | /*[clinic input] |
| 12208 | os.DirEntry.stat |
| 12209 | * |
| 12210 | follow_symlinks: bool = True |
| 12211 | |
| 12212 | Return stat_result object for the entry; cached per entry. |
| 12213 | [clinic start generated code]*/ |
| 12214 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12215 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12216 | os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks) |
| 12217 | /*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12218 | { |
| 12219 | if (!follow_symlinks) |
| 12220 | return DirEntry_get_lstat(self); |
| 12221 | |
| 12222 | if (!self->stat) { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12223 | int result = os_DirEntry_is_symlink_impl(self); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12224 | if (result == -1) |
| 12225 | return NULL; |
| 12226 | else if (result) |
| 12227 | self->stat = DirEntry_fetch_stat(self, 1); |
| 12228 | else |
| 12229 | self->stat = DirEntry_get_lstat(self); |
| 12230 | } |
| 12231 | |
| 12232 | Py_XINCREF(self->stat); |
| 12233 | return self->stat; |
| 12234 | } |
| 12235 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12236 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 12237 | static int |
| 12238 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 12239 | { |
| 12240 | PyObject *stat = NULL; |
| 12241 | PyObject *st_mode = NULL; |
| 12242 | long mode; |
| 12243 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12244 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12245 | int is_symlink; |
| 12246 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12247 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12248 | #ifdef MS_WINDOWS |
| 12249 | unsigned long dir_bits; |
| 12250 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12251 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12252 | |
| 12253 | #ifdef MS_WINDOWS |
| 12254 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 12255 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12256 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12257 | is_symlink = self->d_type == DT_LNK; |
| 12258 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 12259 | #endif |
| 12260 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12261 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12262 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12263 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12264 | stat = os_DirEntry_stat_impl(self, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12265 | if (!stat) { |
| 12266 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 12267 | /* If file doesn't exist (anymore), then return False |
| 12268 | (i.e., say it's not a file/directory) */ |
| 12269 | PyErr_Clear(); |
| 12270 | return 0; |
| 12271 | } |
| 12272 | goto error; |
| 12273 | } |
| 12274 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 12275 | if (!st_mode) |
| 12276 | goto error; |
| 12277 | |
| 12278 | mode = PyLong_AsLong(st_mode); |
| 12279 | if (mode == -1 && PyErr_Occurred()) |
| 12280 | goto error; |
| 12281 | Py_CLEAR(st_mode); |
| 12282 | Py_CLEAR(stat); |
| 12283 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12284 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12285 | } |
| 12286 | else if (is_symlink) { |
| 12287 | assert(mode_bits != S_IFLNK); |
| 12288 | result = 0; |
| 12289 | } |
| 12290 | else { |
| 12291 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 12292 | #ifdef MS_WINDOWS |
| 12293 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 12294 | if (mode_bits == S_IFDIR) |
| 12295 | result = dir_bits != 0; |
| 12296 | else |
| 12297 | result = dir_bits == 0; |
| 12298 | #else /* POSIX */ |
| 12299 | if (mode_bits == S_IFDIR) |
| 12300 | result = self->d_type == DT_DIR; |
| 12301 | else |
| 12302 | result = self->d_type == DT_REG; |
| 12303 | #endif |
| 12304 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12305 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12306 | |
| 12307 | return result; |
| 12308 | |
| 12309 | error: |
| 12310 | Py_XDECREF(st_mode); |
| 12311 | Py_XDECREF(stat); |
| 12312 | return -1; |
| 12313 | } |
| 12314 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12315 | /*[clinic input] |
| 12316 | os.DirEntry.is_dir -> bool |
| 12317 | * |
| 12318 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12319 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12320 | Return True if the entry is a directory; cached per entry. |
| 12321 | [clinic start generated code]*/ |
| 12322 | |
| 12323 | static int |
| 12324 | os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks) |
| 12325 | /*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/ |
| 12326 | { |
| 12327 | return DirEntry_test_mode(self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12328 | } |
| 12329 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12330 | /*[clinic input] |
| 12331 | os.DirEntry.is_file -> bool |
| 12332 | * |
| 12333 | follow_symlinks: bool = True |
| 12334 | |
| 12335 | Return True if the entry is a file; cached per entry. |
| 12336 | [clinic start generated code]*/ |
| 12337 | |
| 12338 | static int |
| 12339 | os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks) |
| 12340 | /*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12341 | { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12342 | return DirEntry_test_mode(self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12343 | } |
| 12344 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12345 | /*[clinic input] |
| 12346 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12347 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12348 | Return inode of the entry; cached per entry. |
| 12349 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12350 | |
| 12351 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12352 | os_DirEntry_inode_impl(DirEntry *self) |
| 12353 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12354 | { |
| 12355 | #ifdef MS_WINDOWS |
| 12356 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12357 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12358 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12359 | STRUCT_STAT stat; |
| 12360 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12361 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12362 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12363 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12364 | path = PyUnicode_AsUnicode(unicode); |
| 12365 | result = LSTAT(path, &stat); |
| 12366 | Py_DECREF(unicode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12367 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12368 | if (result != 0) |
| 12369 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12370 | |
| 12371 | self->win32_file_index = stat.st_ino; |
| 12372 | self->got_file_index = 1; |
| 12373 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12374 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 12375 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12376 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 12377 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 12378 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12379 | #endif |
| 12380 | } |
| 12381 | |
| 12382 | static PyObject * |
| 12383 | DirEntry_repr(DirEntry *self) |
| 12384 | { |
| 12385 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 12386 | } |
| 12387 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12388 | /*[clinic input] |
| 12389 | os.DirEntry.__fspath__ |
| 12390 | |
| 12391 | Returns the path for the entry. |
| 12392 | [clinic start generated code]*/ |
| 12393 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12394 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12395 | os_DirEntry___fspath___impl(DirEntry *self) |
| 12396 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12397 | { |
| 12398 | Py_INCREF(self->path); |
| 12399 | return self->path; |
| 12400 | } |
| 12401 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12402 | static PyMemberDef DirEntry_members[] = { |
| 12403 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 12404 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 12405 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 12406 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 12407 | {NULL} |
| 12408 | }; |
| 12409 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12410 | #include "clinic/posixmodule.c.h" |
| 12411 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12412 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12413 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 12414 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 12415 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 12416 | OS_DIRENTRY_STAT_METHODDEF |
| 12417 | OS_DIRENTRY_INODE_METHODDEF |
| 12418 | OS_DIRENTRY___FSPATH___METHODDEF |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12419 | {NULL} |
| 12420 | }; |
| 12421 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12422 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12423 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12424 | MODNAME ".DirEntry", /* tp_name */ |
| 12425 | sizeof(DirEntry), /* tp_basicsize */ |
| 12426 | 0, /* tp_itemsize */ |
| 12427 | /* methods */ |
| 12428 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
| 12429 | 0, /* tp_print */ |
| 12430 | 0, /* tp_getattr */ |
| 12431 | 0, /* tp_setattr */ |
| 12432 | 0, /* tp_compare */ |
| 12433 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 12434 | 0, /* tp_as_number */ |
| 12435 | 0, /* tp_as_sequence */ |
| 12436 | 0, /* tp_as_mapping */ |
| 12437 | 0, /* tp_hash */ |
| 12438 | 0, /* tp_call */ |
| 12439 | 0, /* tp_str */ |
| 12440 | 0, /* tp_getattro */ |
| 12441 | 0, /* tp_setattro */ |
| 12442 | 0, /* tp_as_buffer */ |
| 12443 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 12444 | 0, /* tp_doc */ |
| 12445 | 0, /* tp_traverse */ |
| 12446 | 0, /* tp_clear */ |
| 12447 | 0, /* tp_richcompare */ |
| 12448 | 0, /* tp_weaklistoffset */ |
| 12449 | 0, /* tp_iter */ |
| 12450 | 0, /* tp_iternext */ |
| 12451 | DirEntry_methods, /* tp_methods */ |
| 12452 | DirEntry_members, /* tp_members */ |
| 12453 | }; |
| 12454 | |
| 12455 | #ifdef MS_WINDOWS |
| 12456 | |
| 12457 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12458 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12459 | { |
| 12460 | Py_ssize_t path_len; |
| 12461 | Py_ssize_t size; |
| 12462 | wchar_t *result; |
| 12463 | wchar_t ch; |
| 12464 | |
| 12465 | if (!path_wide) { /* Default arg: "." */ |
| 12466 | path_wide = L"."; |
| 12467 | path_len = 1; |
| 12468 | } |
| 12469 | else { |
| 12470 | path_len = wcslen(path_wide); |
| 12471 | } |
| 12472 | |
| 12473 | /* The +1's are for the path separator and the NUL */ |
| 12474 | size = path_len + 1 + wcslen(filename) + 1; |
| 12475 | result = PyMem_New(wchar_t, size); |
| 12476 | if (!result) { |
| 12477 | PyErr_NoMemory(); |
| 12478 | return NULL; |
| 12479 | } |
| 12480 | wcscpy(result, path_wide); |
| 12481 | if (path_len > 0) { |
| 12482 | ch = result[path_len - 1]; |
| 12483 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 12484 | result[path_len++] = SEP; |
| 12485 | wcscpy(result + path_len, filename); |
| 12486 | } |
| 12487 | return result; |
| 12488 | } |
| 12489 | |
| 12490 | static PyObject * |
| 12491 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 12492 | { |
| 12493 | DirEntry *entry; |
| 12494 | BY_HANDLE_FILE_INFORMATION file_info; |
| 12495 | ULONG reparse_tag; |
| 12496 | wchar_t *joined_path; |
| 12497 | |
| 12498 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 12499 | if (!entry) |
| 12500 | return NULL; |
| 12501 | entry->name = NULL; |
| 12502 | entry->path = NULL; |
| 12503 | entry->stat = NULL; |
| 12504 | entry->lstat = NULL; |
| 12505 | entry->got_file_index = 0; |
| 12506 | |
| 12507 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 12508 | if (!entry->name) |
| 12509 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12510 | if (path->narrow) { |
| 12511 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 12512 | if (!entry->name) |
| 12513 | goto error; |
| 12514 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12515 | |
| 12516 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 12517 | if (!joined_path) |
| 12518 | goto error; |
| 12519 | |
| 12520 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 12521 | PyMem_Free(joined_path); |
| 12522 | if (!entry->path) |
| 12523 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12524 | if (path->narrow) { |
| 12525 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 12526 | if (!entry->path) |
| 12527 | goto error; |
| 12528 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12529 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12530 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12531 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 12532 | |
| 12533 | return (PyObject *)entry; |
| 12534 | |
| 12535 | error: |
| 12536 | Py_DECREF(entry); |
| 12537 | return NULL; |
| 12538 | } |
| 12539 | |
| 12540 | #else /* POSIX */ |
| 12541 | |
| 12542 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12543 | 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] | 12544 | { |
| 12545 | Py_ssize_t path_len; |
| 12546 | Py_ssize_t size; |
| 12547 | char *result; |
| 12548 | |
| 12549 | if (!path_narrow) { /* Default arg: "." */ |
| 12550 | path_narrow = "."; |
| 12551 | path_len = 1; |
| 12552 | } |
| 12553 | else { |
| 12554 | path_len = strlen(path_narrow); |
| 12555 | } |
| 12556 | |
| 12557 | if (filename_len == -1) |
| 12558 | filename_len = strlen(filename); |
| 12559 | |
| 12560 | /* The +1's are for the path separator and the NUL */ |
| 12561 | size = path_len + 1 + filename_len + 1; |
| 12562 | result = PyMem_New(char, size); |
| 12563 | if (!result) { |
| 12564 | PyErr_NoMemory(); |
| 12565 | return NULL; |
| 12566 | } |
| 12567 | strcpy(result, path_narrow); |
| 12568 | if (path_len > 0 && result[path_len - 1] != '/') |
| 12569 | result[path_len++] = '/'; |
| 12570 | strcpy(result + path_len, filename); |
| 12571 | return result; |
| 12572 | } |
| 12573 | |
| 12574 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12575 | 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] | 12576 | ino_t d_ino |
| 12577 | #ifdef HAVE_DIRENT_D_TYPE |
| 12578 | , unsigned char d_type |
| 12579 | #endif |
| 12580 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12581 | { |
| 12582 | DirEntry *entry; |
| 12583 | char *joined_path; |
| 12584 | |
| 12585 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 12586 | if (!entry) |
| 12587 | return NULL; |
| 12588 | entry->name = NULL; |
| 12589 | entry->path = NULL; |
| 12590 | entry->stat = NULL; |
| 12591 | entry->lstat = NULL; |
| 12592 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12593 | if (path->fd != -1) { |
| 12594 | entry->dir_fd = path->fd; |
| 12595 | joined_path = NULL; |
| 12596 | } |
| 12597 | else { |
| 12598 | entry->dir_fd = DEFAULT_DIR_FD; |
| 12599 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 12600 | if (!joined_path) |
| 12601 | goto error; |
| 12602 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12603 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 12604 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12605 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12606 | if (joined_path) |
| 12607 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12608 | } |
| 12609 | else { |
| 12610 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12611 | if (joined_path) |
| 12612 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12613 | } |
| 12614 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12615 | if (!entry->name) |
| 12616 | goto error; |
| 12617 | |
| 12618 | if (path->fd != -1) { |
| 12619 | entry->path = entry->name; |
| 12620 | Py_INCREF(entry->path); |
| 12621 | } |
| 12622 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12623 | goto error; |
| 12624 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12625 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12626 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12627 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12628 | entry->d_ino = d_ino; |
| 12629 | |
| 12630 | return (PyObject *)entry; |
| 12631 | |
| 12632 | error: |
| 12633 | Py_XDECREF(entry); |
| 12634 | return NULL; |
| 12635 | } |
| 12636 | |
| 12637 | #endif |
| 12638 | |
| 12639 | |
| 12640 | typedef struct { |
| 12641 | PyObject_HEAD |
| 12642 | path_t path; |
| 12643 | #ifdef MS_WINDOWS |
| 12644 | HANDLE handle; |
| 12645 | WIN32_FIND_DATAW file_data; |
| 12646 | int first_time; |
| 12647 | #else /* POSIX */ |
| 12648 | DIR *dirp; |
| 12649 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12650 | #ifdef HAVE_FDOPENDIR |
| 12651 | int fd; |
| 12652 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12653 | } ScandirIterator; |
| 12654 | |
| 12655 | #ifdef MS_WINDOWS |
| 12656 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12657 | static int |
| 12658 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 12659 | { |
| 12660 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 12661 | } |
| 12662 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12663 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12664 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12665 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12666 | HANDLE handle = iterator->handle; |
| 12667 | |
| 12668 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12669 | return; |
| 12670 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12671 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12672 | Py_BEGIN_ALLOW_THREADS |
| 12673 | FindClose(handle); |
| 12674 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12675 | } |
| 12676 | |
| 12677 | static PyObject * |
| 12678 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12679 | { |
| 12680 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 12681 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12682 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12683 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12684 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12685 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12686 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12687 | |
| 12688 | while (1) { |
| 12689 | if (!iterator->first_time) { |
| 12690 | Py_BEGIN_ALLOW_THREADS |
| 12691 | success = FindNextFileW(iterator->handle, file_data); |
| 12692 | Py_END_ALLOW_THREADS |
| 12693 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12694 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12695 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12696 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12697 | break; |
| 12698 | } |
| 12699 | } |
| 12700 | iterator->first_time = 0; |
| 12701 | |
| 12702 | /* Skip over . and .. */ |
| 12703 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12704 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 12705 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 12706 | if (!entry) |
| 12707 | break; |
| 12708 | return entry; |
| 12709 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12710 | |
| 12711 | /* Loop till we get a non-dot directory or finish iterating */ |
| 12712 | } |
| 12713 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12714 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12715 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12716 | return NULL; |
| 12717 | } |
| 12718 | |
| 12719 | #else /* POSIX */ |
| 12720 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12721 | static int |
| 12722 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 12723 | { |
| 12724 | return !iterator->dirp; |
| 12725 | } |
| 12726 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12727 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12728 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12729 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12730 | DIR *dirp = iterator->dirp; |
| 12731 | |
| 12732 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12733 | return; |
| 12734 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12735 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12736 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12737 | #ifdef HAVE_FDOPENDIR |
| 12738 | if (iterator->path.fd != -1) |
| 12739 | rewinddir(dirp); |
| 12740 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12741 | closedir(dirp); |
| 12742 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12743 | return; |
| 12744 | } |
| 12745 | |
| 12746 | static PyObject * |
| 12747 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12748 | { |
| 12749 | struct dirent *direntp; |
| 12750 | Py_ssize_t name_len; |
| 12751 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12752 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12753 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12754 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12755 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12756 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12757 | |
| 12758 | while (1) { |
| 12759 | errno = 0; |
| 12760 | Py_BEGIN_ALLOW_THREADS |
| 12761 | direntp = readdir(iterator->dirp); |
| 12762 | Py_END_ALLOW_THREADS |
| 12763 | |
| 12764 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12765 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12766 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12767 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12768 | break; |
| 12769 | } |
| 12770 | |
| 12771 | /* Skip over . and .. */ |
| 12772 | name_len = NAMLEN(direntp); |
| 12773 | is_dot = direntp->d_name[0] == '.' && |
| 12774 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 12775 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12776 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12777 | name_len, direntp->d_ino |
| 12778 | #ifdef HAVE_DIRENT_D_TYPE |
| 12779 | , direntp->d_type |
| 12780 | #endif |
| 12781 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12782 | if (!entry) |
| 12783 | break; |
| 12784 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12785 | } |
| 12786 | |
| 12787 | /* Loop till we get a non-dot directory or finish iterating */ |
| 12788 | } |
| 12789 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12790 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12791 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12792 | return NULL; |
| 12793 | } |
| 12794 | |
| 12795 | #endif |
| 12796 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12797 | static PyObject * |
| 12798 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 12799 | { |
| 12800 | ScandirIterator_closedir(self); |
| 12801 | Py_RETURN_NONE; |
| 12802 | } |
| 12803 | |
| 12804 | static PyObject * |
| 12805 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 12806 | { |
| 12807 | Py_INCREF(self); |
| 12808 | return self; |
| 12809 | } |
| 12810 | |
| 12811 | static PyObject * |
| 12812 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 12813 | { |
| 12814 | ScandirIterator_closedir(self); |
| 12815 | Py_RETURN_NONE; |
| 12816 | } |
| 12817 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12818 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12819 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 12820 | { |
| 12821 | PyObject *error_type, *error_value, *error_traceback; |
| 12822 | |
| 12823 | /* Save the current exception, if any. */ |
| 12824 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 12825 | |
| 12826 | if (!ScandirIterator_is_closed(iterator)) { |
| 12827 | ScandirIterator_closedir(iterator); |
| 12828 | |
| 12829 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 12830 | "unclosed scandir iterator %R", iterator)) { |
| 12831 | /* Spurious errors can appear at shutdown */ |
| 12832 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 12833 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 12834 | } |
| 12835 | } |
| 12836 | } |
| 12837 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12838 | path_cleanup(&iterator->path); |
| 12839 | |
| 12840 | /* Restore the saved exception. */ |
| 12841 | PyErr_Restore(error_type, error_value, error_traceback); |
| 12842 | } |
| 12843 | |
| 12844 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12845 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 12846 | { |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12847 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 12848 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12849 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12850 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 12851 | } |
| 12852 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12853 | static PyMethodDef ScandirIterator_methods[] = { |
| 12854 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 12855 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 12856 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 12857 | {NULL} |
| 12858 | }; |
| 12859 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12860 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12861 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12862 | MODNAME ".ScandirIterator", /* tp_name */ |
| 12863 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 12864 | 0, /* tp_itemsize */ |
| 12865 | /* methods */ |
| 12866 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
| 12867 | 0, /* tp_print */ |
| 12868 | 0, /* tp_getattr */ |
| 12869 | 0, /* tp_setattr */ |
| 12870 | 0, /* tp_compare */ |
| 12871 | 0, /* tp_repr */ |
| 12872 | 0, /* tp_as_number */ |
| 12873 | 0, /* tp_as_sequence */ |
| 12874 | 0, /* tp_as_mapping */ |
| 12875 | 0, /* tp_hash */ |
| 12876 | 0, /* tp_call */ |
| 12877 | 0, /* tp_str */ |
| 12878 | 0, /* tp_getattro */ |
| 12879 | 0, /* tp_setattro */ |
| 12880 | 0, /* tp_as_buffer */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12881 | Py_TPFLAGS_DEFAULT |
| 12882 | | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12883 | 0, /* tp_doc */ |
| 12884 | 0, /* tp_traverse */ |
| 12885 | 0, /* tp_clear */ |
| 12886 | 0, /* tp_richcompare */ |
| 12887 | 0, /* tp_weaklistoffset */ |
| 12888 | PyObject_SelfIter, /* tp_iter */ |
| 12889 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12890 | ScandirIterator_methods, /* tp_methods */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12891 | 0, /* tp_members */ |
| 12892 | 0, /* tp_getset */ |
| 12893 | 0, /* tp_base */ |
| 12894 | 0, /* tp_dict */ |
| 12895 | 0, /* tp_descr_get */ |
| 12896 | 0, /* tp_descr_set */ |
| 12897 | 0, /* tp_dictoffset */ |
| 12898 | 0, /* tp_init */ |
| 12899 | 0, /* tp_alloc */ |
| 12900 | 0, /* tp_new */ |
| 12901 | 0, /* tp_free */ |
| 12902 | 0, /* tp_is_gc */ |
| 12903 | 0, /* tp_bases */ |
| 12904 | 0, /* tp_mro */ |
| 12905 | 0, /* tp_cache */ |
| 12906 | 0, /* tp_subclasses */ |
| 12907 | 0, /* tp_weaklist */ |
| 12908 | 0, /* tp_del */ |
| 12909 | 0, /* tp_version_tag */ |
| 12910 | (destructor)ScandirIterator_finalize, /* tp_finalize */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12911 | }; |
| 12912 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12913 | /*[clinic input] |
| 12914 | os.scandir |
| 12915 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12916 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12917 | |
| 12918 | Return an iterator of DirEntry objects for given path. |
| 12919 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12920 | 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] | 12921 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 12922 | all other circumstances they will be str. |
| 12923 | |
| 12924 | If path is None, uses the path='.'. |
| 12925 | [clinic start generated code]*/ |
| 12926 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12927 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12928 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12929 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12930 | { |
| 12931 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12932 | #ifdef MS_WINDOWS |
| 12933 | wchar_t *path_strW; |
| 12934 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12935 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12936 | #ifdef HAVE_FDOPENDIR |
| 12937 | int fd = -1; |
| 12938 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12939 | #endif |
| 12940 | |
| 12941 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 12942 | if (!iterator) |
| 12943 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12944 | |
| 12945 | #ifdef MS_WINDOWS |
| 12946 | iterator->handle = INVALID_HANDLE_VALUE; |
| 12947 | #else |
| 12948 | iterator->dirp = NULL; |
| 12949 | #endif |
| 12950 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12951 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 12952 | /* Move the ownership to iterator->path */ |
| 12953 | path->object = NULL; |
| 12954 | path->cleanup = NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12955 | |
| 12956 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12957 | iterator->first_time = 1; |
| 12958 | |
| 12959 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 12960 | if (!path_strW) |
| 12961 | goto error; |
| 12962 | |
| 12963 | Py_BEGIN_ALLOW_THREADS |
| 12964 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 12965 | Py_END_ALLOW_THREADS |
| 12966 | |
| 12967 | PyMem_Free(path_strW); |
| 12968 | |
| 12969 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 12970 | path_error(&iterator->path); |
| 12971 | goto error; |
| 12972 | } |
| 12973 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12974 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12975 | #ifdef HAVE_FDOPENDIR |
| 12976 | if (path->fd != -1) { |
| 12977 | /* closedir() closes the FD, so we duplicate it */ |
| 12978 | fd = _Py_dup(path->fd); |
| 12979 | if (fd == -1) |
| 12980 | goto error; |
| 12981 | |
| 12982 | Py_BEGIN_ALLOW_THREADS |
| 12983 | iterator->dirp = fdopendir(fd); |
| 12984 | Py_END_ALLOW_THREADS |
| 12985 | } |
| 12986 | else |
| 12987 | #endif |
| 12988 | { |
| 12989 | if (iterator->path.narrow) |
| 12990 | path_str = iterator->path.narrow; |
| 12991 | else |
| 12992 | path_str = "."; |
| 12993 | |
| 12994 | Py_BEGIN_ALLOW_THREADS |
| 12995 | iterator->dirp = opendir(path_str); |
| 12996 | Py_END_ALLOW_THREADS |
| 12997 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12998 | |
| 12999 | if (!iterator->dirp) { |
| 13000 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13001 | #ifdef HAVE_FDOPENDIR |
| 13002 | if (fd != -1) { |
| 13003 | Py_BEGIN_ALLOW_THREADS |
| 13004 | close(fd); |
| 13005 | Py_END_ALLOW_THREADS |
| 13006 | } |
| 13007 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13008 | goto error; |
| 13009 | } |
| 13010 | #endif |
| 13011 | |
| 13012 | return (PyObject *)iterator; |
| 13013 | |
| 13014 | error: |
| 13015 | Py_DECREF(iterator); |
| 13016 | return NULL; |
| 13017 | } |
| 13018 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13019 | /* |
| 13020 | Return the file system path representation of the object. |
| 13021 | |
| 13022 | If the object is str or bytes, then allow it to pass through with |
| 13023 | an incremented refcount. If the object defines __fspath__(), then |
| 13024 | return the result of that method. All other types raise a TypeError. |
| 13025 | */ |
| 13026 | PyObject * |
| 13027 | PyOS_FSPath(PyObject *path) |
| 13028 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 13029 | /* For error message reasons, this function is manually inlined in |
| 13030 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13031 | _Py_IDENTIFIER(__fspath__); |
| 13032 | PyObject *func = NULL; |
| 13033 | PyObject *path_repr = NULL; |
| 13034 | |
| 13035 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 13036 | Py_INCREF(path); |
| 13037 | return path; |
| 13038 | } |
| 13039 | |
| 13040 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 13041 | if (NULL == func) { |
| 13042 | return PyErr_Format(PyExc_TypeError, |
| 13043 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13044 | "not %.200s", |
| 13045 | Py_TYPE(path)->tp_name); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13046 | } |
| 13047 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 13048 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13049 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 13050 | if (NULL == path_repr) { |
| 13051 | return NULL; |
| 13052 | } |
| 13053 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13054 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 13055 | PyErr_Format(PyExc_TypeError, |
| 13056 | "expected %.200s.__fspath__() to return str or bytes, " |
| 13057 | "not %.200s", Py_TYPE(path)->tp_name, |
| 13058 | Py_TYPE(path_repr)->tp_name); |
| 13059 | Py_DECREF(path_repr); |
| 13060 | return NULL; |
| 13061 | } |
| 13062 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13063 | return path_repr; |
| 13064 | } |
| 13065 | |
| 13066 | /*[clinic input] |
| 13067 | os.fspath |
| 13068 | |
| 13069 | path: object |
| 13070 | |
| 13071 | Return the file system path representation of the object. |
| 13072 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 13073 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 13074 | object defines __fspath__(), then return the result of that method. All other |
| 13075 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13076 | [clinic start generated code]*/ |
| 13077 | |
| 13078 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 13079 | os_fspath_impl(PyObject *module, PyObject *path) |
| 13080 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13081 | { |
| 13082 | return PyOS_FSPath(path); |
| 13083 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13084 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13085 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13086 | /*[clinic input] |
| 13087 | os.getrandom |
| 13088 | |
| 13089 | size: Py_ssize_t |
| 13090 | flags: int=0 |
| 13091 | |
| 13092 | Obtain a series of random bytes. |
| 13093 | [clinic start generated code]*/ |
| 13094 | |
| 13095 | static PyObject * |
| 13096 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 13097 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 13098 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13099 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13100 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13101 | |
| 13102 | if (size < 0) { |
| 13103 | errno = EINVAL; |
| 13104 | return posix_error(); |
| 13105 | } |
| 13106 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13107 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 13108 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13109 | PyErr_NoMemory(); |
| 13110 | return NULL; |
| 13111 | } |
| 13112 | |
| 13113 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13114 | n = syscall(SYS_getrandom, |
| 13115 | PyBytes_AS_STRING(bytes), |
| 13116 | PyBytes_GET_SIZE(bytes), |
| 13117 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13118 | if (n < 0 && errno == EINTR) { |
| 13119 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13120 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13121 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13122 | |
| 13123 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13124 | continue; |
| 13125 | } |
| 13126 | break; |
| 13127 | } |
| 13128 | |
| 13129 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13130 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13131 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13132 | } |
| 13133 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13134 | if (n != size) { |
| 13135 | _PyBytes_Resize(&bytes, n); |
| 13136 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13137 | |
| 13138 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13139 | |
| 13140 | error: |
| 13141 | Py_DECREF(bytes); |
| 13142 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13143 | } |
| 13144 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 13145 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13146 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13147 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13148 | |
| 13149 | OS_STAT_METHODDEF |
| 13150 | OS_ACCESS_METHODDEF |
| 13151 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13152 | OS_CHDIR_METHODDEF |
| 13153 | OS_CHFLAGS_METHODDEF |
| 13154 | OS_CHMOD_METHODDEF |
| 13155 | OS_FCHMOD_METHODDEF |
| 13156 | OS_LCHMOD_METHODDEF |
| 13157 | OS_CHOWN_METHODDEF |
| 13158 | OS_FCHOWN_METHODDEF |
| 13159 | OS_LCHOWN_METHODDEF |
| 13160 | OS_LCHFLAGS_METHODDEF |
| 13161 | OS_CHROOT_METHODDEF |
| 13162 | OS_CTERMID_METHODDEF |
| 13163 | OS_GETCWD_METHODDEF |
| 13164 | OS_GETCWDB_METHODDEF |
| 13165 | OS_LINK_METHODDEF |
| 13166 | OS_LISTDIR_METHODDEF |
| 13167 | OS_LSTAT_METHODDEF |
| 13168 | OS_MKDIR_METHODDEF |
| 13169 | OS_NICE_METHODDEF |
| 13170 | OS_GETPRIORITY_METHODDEF |
| 13171 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13172 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 13173 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13174 | OS_READLINK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13175 | OS_RENAME_METHODDEF |
| 13176 | OS_REPLACE_METHODDEF |
| 13177 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13178 | OS_SYMLINK_METHODDEF |
| 13179 | OS_SYSTEM_METHODDEF |
| 13180 | OS_UMASK_METHODDEF |
| 13181 | OS_UNAME_METHODDEF |
| 13182 | OS_UNLINK_METHODDEF |
| 13183 | OS_REMOVE_METHODDEF |
| 13184 | OS_UTIME_METHODDEF |
| 13185 | OS_TIMES_METHODDEF |
| 13186 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 13187 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13188 | OS_EXECV_METHODDEF |
| 13189 | OS_EXECVE_METHODDEF |
| 13190 | OS_SPAWNV_METHODDEF |
| 13191 | OS_SPAWNVE_METHODDEF |
| 13192 | OS_FORK1_METHODDEF |
| 13193 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 13194 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13195 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 13196 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 13197 | OS_SCHED_GETPARAM_METHODDEF |
| 13198 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 13199 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 13200 | OS_SCHED_SETPARAM_METHODDEF |
| 13201 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 13202 | OS_SCHED_YIELD_METHODDEF |
| 13203 | OS_SCHED_SETAFFINITY_METHODDEF |
| 13204 | OS_SCHED_GETAFFINITY_METHODDEF |
| 13205 | OS_OPENPTY_METHODDEF |
| 13206 | OS_FORKPTY_METHODDEF |
| 13207 | OS_GETEGID_METHODDEF |
| 13208 | OS_GETEUID_METHODDEF |
| 13209 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 13210 | #ifdef HAVE_GETGROUPLIST |
| 13211 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 13212 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13213 | OS_GETGROUPS_METHODDEF |
| 13214 | OS_GETPID_METHODDEF |
| 13215 | OS_GETPGRP_METHODDEF |
| 13216 | OS_GETPPID_METHODDEF |
| 13217 | OS_GETUID_METHODDEF |
| 13218 | OS_GETLOGIN_METHODDEF |
| 13219 | OS_KILL_METHODDEF |
| 13220 | OS_KILLPG_METHODDEF |
| 13221 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13222 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13223 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13224 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13225 | OS_SETUID_METHODDEF |
| 13226 | OS_SETEUID_METHODDEF |
| 13227 | OS_SETREUID_METHODDEF |
| 13228 | OS_SETGID_METHODDEF |
| 13229 | OS_SETEGID_METHODDEF |
| 13230 | OS_SETREGID_METHODDEF |
| 13231 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13232 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13233 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13234 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13235 | OS_GETPGID_METHODDEF |
| 13236 | OS_SETPGRP_METHODDEF |
| 13237 | OS_WAIT_METHODDEF |
| 13238 | OS_WAIT3_METHODDEF |
| 13239 | OS_WAIT4_METHODDEF |
| 13240 | OS_WAITID_METHODDEF |
| 13241 | OS_WAITPID_METHODDEF |
| 13242 | OS_GETSID_METHODDEF |
| 13243 | OS_SETSID_METHODDEF |
| 13244 | OS_SETPGID_METHODDEF |
| 13245 | OS_TCGETPGRP_METHODDEF |
| 13246 | OS_TCSETPGRP_METHODDEF |
| 13247 | OS_OPEN_METHODDEF |
| 13248 | OS_CLOSE_METHODDEF |
| 13249 | OS_CLOSERANGE_METHODDEF |
| 13250 | OS_DEVICE_ENCODING_METHODDEF |
| 13251 | OS_DUP_METHODDEF |
| 13252 | OS_DUP2_METHODDEF |
| 13253 | OS_LOCKF_METHODDEF |
| 13254 | OS_LSEEK_METHODDEF |
| 13255 | OS_READ_METHODDEF |
| 13256 | OS_READV_METHODDEF |
| 13257 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13258 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13259 | OS_WRITE_METHODDEF |
| 13260 | OS_WRITEV_METHODDEF |
| 13261 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13262 | OS_PWRITEV_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13263 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 13264 | {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13265 | posix_sendfile__doc__}, |
| 13266 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13267 | OS_FSTAT_METHODDEF |
| 13268 | OS_ISATTY_METHODDEF |
| 13269 | OS_PIPE_METHODDEF |
| 13270 | OS_PIPE2_METHODDEF |
| 13271 | OS_MKFIFO_METHODDEF |
| 13272 | OS_MKNOD_METHODDEF |
| 13273 | OS_MAJOR_METHODDEF |
| 13274 | OS_MINOR_METHODDEF |
| 13275 | OS_MAKEDEV_METHODDEF |
| 13276 | OS_FTRUNCATE_METHODDEF |
| 13277 | OS_TRUNCATE_METHODDEF |
| 13278 | OS_POSIX_FALLOCATE_METHODDEF |
| 13279 | OS_POSIX_FADVISE_METHODDEF |
| 13280 | OS_PUTENV_METHODDEF |
| 13281 | OS_UNSETENV_METHODDEF |
| 13282 | OS_STRERROR_METHODDEF |
| 13283 | OS_FCHDIR_METHODDEF |
| 13284 | OS_FSYNC_METHODDEF |
| 13285 | OS_SYNC_METHODDEF |
| 13286 | OS_FDATASYNC_METHODDEF |
| 13287 | OS_WCOREDUMP_METHODDEF |
| 13288 | OS_WIFCONTINUED_METHODDEF |
| 13289 | OS_WIFSTOPPED_METHODDEF |
| 13290 | OS_WIFSIGNALED_METHODDEF |
| 13291 | OS_WIFEXITED_METHODDEF |
| 13292 | OS_WEXITSTATUS_METHODDEF |
| 13293 | OS_WTERMSIG_METHODDEF |
| 13294 | OS_WSTOPSIG_METHODDEF |
| 13295 | OS_FSTATVFS_METHODDEF |
| 13296 | OS_STATVFS_METHODDEF |
| 13297 | OS_CONFSTR_METHODDEF |
| 13298 | OS_SYSCONF_METHODDEF |
| 13299 | OS_FPATHCONF_METHODDEF |
| 13300 | OS_PATHCONF_METHODDEF |
| 13301 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 13302 | OS__GETFULLPATHNAME_METHODDEF |
| 13303 | OS__ISDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13304 | OS__GETDISKUSAGE_METHODDEF |
| 13305 | OS__GETFINALPATHNAME_METHODDEF |
| 13306 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 13307 | OS_GETLOADAVG_METHODDEF |
| 13308 | OS_URANDOM_METHODDEF |
| 13309 | OS_SETRESUID_METHODDEF |
| 13310 | OS_SETRESGID_METHODDEF |
| 13311 | OS_GETRESUID_METHODDEF |
| 13312 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 13313 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13314 | OS_GETXATTR_METHODDEF |
| 13315 | OS_SETXATTR_METHODDEF |
| 13316 | OS_REMOVEXATTR_METHODDEF |
| 13317 | OS_LISTXATTR_METHODDEF |
| 13318 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13319 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 13320 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 13321 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13322 | OS_CPU_COUNT_METHODDEF |
| 13323 | OS_GET_INHERITABLE_METHODDEF |
| 13324 | OS_SET_INHERITABLE_METHODDEF |
| 13325 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 13326 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13327 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13328 | OS_GET_BLOCKING_METHODDEF |
| 13329 | OS_SET_BLOCKING_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13330 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13331 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13332 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13333 | OS_GETRANDOM_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13334 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13335 | }; |
| 13336 | |
| 13337 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13338 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13339 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13340 | enable_symlink() |
| 13341 | { |
| 13342 | HANDLE tok; |
| 13343 | TOKEN_PRIVILEGES tok_priv; |
| 13344 | LUID luid; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13345 | |
| 13346 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13347 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13348 | |
| 13349 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13350 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13351 | |
| 13352 | tok_priv.PrivilegeCount = 1; |
| 13353 | tok_priv.Privileges[0].Luid = luid; |
| 13354 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 13355 | |
| 13356 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 13357 | sizeof(TOKEN_PRIVILEGES), |
| 13358 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13359 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13360 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13361 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 13362 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13363 | } |
| 13364 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 13365 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13366 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13367 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13368 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13369 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13370 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13371 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13372 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13373 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13374 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13375 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13376 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13377 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13378 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13379 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13380 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13381 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13382 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13383 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13384 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13385 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13386 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13387 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13388 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13389 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13390 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13391 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13392 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13393 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13394 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13395 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13396 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13397 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13398 | #endif |
| 13399 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13400 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13401 | #endif |
| 13402 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13403 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13404 | #endif |
| 13405 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13406 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13407 | #endif |
| 13408 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13409 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13410 | #endif |
| 13411 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13412 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13413 | #endif |
| 13414 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13415 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13416 | #endif |
| 13417 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13418 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13419 | #endif |
| 13420 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13421 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13422 | #endif |
| 13423 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13424 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13425 | #endif |
| 13426 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13427 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13428 | #endif |
| 13429 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13430 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13431 | #endif |
| 13432 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13433 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13434 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13435 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13436 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13437 | #endif |
| 13438 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13439 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13440 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13441 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13442 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13443 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13444 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13445 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13446 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13447 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13448 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13449 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13450 | #endif |
| 13451 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13452 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13453 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13454 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13455 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13456 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13457 | #endif |
| 13458 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13459 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13460 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13461 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13462 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13463 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13464 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13465 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13466 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 13467 | #ifdef O_TMPFILE |
| 13468 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 13469 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13470 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13471 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13472 | #endif |
| 13473 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13474 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13475 | #endif |
| 13476 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13477 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13478 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13479 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13480 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13481 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13482 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13483 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13484 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13485 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13486 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13487 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13488 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13489 | #endif |
| 13490 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13491 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13492 | #endif |
| 13493 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13494 | /* MS Windows */ |
| 13495 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13496 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13497 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13498 | #endif |
| 13499 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13500 | /* Optimize for short life (keep in memory). */ |
| 13501 | /* 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] | 13502 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13503 | #endif |
| 13504 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13505 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13506 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13507 | #endif |
| 13508 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13509 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13510 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13511 | #endif |
| 13512 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13513 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13514 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13515 | #endif |
| 13516 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13517 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13518 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13519 | /* Send a SIGIO signal whenever input or output |
| 13520 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13521 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13522 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13523 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13524 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13525 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13526 | #endif |
| 13527 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13528 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13529 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13530 | #endif |
| 13531 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13532 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13533 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13534 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13535 | #ifdef O_NOLINKS |
| 13536 | /* 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] | 13537 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13538 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13539 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13540 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13541 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13542 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 13543 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13544 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13545 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13546 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13547 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13548 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13549 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13550 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13551 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13552 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13553 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13554 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13555 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13556 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13557 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13558 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13559 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13560 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13561 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13562 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13563 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13564 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13565 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13566 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13567 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13568 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13569 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13570 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13571 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13572 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13573 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13574 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13575 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13576 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13577 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13578 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13579 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13580 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13581 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13582 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13583 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13584 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13585 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13586 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13587 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13588 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13589 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13590 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13591 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13592 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13593 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13594 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13595 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13596 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 13597 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13598 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13599 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13600 | #endif /* ST_RDONLY */ |
| 13601 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13602 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13603 | #endif /* ST_NOSUID */ |
| 13604 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 13605 | /* GNU extensions */ |
| 13606 | #ifdef ST_NODEV |
| 13607 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 13608 | #endif /* ST_NODEV */ |
| 13609 | #ifdef ST_NOEXEC |
| 13610 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 13611 | #endif /* ST_NOEXEC */ |
| 13612 | #ifdef ST_SYNCHRONOUS |
| 13613 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 13614 | #endif /* ST_SYNCHRONOUS */ |
| 13615 | #ifdef ST_MANDLOCK |
| 13616 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 13617 | #endif /* ST_MANDLOCK */ |
| 13618 | #ifdef ST_WRITE |
| 13619 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 13620 | #endif /* ST_WRITE */ |
| 13621 | #ifdef ST_APPEND |
| 13622 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 13623 | #endif /* ST_APPEND */ |
| 13624 | #ifdef ST_NOATIME |
| 13625 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 13626 | #endif /* ST_NOATIME */ |
| 13627 | #ifdef ST_NODIRATIME |
| 13628 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 13629 | #endif /* ST_NODIRATIME */ |
| 13630 | #ifdef ST_RELATIME |
| 13631 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 13632 | #endif /* ST_RELATIME */ |
| 13633 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13634 | /* FreeBSD sendfile() constants */ |
| 13635 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13636 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13637 | #endif |
| 13638 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13639 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13640 | #endif |
| 13641 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13642 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13643 | #endif |
| 13644 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13645 | /* constants for posix_fadvise */ |
| 13646 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13647 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13648 | #endif |
| 13649 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13650 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13651 | #endif |
| 13652 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13653 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13654 | #endif |
| 13655 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13656 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13657 | #endif |
| 13658 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13659 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13660 | #endif |
| 13661 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13662 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13663 | #endif |
| 13664 | |
| 13665 | /* constants for waitid */ |
| 13666 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13667 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 13668 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 13669 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13670 | #endif |
| 13671 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13672 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13673 | #endif |
| 13674 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13675 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13676 | #endif |
| 13677 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13678 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13679 | #endif |
| 13680 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13681 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13682 | #endif |
| 13683 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13684 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13685 | #endif |
| 13686 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13687 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13688 | #endif |
| 13689 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13690 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13691 | #endif |
| 13692 | |
| 13693 | /* constants for lockf */ |
| 13694 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13695 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13696 | #endif |
| 13697 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13698 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13699 | #endif |
| 13700 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13701 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13702 | #endif |
| 13703 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13704 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13705 | #endif |
| 13706 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13707 | #ifdef RWF_DSYNC |
| 13708 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 13709 | #endif |
| 13710 | #ifdef RWF_HIPRI |
| 13711 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 13712 | #endif |
| 13713 | #ifdef RWF_SYNC |
| 13714 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 13715 | #endif |
| 13716 | #ifdef RWF_NOWAIT |
| 13717 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 13718 | #endif |
| 13719 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13720 | /* constants for posix_spawn */ |
| 13721 | #ifdef HAVE_POSIX_SPAWN |
| 13722 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 13723 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 13724 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 13725 | #endif |
| 13726 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 13727 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13728 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 13729 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 13730 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 13731 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 13732 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 13733 | #endif |
| 13734 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13735 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13736 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13737 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13738 | #endif |
| 13739 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13740 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13741 | #endif |
| 13742 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13743 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13744 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13745 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 13746 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13747 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13748 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13749 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13750 | #endif |
| 13751 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13752 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13753 | #endif |
| 13754 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13755 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13756 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13757 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13758 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13759 | #endif |
| 13760 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13761 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13762 | #endif |
| 13763 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13764 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13765 | #endif |
| 13766 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13767 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13768 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13769 | #endif |
| 13770 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 13771 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13772 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 13773 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 13774 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 13775 | #endif |
| 13776 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13777 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13778 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13779 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13780 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13781 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13782 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13783 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13784 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13785 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13786 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13787 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13788 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13789 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13790 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13791 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13792 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13793 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13794 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13795 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13796 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13797 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 13798 | #if HAVE_DECL_RTLD_MEMBER |
| 13799 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 13800 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13801 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13802 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13803 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 13804 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 13805 | #endif |
| 13806 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 13807 | #if defined(__APPLE__) |
| 13808 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 13809 | #endif |
| 13810 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13811 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13812 | } |
| 13813 | |
| 13814 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 13815 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13816 | PyModuleDef_HEAD_INIT, |
| 13817 | MODNAME, |
| 13818 | posix__doc__, |
| 13819 | -1, |
| 13820 | posix_methods, |
| 13821 | NULL, |
| 13822 | NULL, |
| 13823 | NULL, |
| 13824 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 13825 | }; |
| 13826 | |
| 13827 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 13828 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13829 | |
| 13830 | #ifdef HAVE_FACCESSAT |
| 13831 | "HAVE_FACCESSAT", |
| 13832 | #endif |
| 13833 | |
| 13834 | #ifdef HAVE_FCHDIR |
| 13835 | "HAVE_FCHDIR", |
| 13836 | #endif |
| 13837 | |
| 13838 | #ifdef HAVE_FCHMOD |
| 13839 | "HAVE_FCHMOD", |
| 13840 | #endif |
| 13841 | |
| 13842 | #ifdef HAVE_FCHMODAT |
| 13843 | "HAVE_FCHMODAT", |
| 13844 | #endif |
| 13845 | |
| 13846 | #ifdef HAVE_FCHOWN |
| 13847 | "HAVE_FCHOWN", |
| 13848 | #endif |
| 13849 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 13850 | #ifdef HAVE_FCHOWNAT |
| 13851 | "HAVE_FCHOWNAT", |
| 13852 | #endif |
| 13853 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13854 | #ifdef HAVE_FEXECVE |
| 13855 | "HAVE_FEXECVE", |
| 13856 | #endif |
| 13857 | |
| 13858 | #ifdef HAVE_FDOPENDIR |
| 13859 | "HAVE_FDOPENDIR", |
| 13860 | #endif |
| 13861 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 13862 | #ifdef HAVE_FPATHCONF |
| 13863 | "HAVE_FPATHCONF", |
| 13864 | #endif |
| 13865 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13866 | #ifdef HAVE_FSTATAT |
| 13867 | "HAVE_FSTATAT", |
| 13868 | #endif |
| 13869 | |
| 13870 | #ifdef HAVE_FSTATVFS |
| 13871 | "HAVE_FSTATVFS", |
| 13872 | #endif |
| 13873 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 13874 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 13875 | "HAVE_FTRUNCATE", |
| 13876 | #endif |
| 13877 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13878 | #ifdef HAVE_FUTIMENS |
| 13879 | "HAVE_FUTIMENS", |
| 13880 | #endif |
| 13881 | |
| 13882 | #ifdef HAVE_FUTIMES |
| 13883 | "HAVE_FUTIMES", |
| 13884 | #endif |
| 13885 | |
| 13886 | #ifdef HAVE_FUTIMESAT |
| 13887 | "HAVE_FUTIMESAT", |
| 13888 | #endif |
| 13889 | |
| 13890 | #ifdef HAVE_LINKAT |
| 13891 | "HAVE_LINKAT", |
| 13892 | #endif |
| 13893 | |
| 13894 | #ifdef HAVE_LCHFLAGS |
| 13895 | "HAVE_LCHFLAGS", |
| 13896 | #endif |
| 13897 | |
| 13898 | #ifdef HAVE_LCHMOD |
| 13899 | "HAVE_LCHMOD", |
| 13900 | #endif |
| 13901 | |
| 13902 | #ifdef HAVE_LCHOWN |
| 13903 | "HAVE_LCHOWN", |
| 13904 | #endif |
| 13905 | |
| 13906 | #ifdef HAVE_LSTAT |
| 13907 | "HAVE_LSTAT", |
| 13908 | #endif |
| 13909 | |
| 13910 | #ifdef HAVE_LUTIMES |
| 13911 | "HAVE_LUTIMES", |
| 13912 | #endif |
| 13913 | |
| 13914 | #ifdef HAVE_MKDIRAT |
| 13915 | "HAVE_MKDIRAT", |
| 13916 | #endif |
| 13917 | |
| 13918 | #ifdef HAVE_MKFIFOAT |
| 13919 | "HAVE_MKFIFOAT", |
| 13920 | #endif |
| 13921 | |
| 13922 | #ifdef HAVE_MKNODAT |
| 13923 | "HAVE_MKNODAT", |
| 13924 | #endif |
| 13925 | |
| 13926 | #ifdef HAVE_OPENAT |
| 13927 | "HAVE_OPENAT", |
| 13928 | #endif |
| 13929 | |
| 13930 | #ifdef HAVE_READLINKAT |
| 13931 | "HAVE_READLINKAT", |
| 13932 | #endif |
| 13933 | |
| 13934 | #ifdef HAVE_RENAMEAT |
| 13935 | "HAVE_RENAMEAT", |
| 13936 | #endif |
| 13937 | |
| 13938 | #ifdef HAVE_SYMLINKAT |
| 13939 | "HAVE_SYMLINKAT", |
| 13940 | #endif |
| 13941 | |
| 13942 | #ifdef HAVE_UNLINKAT |
| 13943 | "HAVE_UNLINKAT", |
| 13944 | #endif |
| 13945 | |
| 13946 | #ifdef HAVE_UTIMENSAT |
| 13947 | "HAVE_UTIMENSAT", |
| 13948 | #endif |
| 13949 | |
| 13950 | #ifdef MS_WINDOWS |
| 13951 | "MS_WINDOWS", |
| 13952 | #endif |
| 13953 | |
| 13954 | NULL |
| 13955 | }; |
| 13956 | |
| 13957 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 13958 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 13959 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13960 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13961 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13962 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 13963 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13964 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13965 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13966 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13967 | #endif |
| 13968 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13969 | m = PyModule_Create(&posixmodule); |
| 13970 | if (m == NULL) |
| 13971 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13972 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13973 | /* Initialize environ dictionary */ |
| 13974 | v = convertenviron(); |
| 13975 | Py_XINCREF(v); |
| 13976 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 13977 | return NULL; |
| 13978 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13979 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13980 | if (all_ins(m)) |
| 13981 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13982 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13983 | if (setup_confname_tables(m)) |
| 13984 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 13985 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13986 | Py_INCREF(PyExc_OSError); |
| 13987 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 13988 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 13989 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13990 | if (posix_putenv_garbage == NULL) |
| 13991 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 13992 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 13993 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13994 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13995 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 13996 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13997 | WaitidResultType = PyStructSequence_NewType(&waitid_result_desc); |
| 13998 | if (WaitidResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13999 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14000 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14001 | #endif |
| 14002 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 14003 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14004 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 14005 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 14006 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14007 | StatResultType = PyStructSequence_NewType(&stat_result_desc); |
| 14008 | if (StatResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14009 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14010 | } |
| 14011 | structseq_new = StatResultType->tp_new; |
| 14012 | StatResultType->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14013 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 14014 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14015 | StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc); |
| 14016 | if (StatVFSResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14017 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14018 | } |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14019 | #ifdef NEED_TICKS_PER_SECOND |
| 14020 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14021 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14022 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14023 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14024 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14025 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14026 | # endif |
| 14027 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14028 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 14029 | #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] | 14030 | sched_param_desc.name = MODNAME ".sched_param"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14031 | SchedParamType = PyStructSequence_NewType(&sched_param_desc); |
| 14032 | if (SchedParamType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14033 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14034 | } |
| 14035 | SchedParamType->tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14036 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14037 | |
| 14038 | /* initialize TerminalSize_info */ |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14039 | TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc); |
| 14040 | if (TerminalSizeType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14041 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14042 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14043 | |
| 14044 | /* initialize scandir types */ |
| 14045 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 14046 | return NULL; |
| 14047 | if (PyType_Ready(&DirEntryType) < 0) |
| 14048 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14049 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14050 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14051 | Py_INCREF((PyObject*) WaitidResultType); |
| 14052 | PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14053 | #endif |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14054 | Py_INCREF((PyObject*) StatResultType); |
| 14055 | PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType); |
| 14056 | Py_INCREF((PyObject*) StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14057 | PyModule_AddObject(m, "statvfs_result", |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14058 | (PyObject*) StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14059 | |
| 14060 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14061 | Py_INCREF(SchedParamType); |
| 14062 | PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14063 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14064 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14065 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14066 | TimesResultType = PyStructSequence_NewType(×_result_desc); |
| 14067 | if (TimesResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14068 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14069 | } |
| 14070 | PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14071 | |
| 14072 | uname_result_desc.name = MODNAME ".uname_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14073 | UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
| 14074 | if (UnameResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14075 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14076 | } |
| 14077 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14078 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14079 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14080 | /* |
| 14081 | * Step 2 of weak-linking support on Mac OS X. |
| 14082 | * |
| 14083 | * The code below removes functions that are not available on the |
| 14084 | * currently active platform. |
| 14085 | * |
| 14086 | * 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] | 14087 | * 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] | 14088 | * OSX 10.4. |
| 14089 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14090 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14091 | if (fstatvfs == NULL) { |
| 14092 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 14093 | return NULL; |
| 14094 | } |
| 14095 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14096 | #endif /* HAVE_FSTATVFS */ |
| 14097 | |
| 14098 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14099 | if (statvfs == NULL) { |
| 14100 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 14101 | return NULL; |
| 14102 | } |
| 14103 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14104 | #endif /* HAVE_STATVFS */ |
| 14105 | |
| 14106 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14107 | if (lchown == NULL) { |
| 14108 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 14109 | return NULL; |
| 14110 | } |
| 14111 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14112 | #endif /* HAVE_LCHOWN */ |
| 14113 | |
| 14114 | |
| 14115 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14116 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14117 | Py_INCREF(TerminalSizeType); |
| 14118 | PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14119 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 14120 | billion = PyLong_FromLong(1000000000); |
| 14121 | if (!billion) |
| 14122 | return NULL; |
| 14123 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14124 | /* suppress "function not used" warnings */ |
| 14125 | { |
| 14126 | int ignored; |
| 14127 | fd_specified("", -1); |
| 14128 | follow_symlinks_specified("", 1); |
| 14129 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 14130 | dir_fd_converter(Py_None, &ignored); |
| 14131 | dir_fd_unavailable(Py_None, &ignored); |
| 14132 | } |
| 14133 | |
| 14134 | /* |
| 14135 | * provide list of locally available functions |
| 14136 | * so os.py can populate support_* lists |
| 14137 | */ |
| 14138 | list = PyList_New(0); |
| 14139 | if (!list) |
| 14140 | return NULL; |
| 14141 | for (trace = have_functions; *trace; trace++) { |
| 14142 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 14143 | if (!unicode) |
| 14144 | return NULL; |
| 14145 | if (PyList_Append(list, unicode)) |
| 14146 | return NULL; |
| 14147 | Py_DECREF(unicode); |
| 14148 | } |
| 14149 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 14150 | |
| 14151 | Py_INCREF((PyObject *) &DirEntryType); |
Brett Cannon | a32c4d0 | 2016-06-24 14:14:44 -0700 | [diff] [blame] | 14152 | PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14153 | |
| 14154 | initialized = 1; |
| 14155 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14156 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14157 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14158 | |
| 14159 | #ifdef __cplusplus |
| 14160 | } |
| 14161 | #endif |