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 | |
| 370 | #ifdef HAVE_FORK |
| 371 | static void |
| 372 | run_at_forkers(PyObject *lst, int reverse) |
| 373 | { |
| 374 | Py_ssize_t i; |
| 375 | PyObject *cpy; |
| 376 | |
| 377 | if (lst != NULL) { |
| 378 | assert(PyList_CheckExact(lst)); |
| 379 | |
| 380 | /* Use a list copy in case register_at_fork() is called from |
| 381 | * one of the callbacks. |
| 382 | */ |
| 383 | cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst)); |
| 384 | if (cpy == NULL) |
| 385 | PyErr_WriteUnraisable(lst); |
| 386 | else { |
| 387 | if (reverse) |
| 388 | PyList_Reverse(cpy); |
| 389 | for (i = 0; i < PyList_GET_SIZE(cpy); i++) { |
| 390 | PyObject *func, *res; |
| 391 | func = PyList_GET_ITEM(cpy, i); |
| 392 | res = PyObject_CallObject(func, NULL); |
| 393 | if (res == NULL) |
| 394 | PyErr_WriteUnraisable(func); |
| 395 | else |
| 396 | Py_DECREF(res); |
| 397 | } |
| 398 | Py_DECREF(cpy); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void |
| 404 | PyOS_BeforeFork(void) |
| 405 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 406 | run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 407 | |
| 408 | _PyImport_AcquireLock(); |
| 409 | } |
| 410 | |
| 411 | void |
| 412 | PyOS_AfterFork_Parent(void) |
| 413 | { |
| 414 | if (_PyImport_ReleaseLock() <= 0) |
| 415 | Py_FatalError("failed releasing import lock after fork"); |
| 416 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 417 | run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void |
| 421 | PyOS_AfterFork_Child(void) |
| 422 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 423 | _PyGILState_Reinit(); |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 424 | _PyInterpreterState_DeleteExceptMain(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 425 | PyEval_ReInitThreads(); |
| 426 | _PyImport_ReInitLock(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 427 | _PySignal_AfterFork(); |
| 428 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 429 | run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static int |
| 433 | register_at_forker(PyObject **lst, PyObject *func) |
| 434 | { |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 435 | if (func == NULL) /* nothing to register? do nothing. */ |
| 436 | return 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 437 | if (*lst == NULL) { |
| 438 | *lst = PyList_New(0); |
| 439 | if (*lst == NULL) |
| 440 | return -1; |
| 441 | } |
| 442 | return PyList_Append(*lst, func); |
| 443 | } |
| 444 | #endif |
| 445 | |
| 446 | /* Legacy wrapper */ |
| 447 | void |
| 448 | PyOS_AfterFork(void) |
| 449 | { |
| 450 | #ifdef HAVE_FORK |
| 451 | PyOS_AfterFork_Child(); |
| 452 | #endif |
| 453 | } |
| 454 | |
| 455 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 456 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 457 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 458 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 459 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 460 | ULONG, struct _Py_stat_struct *); |
| 461 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 462 | |
| 463 | #ifdef MS_WINDOWS |
| 464 | static int |
| 465 | win32_warn_bytes_api() |
| 466 | { |
| 467 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 468 | "The Windows bytes API has been deprecated, " |
| 469 | "use Unicode filenames instead", |
| 470 | 1); |
| 471 | } |
| 472 | #endif |
| 473 | |
| 474 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 475 | #ifndef MS_WINDOWS |
| 476 | PyObject * |
| 477 | _PyLong_FromUid(uid_t uid) |
| 478 | { |
| 479 | if (uid == (uid_t)-1) |
| 480 | return PyLong_FromLong(-1); |
| 481 | return PyLong_FromUnsignedLong(uid); |
| 482 | } |
| 483 | |
| 484 | PyObject * |
| 485 | _PyLong_FromGid(gid_t gid) |
| 486 | { |
| 487 | if (gid == (gid_t)-1) |
| 488 | return PyLong_FromLong(-1); |
| 489 | return PyLong_FromUnsignedLong(gid); |
| 490 | } |
| 491 | |
| 492 | int |
| 493 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 494 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 495 | uid_t uid; |
| 496 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 497 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 498 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 499 | unsigned long uresult; |
| 500 | |
| 501 | index = PyNumber_Index(obj); |
| 502 | if (index == NULL) { |
| 503 | PyErr_Format(PyExc_TypeError, |
| 504 | "uid should be integer, not %.200s", |
| 505 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 506 | return 0; |
| 507 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 508 | |
| 509 | /* |
| 510 | * Handling uid_t is complicated for two reasons: |
| 511 | * * Although uid_t is (always?) unsigned, it still |
| 512 | * accepts -1. |
| 513 | * * We don't know its size in advance--it may be |
| 514 | * bigger than an int, or it may be smaller than |
| 515 | * a long. |
| 516 | * |
| 517 | * So a bit of defensive programming is in order. |
| 518 | * Start with interpreting the value passed |
| 519 | * in as a signed long and see if it works. |
| 520 | */ |
| 521 | |
| 522 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 523 | |
| 524 | if (!overflow) { |
| 525 | uid = (uid_t)result; |
| 526 | |
| 527 | if (result == -1) { |
| 528 | if (PyErr_Occurred()) |
| 529 | goto fail; |
| 530 | /* It's a legitimate -1, we're done. */ |
| 531 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 532 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 533 | |
| 534 | /* Any other negative number is disallowed. */ |
| 535 | if (result < 0) |
| 536 | goto underflow; |
| 537 | |
| 538 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 539 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 540 | (long)uid != result) |
| 541 | goto underflow; |
| 542 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 543 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 544 | |
| 545 | if (overflow < 0) |
| 546 | goto underflow; |
| 547 | |
| 548 | /* |
| 549 | * Okay, the value overflowed a signed long. If it |
| 550 | * fits in an *unsigned* long, it may still be okay, |
| 551 | * as uid_t may be unsigned long on this platform. |
| 552 | */ |
| 553 | uresult = PyLong_AsUnsignedLong(index); |
| 554 | if (PyErr_Occurred()) { |
| 555 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 556 | goto overflow; |
| 557 | goto fail; |
| 558 | } |
| 559 | |
| 560 | uid = (uid_t)uresult; |
| 561 | |
| 562 | /* |
| 563 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 564 | * but this value would get interpreted as (uid_t)-1 by chown |
| 565 | * and its siblings. That's not what the user meant! So we |
| 566 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 567 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 568 | */ |
| 569 | if (uid == (uid_t)-1) |
| 570 | goto overflow; |
| 571 | |
| 572 | /* Ensure the value wasn't truncated. */ |
| 573 | if (sizeof(uid_t) < sizeof(long) && |
| 574 | (unsigned long)uid != uresult) |
| 575 | goto overflow; |
| 576 | /* fallthrough */ |
| 577 | |
| 578 | success: |
| 579 | Py_DECREF(index); |
| 580 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 581 | return 1; |
| 582 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 583 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 584 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 585 | "uid is less than minimum"); |
| 586 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 587 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 588 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 589 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 590 | "uid is greater than maximum"); |
| 591 | /* fallthrough */ |
| 592 | |
| 593 | fail: |
| 594 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | int |
| 599 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 600 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 601 | gid_t gid; |
| 602 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 603 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 604 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 605 | unsigned long uresult; |
| 606 | |
| 607 | index = PyNumber_Index(obj); |
| 608 | if (index == NULL) { |
| 609 | PyErr_Format(PyExc_TypeError, |
| 610 | "gid should be integer, not %.200s", |
| 611 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 612 | return 0; |
| 613 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 614 | |
| 615 | /* |
| 616 | * Handling gid_t is complicated for two reasons: |
| 617 | * * Although gid_t is (always?) unsigned, it still |
| 618 | * accepts -1. |
| 619 | * * We don't know its size in advance--it may be |
| 620 | * bigger than an int, or it may be smaller than |
| 621 | * a long. |
| 622 | * |
| 623 | * So a bit of defensive programming is in order. |
| 624 | * Start with interpreting the value passed |
| 625 | * in as a signed long and see if it works. |
| 626 | */ |
| 627 | |
| 628 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 629 | |
| 630 | if (!overflow) { |
| 631 | gid = (gid_t)result; |
| 632 | |
| 633 | if (result == -1) { |
| 634 | if (PyErr_Occurred()) |
| 635 | goto fail; |
| 636 | /* It's a legitimate -1, we're done. */ |
| 637 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 638 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 639 | |
| 640 | /* Any other negative number is disallowed. */ |
| 641 | if (result < 0) { |
| 642 | goto underflow; |
| 643 | } |
| 644 | |
| 645 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 646 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 647 | (long)gid != result) |
| 648 | goto underflow; |
| 649 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 650 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 651 | |
| 652 | if (overflow < 0) |
| 653 | goto underflow; |
| 654 | |
| 655 | /* |
| 656 | * Okay, the value overflowed a signed long. If it |
| 657 | * fits in an *unsigned* long, it may still be okay, |
| 658 | * as gid_t may be unsigned long on this platform. |
| 659 | */ |
| 660 | uresult = PyLong_AsUnsignedLong(index); |
| 661 | if (PyErr_Occurred()) { |
| 662 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 663 | goto overflow; |
| 664 | goto fail; |
| 665 | } |
| 666 | |
| 667 | gid = (gid_t)uresult; |
| 668 | |
| 669 | /* |
| 670 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 671 | * but this value would get interpreted as (gid_t)-1 by chown |
| 672 | * and its siblings. That's not what the user meant! So we |
| 673 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 674 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 675 | */ |
| 676 | if (gid == (gid_t)-1) |
| 677 | goto overflow; |
| 678 | |
| 679 | /* Ensure the value wasn't truncated. */ |
| 680 | if (sizeof(gid_t) < sizeof(long) && |
| 681 | (unsigned long)gid != uresult) |
| 682 | goto overflow; |
| 683 | /* fallthrough */ |
| 684 | |
| 685 | success: |
| 686 | Py_DECREF(index); |
| 687 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 688 | return 1; |
| 689 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 690 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 691 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 692 | "gid is less than minimum"); |
| 693 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 694 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 695 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 696 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 697 | "gid is greater than maximum"); |
| 698 | /* fallthrough */ |
| 699 | |
| 700 | fail: |
| 701 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | #endif /* MS_WINDOWS */ |
| 705 | |
| 706 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 707 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 708 | |
| 709 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 710 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 711 | static int |
| 712 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 713 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 714 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 715 | if (PyErr_Occurred()) |
| 716 | return 0; |
| 717 | return 1; |
| 718 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 719 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 720 | |
| 721 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 722 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 723 | /* |
| 724 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 725 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 726 | * which doesn't play nicely with all the initializer lines in this file that |
| 727 | * look like this: |
| 728 | * int dir_fd = DEFAULT_DIR_FD; |
| 729 | */ |
| 730 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 731 | #else |
| 732 | #define DEFAULT_DIR_FD (-100) |
| 733 | #endif |
| 734 | |
| 735 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 736 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 737 | { |
| 738 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 739 | long long_value; |
| 740 | |
| 741 | PyObject *index = PyNumber_Index(o); |
| 742 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 743 | return 0; |
| 744 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 745 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 746 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 747 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 748 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 749 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 750 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 751 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 752 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 753 | return 0; |
| 754 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 755 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 756 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 757 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 758 | return 0; |
| 759 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 760 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 761 | *p = (int)long_value; |
| 762 | return 1; |
| 763 | } |
| 764 | |
| 765 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 766 | dir_fd_converter(PyObject *o, void *p) |
| 767 | { |
| 768 | if (o == Py_None) { |
| 769 | *(int *)p = DEFAULT_DIR_FD; |
| 770 | return 1; |
| 771 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 772 | else if (PyIndex_Check(o)) { |
| 773 | return _fd_converter(o, (int *)p); |
| 774 | } |
| 775 | else { |
| 776 | PyErr_Format(PyExc_TypeError, |
| 777 | "argument should be integer or None, not %.200s", |
| 778 | Py_TYPE(o)->tp_name); |
| 779 | return 0; |
| 780 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 784 | /* |
| 785 | * A PyArg_ParseTuple "converter" function |
| 786 | * that handles filesystem paths in the manner |
| 787 | * preferred by the os module. |
| 788 | * |
| 789 | * path_converter accepts (Unicode) strings and their |
| 790 | * subclasses, and bytes and their subclasses. What |
| 791 | * it does with the argument depends on the platform: |
| 792 | * |
| 793 | * * On Windows, if we get a (Unicode) string we |
| 794 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 795 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 796 | * |
| 797 | * * On all other platforms, strings are encoded |
| 798 | * to bytes using PyUnicode_FSConverter, then we |
| 799 | * extract the char * from the bytes object and |
| 800 | * return that. |
| 801 | * |
| 802 | * path_converter also optionally accepts signed |
| 803 | * integers (representing open file descriptors) instead |
| 804 | * of path strings. |
| 805 | * |
| 806 | * Input fields: |
| 807 | * path.nullable |
| 808 | * If nonzero, the path is permitted to be None. |
| 809 | * path.allow_fd |
| 810 | * If nonzero, the path is permitted to be a file handle |
| 811 | * (a signed int) instead of a string. |
| 812 | * path.function_name |
| 813 | * If non-NULL, path_converter will use that as the name |
| 814 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 815 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 816 | * path.argument_name |
| 817 | * If non-NULL, path_converter will use that as the name |
| 818 | * of the parameter in error messages. |
| 819 | * (If path.argument_name is NULL it uses "path".) |
| 820 | * |
| 821 | * Output fields: |
| 822 | * path.wide |
| 823 | * Points to the path if it was expressed as Unicode |
| 824 | * and was not encoded. (Only used on Windows.) |
| 825 | * path.narrow |
| 826 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 827 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 828 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 829 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 830 | * path.fd |
| 831 | * Contains a file descriptor if path.accept_fd was true |
| 832 | * and the caller provided a signed integer instead of any |
| 833 | * sort of string. |
| 834 | * |
| 835 | * WARNING: if your "path" parameter is optional, and is |
| 836 | * unspecified, path_converter will never get called. |
| 837 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 838 | * yourself! |
| 839 | * path.length |
| 840 | * The length of the path in characters, if specified as |
| 841 | * a string. |
| 842 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 843 | * The original object passed in (if get a PathLike object, |
| 844 | * the result of PyOS_FSPath() is treated as the original object). |
| 845 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 846 | * path.cleanup |
| 847 | * For internal use only. May point to a temporary object. |
| 848 | * (Pay no attention to the man behind the curtain.) |
| 849 | * |
| 850 | * At most one of path.wide or path.narrow will be non-NULL. |
| 851 | * If path was None and path.nullable was set, |
| 852 | * or if path was an integer and path.allow_fd was set, |
| 853 | * both path.wide and path.narrow will be NULL |
| 854 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 855 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 856 | * path_converter takes care to not write to the path_t |
| 857 | * unless it's successful. However it must reset the |
| 858 | * "cleanup" field each time it's called. |
| 859 | * |
| 860 | * Use as follows: |
| 861 | * path_t path; |
| 862 | * memset(&path, 0, sizeof(path)); |
| 863 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 864 | * // ... use values from path ... |
| 865 | * path_cleanup(&path); |
| 866 | * |
| 867 | * (Note that if PyArg_Parse fails you don't need to call |
| 868 | * path_cleanup(). However it is safe to do so.) |
| 869 | */ |
| 870 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 871 | const char *function_name; |
| 872 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 873 | int nullable; |
| 874 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 875 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 876 | #ifdef MS_WINDOWS |
| 877 | BOOL narrow; |
| 878 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 879 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 880 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 881 | int fd; |
| 882 | Py_ssize_t length; |
| 883 | PyObject *object; |
| 884 | PyObject *cleanup; |
| 885 | } path_t; |
| 886 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 887 | #ifdef MS_WINDOWS |
| 888 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 889 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 890 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 891 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 892 | {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] | 893 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 894 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 895 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 896 | path_cleanup(path_t *path) |
| 897 | { |
| 898 | Py_CLEAR(path->object); |
| 899 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 903 | path_converter(PyObject *o, void *p) |
| 904 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 905 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 906 | PyObject *bytes = NULL; |
| 907 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 908 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 909 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 910 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 911 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 912 | const wchar_t *wide; |
| 913 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 914 | |
| 915 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 916 | PyErr_Format(exc, "%s%s" fmt, \ |
| 917 | path->function_name ? path->function_name : "", \ |
| 918 | path->function_name ? ": " : "", \ |
| 919 | path->argument_name ? path->argument_name : "path") |
| 920 | |
| 921 | /* Py_CLEANUP_SUPPORTED support */ |
| 922 | if (o == NULL) { |
| 923 | path_cleanup(path); |
| 924 | return 1; |
| 925 | } |
| 926 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 927 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 928 | path->object = path->cleanup = NULL; |
| 929 | /* path->object owns a reference to the original object */ |
| 930 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 931 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 932 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 933 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 934 | #ifdef MS_WINDOWS |
| 935 | path->narrow = FALSE; |
| 936 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 937 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 938 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 939 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 940 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 943 | /* Only call this here so that we don't treat the return value of |
| 944 | os.fspath() as an fd or buffer. */ |
| 945 | is_index = path->allow_fd && PyIndex_Check(o); |
| 946 | is_buffer = PyObject_CheckBuffer(o); |
| 947 | is_bytes = PyBytes_Check(o); |
| 948 | is_unicode = PyUnicode_Check(o); |
| 949 | |
| 950 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 951 | /* Inline PyOS_FSPath() for better error messages. */ |
| 952 | _Py_IDENTIFIER(__fspath__); |
| 953 | PyObject *func = NULL; |
| 954 | |
| 955 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 956 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 957 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 958 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 959 | /* still owns a reference to the original object */ |
| 960 | Py_DECREF(o); |
| 961 | o = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 962 | Py_DECREF(func); |
| 963 | if (NULL == o) { |
| 964 | goto error_exit; |
| 965 | } |
| 966 | else if (PyUnicode_Check(o)) { |
| 967 | is_unicode = 1; |
| 968 | } |
| 969 | else if (PyBytes_Check(o)) { |
| 970 | is_bytes = 1; |
| 971 | } |
| 972 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 973 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
| 977 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 978 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 979 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 980 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 981 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 982 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 983 | if (length > 32767) { |
| 984 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
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 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 987 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 988 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 989 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 990 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 991 | |
| 992 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 993 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 994 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 995 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 996 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 997 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 998 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 999 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1000 | #endif |
| 1001 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1002 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1003 | bytes = o; |
| 1004 | Py_INCREF(bytes); |
| 1005 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1006 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1007 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1008 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1009 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1010 | "%s%s%s should be %s, not %.200s", |
| 1011 | path->function_name ? path->function_name : "", |
| 1012 | path->function_name ? ": " : "", |
| 1013 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1014 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1015 | "integer or None" : |
| 1016 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1017 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1018 | "string, bytes or os.PathLike", |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1019 | Py_TYPE(o)->tp_name)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1020 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1021 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1022 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1023 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1024 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1025 | } |
| 1026 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1027 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1028 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1029 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1030 | } |
| 1031 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1032 | #ifdef MS_WINDOWS |
| 1033 | path->narrow = FALSE; |
| 1034 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1035 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1036 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1037 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1038 | } |
| 1039 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1040 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1041 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1042 | path->function_name ? path->function_name : "", |
| 1043 | path->function_name ? ": " : "", |
| 1044 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1045 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1046 | "integer or None" : |
| 1047 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1048 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1049 | "string, bytes or os.PathLike", |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1050 | Py_TYPE(o)->tp_name); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1051 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1054 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1055 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1056 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1057 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1058 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1061 | #ifdef MS_WINDOWS |
| 1062 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1063 | narrow, |
| 1064 | length |
| 1065 | ); |
| 1066 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1067 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1070 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1071 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1072 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1073 | } |
| 1074 | if (length > 32767) { |
| 1075 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
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 (wcslen(wide) != length) { |
| 1079 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
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 | path->wide = wide; |
| 1083 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1084 | path->cleanup = wo; |
| 1085 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1086 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1087 | path->wide = NULL; |
| 1088 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1089 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1090 | /* Still a reference owned by path->object, don't have to |
| 1091 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1092 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1093 | } |
| 1094 | else { |
| 1095 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1096 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1097 | #endif |
| 1098 | path->fd = -1; |
| 1099 | |
| 1100 | success_exit: |
| 1101 | path->length = length; |
| 1102 | path->object = o; |
| 1103 | return Py_CLEANUP_SUPPORTED; |
| 1104 | |
| 1105 | error_exit: |
| 1106 | Py_XDECREF(o); |
| 1107 | Py_XDECREF(bytes); |
| 1108 | #ifdef MS_WINDOWS |
| 1109 | Py_XDECREF(wo); |
| 1110 | #endif |
| 1111 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1115 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1116 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1117 | PyErr_Format(PyExc_NotImplementedError, |
| 1118 | "%s%s%s unavailable on this platform", |
| 1119 | (function_name != NULL) ? function_name : "", |
| 1120 | (function_name != NULL) ? ": ": "", |
| 1121 | argument_name); |
| 1122 | } |
| 1123 | |
| 1124 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1125 | dir_fd_unavailable(PyObject *o, void *p) |
| 1126 | { |
| 1127 | int dir_fd; |
| 1128 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1129 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1130 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1131 | argument_unavailable_error(NULL, "dir_fd"); |
| 1132 | return 0; |
| 1133 | } |
| 1134 | *(int *)p = dir_fd; |
| 1135 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1139 | fd_specified(const char *function_name, int fd) |
| 1140 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1141 | if (fd == -1) |
| 1142 | return 0; |
| 1143 | |
| 1144 | argument_unavailable_error(function_name, "fd"); |
| 1145 | return 1; |
| 1146 | } |
| 1147 | |
| 1148 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1149 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1150 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1151 | if (follow_symlinks) |
| 1152 | return 0; |
| 1153 | |
| 1154 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1155 | return 1; |
| 1156 | } |
| 1157 | |
| 1158 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1159 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1160 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1161 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1162 | #ifndef MS_WINDOWS |
| 1163 | && !path->narrow |
| 1164 | #endif |
| 1165 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1166 | PyErr_Format(PyExc_ValueError, |
| 1167 | "%s: can't specify dir_fd without matching path", |
| 1168 | function_name); |
| 1169 | return 1; |
| 1170 | } |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
| 1174 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1175 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1176 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1177 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1178 | PyErr_Format(PyExc_ValueError, |
| 1179 | "%s: can't specify both dir_fd and fd", |
| 1180 | function_name); |
| 1181 | return 1; |
| 1182 | } |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1187 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1188 | int follow_symlinks) |
| 1189 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1190 | if ((fd > 0) && (!follow_symlinks)) { |
| 1191 | PyErr_Format(PyExc_ValueError, |
| 1192 | "%s: cannot use fd and follow_symlinks together", |
| 1193 | function_name); |
| 1194 | return 1; |
| 1195 | } |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1200 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1201 | int follow_symlinks) |
| 1202 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1203 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1204 | PyErr_Format(PyExc_ValueError, |
| 1205 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1206 | function_name); |
| 1207 | return 1; |
| 1208 | } |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1212 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1213 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1214 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1215 | typedef off_t Py_off_t; |
| 1216 | #endif |
| 1217 | |
| 1218 | static int |
| 1219 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1220 | { |
| 1221 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1222 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1223 | #else |
| 1224 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1225 | #endif |
| 1226 | if (PyErr_Occurred()) |
| 1227 | return 0; |
| 1228 | return 1; |
| 1229 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1230 | |
| 1231 | static PyObject * |
| 1232 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1233 | { |
| 1234 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1235 | return PyLong_FromLongLong(offset); |
| 1236 | #else |
| 1237 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1238 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1239 | } |
| 1240 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1241 | #ifdef HAVE_SIGSET_T |
| 1242 | /* Convert an iterable of integers to a sigset. |
| 1243 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1244 | int |
| 1245 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1246 | { |
| 1247 | sigset_t *mask = (sigset_t *)addr; |
| 1248 | PyObject *iterator, *item; |
| 1249 | long signum; |
| 1250 | int overflow; |
| 1251 | |
| 1252 | if (sigemptyset(mask)) { |
| 1253 | /* Probably only if mask == NULL. */ |
| 1254 | PyErr_SetFromErrno(PyExc_OSError); |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | iterator = PyObject_GetIter(obj); |
| 1259 | if (iterator == NULL) { |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1264 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1265 | Py_DECREF(item); |
| 1266 | if (signum <= 0 || signum >= NSIG) { |
| 1267 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1268 | PyErr_Format(PyExc_ValueError, |
| 1269 | "signal number %ld out of range", signum); |
| 1270 | } |
| 1271 | goto error; |
| 1272 | } |
| 1273 | if (sigaddset(mask, (int)signum)) { |
| 1274 | if (errno != EINVAL) { |
| 1275 | /* Probably impossible */ |
| 1276 | PyErr_SetFromErrno(PyExc_OSError); |
| 1277 | goto error; |
| 1278 | } |
| 1279 | /* For backwards compatibility, allow idioms such as |
| 1280 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1281 | */ |
| 1282 | const char msg[] = |
| 1283 | "invalid signal number %ld, please use valid_signals()"; |
| 1284 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1285 | goto error; |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | if (!PyErr_Occurred()) { |
| 1290 | Py_DECREF(iterator); |
| 1291 | return 1; |
| 1292 | } |
| 1293 | |
| 1294 | error: |
| 1295 | Py_DECREF(iterator); |
| 1296 | return 0; |
| 1297 | } |
| 1298 | #endif /* HAVE_SIGSET_T */ |
| 1299 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1300 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1301 | |
| 1302 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1303 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1304 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1305 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1306 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1307 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1308 | |
| 1309 | if (0 == DeviceIoControl( |
| 1310 | reparse_point_handle, |
| 1311 | FSCTL_GET_REPARSE_POINT, |
| 1312 | NULL, 0, /* in buffer */ |
| 1313 | target_buffer, sizeof(target_buffer), |
| 1314 | &n_bytes_returned, |
| 1315 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1316 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1317 | |
| 1318 | if (reparse_tag) |
| 1319 | *reparse_tag = rdb->ReparseTag; |
| 1320 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1321 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1322 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1323 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1324 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1325 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1326 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1327 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1328 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1329 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1330 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1331 | */ |
| 1332 | #include <crt_externs.h> |
| 1333 | static char **environ; |
| 1334 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1335 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1336 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1337 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1338 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1339 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1340 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1341 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1342 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1343 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1344 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1345 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1346 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1347 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1348 | d = PyDict_New(); |
| 1349 | if (d == NULL) |
| 1350 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1351 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1352 | if (environ == NULL) |
| 1353 | environ = *_NSGetEnviron(); |
| 1354 | #endif |
| 1355 | #ifdef MS_WINDOWS |
| 1356 | /* _wenviron must be initialized in this way if the program is started |
| 1357 | through main() instead of wmain(). */ |
| 1358 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1359 | e = _wenviron; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1360 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1361 | e = environ; |
| 1362 | #endif |
| 1363 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1364 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1365 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1366 | PyObject *k; |
| 1367 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1368 | #ifdef MS_WINDOWS |
| 1369 | const wchar_t *p = wcschr(*e, L'='); |
| 1370 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1371 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1372 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1373 | if (p == NULL) |
| 1374 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1375 | #ifdef MS_WINDOWS |
| 1376 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1377 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1378 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1379 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1380 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1381 | Py_DECREF(d); |
| 1382 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1383 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1384 | #ifdef MS_WINDOWS |
| 1385 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1386 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1387 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1388 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1389 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1390 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1391 | Py_DECREF(d); |
| 1392 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1393 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1394 | if (PyDict_GetItemWithError(d, k) == NULL) { |
| 1395 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { |
| 1396 | Py_DECREF(v); |
| 1397 | Py_DECREF(k); |
| 1398 | Py_DECREF(d); |
| 1399 | return NULL; |
| 1400 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1401 | } |
| 1402 | Py_DECREF(k); |
| 1403 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1404 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1405 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1408 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1409 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1410 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1411 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1412 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1413 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1414 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1415 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1416 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1417 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1418 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1419 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1420 | /* XXX We should pass the function name along in the future. |
| 1421 | (winreg.c also wants to pass the function name.) |
| 1422 | This would however require an additional param to the |
| 1423 | Windows error object, which is non-trivial. |
| 1424 | */ |
| 1425 | errno = GetLastError(); |
| 1426 | if (filename) |
| 1427 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1428 | else |
| 1429 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1430 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1431 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1432 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1433 | win32_error_object(const char* function, PyObject* filename) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1434 | { |
| 1435 | /* XXX - see win32_error for comments on 'function' */ |
| 1436 | errno = GetLastError(); |
| 1437 | if (filename) |
| 1438 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1439 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1440 | errno, |
| 1441 | filename); |
| 1442 | else |
| 1443 | return PyErr_SetFromWindowsErr(errno); |
| 1444 | } |
| 1445 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1446 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1447 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1448 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1449 | posix_path_object_error(PyObject *path) |
| 1450 | { |
| 1451 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1452 | } |
| 1453 | |
| 1454 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1455 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1456 | { |
| 1457 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1458 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1459 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1460 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1461 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1462 | #endif |
| 1463 | } |
| 1464 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1465 | static PyObject * |
| 1466 | path_object_error2(PyObject *path, PyObject *path2) |
| 1467 | { |
| 1468 | #ifdef MS_WINDOWS |
| 1469 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1470 | PyExc_OSError, 0, path, path2); |
| 1471 | #else |
| 1472 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1473 | #endif |
| 1474 | } |
| 1475 | |
| 1476 | static PyObject * |
| 1477 | path_error(path_t *path) |
| 1478 | { |
| 1479 | return path_object_error(path->object); |
| 1480 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1481 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1482 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1483 | posix_path_error(path_t *path) |
| 1484 | { |
| 1485 | return posix_path_object_error(path->object); |
| 1486 | } |
| 1487 | |
| 1488 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1489 | path_error2(path_t *path, path_t *path2) |
| 1490 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1491 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1495 | /* POSIX generic methods */ |
| 1496 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1497 | static int |
| 1498 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1499 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1500 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1501 | int *pointer = (int *)p; |
| 1502 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1503 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1504 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1505 | *pointer = fd; |
| 1506 | return 1; |
| 1507 | } |
| 1508 | |
| 1509 | static PyObject * |
| 1510 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1511 | { |
| 1512 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1513 | int async_err = 0; |
| 1514 | |
| 1515 | do { |
| 1516 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1517 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1518 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1519 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1520 | Py_END_ALLOW_THREADS |
| 1521 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1522 | if (res != 0) |
| 1523 | return (!async_err) ? posix_error() : NULL; |
| 1524 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1525 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1526 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1527 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1528 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1529 | /* This is a reimplementation of the C library's chdir function, |
| 1530 | but one that produces Win32 errors instead of DOS error codes. |
| 1531 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1532 | it also needs to set "magic" environment variables indicating |
| 1533 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1534 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1535 | win32_wchdir(LPCWSTR path) |
| 1536 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1537 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1538 | int result; |
| 1539 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1540 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1541 | if(!SetCurrentDirectoryW(path)) |
| 1542 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1543 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1544 | if (!result) |
| 1545 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1546 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1547 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1548 | if (!new_path) { |
| 1549 | SetLastError(ERROR_OUTOFMEMORY); |
| 1550 | return FALSE; |
| 1551 | } |
| 1552 | result = GetCurrentDirectoryW(result, new_path); |
| 1553 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1554 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1555 | return FALSE; |
| 1556 | } |
| 1557 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1558 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1559 | wcsncmp(new_path, L"//", 2) == 0); |
| 1560 | if (!is_unc_like_path) { |
| 1561 | env[1] = new_path[0]; |
| 1562 | result = SetEnvironmentVariableW(env, new_path); |
| 1563 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1564 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1565 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1566 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1567 | } |
| 1568 | #endif |
| 1569 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1570 | #ifdef MS_WINDOWS |
| 1571 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1572 | - time stamps are restricted to second resolution |
| 1573 | - file modification times suffer from forth-and-back conversions between |
| 1574 | UTC and local time |
| 1575 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1576 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1577 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1578 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1579 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1580 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1581 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1582 | BY_HANDLE_FILE_INFORMATION *info, |
| 1583 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1584 | { |
| 1585 | memset(info, 0, sizeof(*info)); |
| 1586 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1587 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1588 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1589 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1590 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1591 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1592 | /* info->nNumberOfLinks = 1; */ |
| 1593 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1594 | *reparse_tag = pFileData->dwReserved0; |
| 1595 | else |
| 1596 | *reparse_tag = 0; |
| 1597 | } |
| 1598 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1599 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1600 | 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] | 1601 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1602 | HANDLE hFindFile; |
| 1603 | WIN32_FIND_DATAW FileData; |
| 1604 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1605 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1606 | return FALSE; |
| 1607 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1608 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1609 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1612 | static BOOL |
| 1613 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1614 | { |
| 1615 | int buf_size, result_length; |
| 1616 | wchar_t *buf; |
| 1617 | |
| 1618 | /* We have a good handle to the target, use it to determine |
| 1619 | the target path name (then we'll call lstat on it). */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1620 | buf_size = GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1621 | VOLUME_NAME_DOS); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1622 | if(!buf_size) |
| 1623 | return FALSE; |
| 1624 | |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1625 | buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1626 | if (!buf) { |
| 1627 | SetLastError(ERROR_OUTOFMEMORY); |
| 1628 | return FALSE; |
| 1629 | } |
| 1630 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1631 | result_length = GetFinalPathNameByHandleW(hdl, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1632 | buf, buf_size, VOLUME_NAME_DOS); |
| 1633 | |
| 1634 | if(!result_length) { |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1635 | PyMem_RawFree(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1636 | return FALSE; |
| 1637 | } |
| 1638 | |
| 1639 | if(!CloseHandle(hdl)) { |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1640 | PyMem_RawFree(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1641 | return FALSE; |
| 1642 | } |
| 1643 | |
| 1644 | buf[result_length] = 0; |
| 1645 | |
| 1646 | *target_path = buf; |
| 1647 | return TRUE; |
| 1648 | } |
| 1649 | |
| 1650 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1651 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1652 | BOOL traverse) |
| 1653 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1654 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1655 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1656 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1657 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1658 | wchar_t *target_path; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1659 | const wchar_t *dot; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1660 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1661 | hFile = CreateFileW( |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1662 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1663 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1664 | 0, /* share mode */ |
| 1665 | NULL, /* security attributes */ |
| 1666 | OPEN_EXISTING, |
| 1667 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1668 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1669 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1670 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1671 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1672 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1673 | NULL); |
| 1674 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1675 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1676 | /* Either the target doesn't exist, or we don't have access to |
| 1677 | get a handle to it. If the former, we need to return an error. |
| 1678 | If the latter, we can use attributes_from_dir. */ |
Berker Peksag | 0b4dc48 | 2016-09-17 15:49:59 +0300 | [diff] [blame] | 1679 | DWORD lastError = GetLastError(); |
| 1680 | if (lastError != ERROR_ACCESS_DENIED && |
| 1681 | lastError != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1682 | return -1; |
| 1683 | /* Could not get attributes on open file. Fall back to |
| 1684 | reading the directory. */ |
| 1685 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1686 | /* Very strange. This should not fail now */ |
| 1687 | return -1; |
| 1688 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1689 | if (traverse) { |
| 1690 | /* Should traverse, but could not open reparse point handle */ |
Berker Peksag | 0b4dc48 | 2016-09-17 15:49:59 +0300 | [diff] [blame] | 1691 | SetLastError(lastError); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1692 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1693 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1694 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1695 | } else { |
| 1696 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1697 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1698 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1699 | } |
| 1700 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1701 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1702 | return -1; |
| 1703 | |
| 1704 | /* Close the outer open file handle now that we're about to |
| 1705 | reopen it with different flags. */ |
| 1706 | if (!CloseHandle(hFile)) |
| 1707 | return -1; |
| 1708 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1709 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1710 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1711 | the file without the reparse handling flag set. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1712 | hFile2 = CreateFileW( |
| 1713 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1714 | NULL, OPEN_EXISTING, |
| 1715 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1716 | NULL); |
| 1717 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1718 | return -1; |
| 1719 | |
| 1720 | if (!get_target_path(hFile2, &target_path)) |
| 1721 | return -1; |
| 1722 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1723 | code = win32_xstat_impl(target_path, result, FALSE); |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1724 | PyMem_RawFree(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1725 | return code; |
| 1726 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1727 | } else |
| 1728 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1729 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1730 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1731 | |
| 1732 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1733 | dot = wcsrchr(path, '.'); |
| 1734 | if (dot) { |
| 1735 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1736 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1737 | result->st_mode |= 0111; |
| 1738 | } |
| 1739 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1743 | 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] | 1744 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1745 | /* Protocol violation: we explicitly clear errno, instead of |
| 1746 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1747 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1748 | errno = 0; |
| 1749 | return code; |
| 1750 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1751 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1752 | |
| 1753 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1754 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1755 | default does not traverse symlinks and instead returns attributes for |
| 1756 | the symlink. |
| 1757 | |
| 1758 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1759 | win32_stat will first explicitly resolve the symlink target and then will |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1760 | call win32_lstat on that result. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1761 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1762 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1763 | 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] | 1764 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1765 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1768 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1769 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1770 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1771 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1774 | #endif /* MS_WINDOWS */ |
| 1775 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1776 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1777 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1778 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1779 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1780 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1781 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1782 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1783 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1784 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1785 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1786 | |
| 1787 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1788 | {"st_mode", "protection bits"}, |
| 1789 | {"st_ino", "inode"}, |
| 1790 | {"st_dev", "device"}, |
| 1791 | {"st_nlink", "number of hard links"}, |
| 1792 | {"st_uid", "user ID of owner"}, |
| 1793 | {"st_gid", "group ID of owner"}, |
| 1794 | {"st_size", "total size, in bytes"}, |
| 1795 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1796 | {NULL, "integer time of last access"}, |
| 1797 | {NULL, "integer time of last modification"}, |
| 1798 | {NULL, "integer time of last change"}, |
| 1799 | {"st_atime", "time of last access"}, |
| 1800 | {"st_mtime", "time of last modification"}, |
| 1801 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1802 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1803 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1804 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1805 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1806 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1807 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1808 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1809 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1810 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1811 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1812 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1813 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1814 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1815 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1816 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1817 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1818 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1819 | #endif |
| 1820 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1821 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1822 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1823 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1824 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1825 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1826 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1827 | {"st_fstype", "Type of filesystem"}, |
| 1828 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1829 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1830 | }; |
| 1831 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1832 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1833 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1834 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1835 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1836 | #endif |
| 1837 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1838 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1839 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1840 | #else |
| 1841 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1842 | #endif |
| 1843 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1844 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1845 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1846 | #else |
| 1847 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1848 | #endif |
| 1849 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1850 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1851 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1852 | #else |
| 1853 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1854 | #endif |
| 1855 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1856 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1857 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1858 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1859 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1860 | #endif |
| 1861 | |
| 1862 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1863 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1864 | #else |
| 1865 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1866 | #endif |
| 1867 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1868 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1869 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1870 | #else |
| 1871 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 1872 | #endif |
| 1873 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1874 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1875 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 1876 | #else |
| 1877 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 1878 | #endif |
| 1879 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1880 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1881 | "stat_result", /* name */ |
| 1882 | stat_result__doc__, /* doc */ |
| 1883 | stat_result_fields, |
| 1884 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1885 | }; |
| 1886 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1887 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1888 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1889 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1890 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1891 | 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] | 1892 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1893 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1894 | |
| 1895 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1896 | {"f_bsize", }, |
| 1897 | {"f_frsize", }, |
| 1898 | {"f_blocks", }, |
| 1899 | {"f_bfree", }, |
| 1900 | {"f_bavail", }, |
| 1901 | {"f_files", }, |
| 1902 | {"f_ffree", }, |
| 1903 | {"f_favail", }, |
| 1904 | {"f_flag", }, |
| 1905 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 1906 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1907 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1908 | }; |
| 1909 | |
| 1910 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1911 | "statvfs_result", /* name */ |
| 1912 | statvfs_result__doc__, /* doc */ |
| 1913 | statvfs_result_fields, |
| 1914 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1915 | }; |
| 1916 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1917 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1918 | PyDoc_STRVAR(waitid_result__doc__, |
| 1919 | "waitid_result: Result from waitid.\n\n\ |
| 1920 | This object may be accessed either as a tuple of\n\ |
| 1921 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1922 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1923 | \n\ |
| 1924 | See os.waitid for more information."); |
| 1925 | |
| 1926 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1927 | {"si_pid", }, |
| 1928 | {"si_uid", }, |
| 1929 | {"si_signo", }, |
| 1930 | {"si_status", }, |
| 1931 | {"si_code", }, |
| 1932 | {0} |
| 1933 | }; |
| 1934 | |
| 1935 | static PyStructSequence_Desc waitid_result_desc = { |
| 1936 | "waitid_result", /* name */ |
| 1937 | waitid_result__doc__, /* doc */ |
| 1938 | waitid_result_fields, |
| 1939 | 5 |
| 1940 | }; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 1941 | static PyTypeObject* WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1942 | #endif |
| 1943 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1944 | static int initialized; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 1945 | static PyTypeObject* StatResultType; |
| 1946 | static PyTypeObject* StatVFSResultType; |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 1947 | #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] | 1948 | static PyTypeObject* SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1949 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1950 | static newfunc structseq_new; |
| 1951 | |
| 1952 | static PyObject * |
| 1953 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1954 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1955 | PyStructSequence *result; |
| 1956 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1957 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1958 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1959 | if (!result) |
| 1960 | return NULL; |
| 1961 | /* If we have been initialized from a tuple, |
| 1962 | st_?time might be set to None. Initialize it |
| 1963 | from the int slots. */ |
| 1964 | for (i = 7; i <= 9; i++) { |
| 1965 | if (result->ob_item[i+3] == Py_None) { |
| 1966 | Py_DECREF(Py_None); |
| 1967 | Py_INCREF(result->ob_item[i]); |
| 1968 | result->ob_item[i+3] = result->ob_item[i]; |
| 1969 | } |
| 1970 | } |
| 1971 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1975 | static PyObject *billion = NULL; |
| 1976 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1977 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1978 | 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] | 1979 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1980 | PyObject *s = _PyLong_FromTime_t(sec); |
| 1981 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 1982 | PyObject *s_in_ns = NULL; |
| 1983 | PyObject *ns_total = NULL; |
| 1984 | PyObject *float_s = NULL; |
| 1985 | |
| 1986 | if (!(s && ns_fractional)) |
| 1987 | goto exit; |
| 1988 | |
| 1989 | s_in_ns = PyNumber_Multiply(s, billion); |
| 1990 | if (!s_in_ns) |
| 1991 | goto exit; |
| 1992 | |
| 1993 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 1994 | if (!ns_total) |
| 1995 | goto exit; |
| 1996 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 1997 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1998 | if (!float_s) { |
| 1999 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | PyStructSequence_SET_ITEM(v, index, s); |
| 2003 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2004 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2005 | s = NULL; |
| 2006 | float_s = NULL; |
| 2007 | ns_total = NULL; |
| 2008 | exit: |
| 2009 | Py_XDECREF(s); |
| 2010 | Py_XDECREF(ns_fractional); |
| 2011 | Py_XDECREF(s_in_ns); |
| 2012 | Py_XDECREF(ns_total); |
| 2013 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2016 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2017 | (used by posix_stat() and posix_fstat()) */ |
| 2018 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2019 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2020 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2021 | unsigned long ansec, mnsec, cnsec; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2022 | PyObject *v = PyStructSequence_New(StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2023 | if (v == NULL) |
| 2024 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2025 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2026 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2027 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2028 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2029 | #ifdef MS_WINDOWS |
| 2030 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2031 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2032 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2033 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2034 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2035 | #if defined(MS_WINDOWS) |
| 2036 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2037 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2038 | #else |
| 2039 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2040 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2041 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2042 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2043 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2044 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2045 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2046 | ansec = st->st_atim.tv_nsec; |
| 2047 | mnsec = st->st_mtim.tv_nsec; |
| 2048 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2049 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2050 | ansec = st->st_atimespec.tv_nsec; |
| 2051 | mnsec = st->st_mtimespec.tv_nsec; |
| 2052 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2053 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2054 | ansec = st->st_atime_nsec; |
| 2055 | mnsec = st->st_mtime_nsec; |
| 2056 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2057 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2058 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2059 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2060 | fill_time(v, 7, st->st_atime, ansec); |
| 2061 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2062 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2063 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2064 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2065 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2066 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2067 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2068 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2069 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2070 | PyLong_FromLong((long)st->st_blocks)); |
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_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2073 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2074 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2075 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2076 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2077 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2078 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2079 | #endif |
| 2080 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2081 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2082 | PyObject *val; |
| 2083 | unsigned long bsec,bnsec; |
| 2084 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2085 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2086 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2087 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2088 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2089 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2090 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2091 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2092 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2093 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2094 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2095 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2096 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2097 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2098 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2099 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2100 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2101 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2102 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2103 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2104 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2105 | PyUnicode_FromString(st->st_fstype)); |
| 2106 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2107 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2108 | if (PyErr_Occurred()) { |
| 2109 | Py_DECREF(v); |
| 2110 | return NULL; |
| 2111 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2112 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2113 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2116 | /* POSIX methods */ |
| 2117 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2118 | |
| 2119 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2120 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2121 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2122 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2123 | STRUCT_STAT st; |
| 2124 | int result; |
| 2125 | |
| 2126 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2127 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2128 | return NULL; |
| 2129 | #endif |
| 2130 | |
| 2131 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2132 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2133 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2134 | return NULL; |
| 2135 | |
| 2136 | Py_BEGIN_ALLOW_THREADS |
| 2137 | if (path->fd != -1) |
| 2138 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2139 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2140 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2141 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2142 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2143 | result = win32_lstat(path->wide, &st); |
| 2144 | #else |
| 2145 | else |
| 2146 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2147 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2148 | result = LSTAT(path->narrow, &st); |
| 2149 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2150 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2151 | #ifdef HAVE_FSTATAT |
| 2152 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2153 | result = fstatat(dir_fd, path->narrow, &st, |
| 2154 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2155 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2156 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2157 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2158 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2159 | Py_END_ALLOW_THREADS |
| 2160 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2161 | if (result != 0) { |
| 2162 | return path_error(path); |
| 2163 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2164 | |
| 2165 | return _pystat_fromstructstat(&st); |
| 2166 | } |
| 2167 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2168 | /*[python input] |
| 2169 | |
| 2170 | for s in """ |
| 2171 | |
| 2172 | FACCESSAT |
| 2173 | FCHMODAT |
| 2174 | FCHOWNAT |
| 2175 | FSTATAT |
| 2176 | LINKAT |
| 2177 | MKDIRAT |
| 2178 | MKFIFOAT |
| 2179 | MKNODAT |
| 2180 | OPENAT |
| 2181 | READLINKAT |
| 2182 | SYMLINKAT |
| 2183 | UNLINKAT |
| 2184 | |
| 2185 | """.strip().split(): |
| 2186 | s = s.strip() |
| 2187 | print(""" |
| 2188 | #ifdef HAVE_{s} |
| 2189 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2190 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2191 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2192 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2193 | """.rstrip().format(s=s)) |
| 2194 | |
| 2195 | for s in """ |
| 2196 | |
| 2197 | FCHDIR |
| 2198 | FCHMOD |
| 2199 | FCHOWN |
| 2200 | FDOPENDIR |
| 2201 | FEXECVE |
| 2202 | FPATHCONF |
| 2203 | FSTATVFS |
| 2204 | FTRUNCATE |
| 2205 | |
| 2206 | """.strip().split(): |
| 2207 | s = s.strip() |
| 2208 | print(""" |
| 2209 | #ifdef HAVE_{s} |
| 2210 | #define PATH_HAVE_{s} 1 |
| 2211 | #else |
| 2212 | #define PATH_HAVE_{s} 0 |
| 2213 | #endif |
| 2214 | |
| 2215 | """.rstrip().format(s=s)) |
| 2216 | [python start generated code]*/ |
| 2217 | |
| 2218 | #ifdef HAVE_FACCESSAT |
| 2219 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2220 | #else |
| 2221 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2222 | #endif |
| 2223 | |
| 2224 | #ifdef HAVE_FCHMODAT |
| 2225 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2226 | #else |
| 2227 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2228 | #endif |
| 2229 | |
| 2230 | #ifdef HAVE_FCHOWNAT |
| 2231 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2232 | #else |
| 2233 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2234 | #endif |
| 2235 | |
| 2236 | #ifdef HAVE_FSTATAT |
| 2237 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2238 | #else |
| 2239 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2240 | #endif |
| 2241 | |
| 2242 | #ifdef HAVE_LINKAT |
| 2243 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2244 | #else |
| 2245 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2246 | #endif |
| 2247 | |
| 2248 | #ifdef HAVE_MKDIRAT |
| 2249 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2250 | #else |
| 2251 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2252 | #endif |
| 2253 | |
| 2254 | #ifdef HAVE_MKFIFOAT |
| 2255 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2256 | #else |
| 2257 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2258 | #endif |
| 2259 | |
| 2260 | #ifdef HAVE_MKNODAT |
| 2261 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2262 | #else |
| 2263 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2264 | #endif |
| 2265 | |
| 2266 | #ifdef HAVE_OPENAT |
| 2267 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2268 | #else |
| 2269 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2270 | #endif |
| 2271 | |
| 2272 | #ifdef HAVE_READLINKAT |
| 2273 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2274 | #else |
| 2275 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2276 | #endif |
| 2277 | |
| 2278 | #ifdef HAVE_SYMLINKAT |
| 2279 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2280 | #else |
| 2281 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2282 | #endif |
| 2283 | |
| 2284 | #ifdef HAVE_UNLINKAT |
| 2285 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2286 | #else |
| 2287 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2288 | #endif |
| 2289 | |
| 2290 | #ifdef HAVE_FCHDIR |
| 2291 | #define PATH_HAVE_FCHDIR 1 |
| 2292 | #else |
| 2293 | #define PATH_HAVE_FCHDIR 0 |
| 2294 | #endif |
| 2295 | |
| 2296 | #ifdef HAVE_FCHMOD |
| 2297 | #define PATH_HAVE_FCHMOD 1 |
| 2298 | #else |
| 2299 | #define PATH_HAVE_FCHMOD 0 |
| 2300 | #endif |
| 2301 | |
| 2302 | #ifdef HAVE_FCHOWN |
| 2303 | #define PATH_HAVE_FCHOWN 1 |
| 2304 | #else |
| 2305 | #define PATH_HAVE_FCHOWN 0 |
| 2306 | #endif |
| 2307 | |
| 2308 | #ifdef HAVE_FDOPENDIR |
| 2309 | #define PATH_HAVE_FDOPENDIR 1 |
| 2310 | #else |
| 2311 | #define PATH_HAVE_FDOPENDIR 0 |
| 2312 | #endif |
| 2313 | |
| 2314 | #ifdef HAVE_FEXECVE |
| 2315 | #define PATH_HAVE_FEXECVE 1 |
| 2316 | #else |
| 2317 | #define PATH_HAVE_FEXECVE 0 |
| 2318 | #endif |
| 2319 | |
| 2320 | #ifdef HAVE_FPATHCONF |
| 2321 | #define PATH_HAVE_FPATHCONF 1 |
| 2322 | #else |
| 2323 | #define PATH_HAVE_FPATHCONF 0 |
| 2324 | #endif |
| 2325 | |
| 2326 | #ifdef HAVE_FSTATVFS |
| 2327 | #define PATH_HAVE_FSTATVFS 1 |
| 2328 | #else |
| 2329 | #define PATH_HAVE_FSTATVFS 0 |
| 2330 | #endif |
| 2331 | |
| 2332 | #ifdef HAVE_FTRUNCATE |
| 2333 | #define PATH_HAVE_FTRUNCATE 1 |
| 2334 | #else |
| 2335 | #define PATH_HAVE_FTRUNCATE 0 |
| 2336 | #endif |
| 2337 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2338 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2339 | #ifdef MS_WINDOWS |
| 2340 | #undef PATH_HAVE_FTRUNCATE |
| 2341 | #define PATH_HAVE_FTRUNCATE 1 |
| 2342 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2343 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2344 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2345 | |
| 2346 | class path_t_converter(CConverter): |
| 2347 | |
| 2348 | type = "path_t" |
| 2349 | impl_by_reference = True |
| 2350 | parse_by_reference = True |
| 2351 | |
| 2352 | converter = 'path_converter' |
| 2353 | |
| 2354 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2355 | # right now path_t doesn't support default values. |
| 2356 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2357 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2358 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2359 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2360 | if self.c_default not in (None, 'Py_None'): |
| 2361 | 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] | 2362 | |
| 2363 | self.nullable = nullable |
| 2364 | self.allow_fd = allow_fd |
| 2365 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2366 | def pre_render(self): |
| 2367 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2368 | if isinstance(value, str): |
| 2369 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2370 | return str(int(bool(value))) |
| 2371 | |
| 2372 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2373 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2374 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2375 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2376 | strify(self.nullable), |
| 2377 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2378 | ) |
| 2379 | |
| 2380 | def cleanup(self): |
| 2381 | return "path_cleanup(&" + self.name + ");\n" |
| 2382 | |
| 2383 | |
| 2384 | class dir_fd_converter(CConverter): |
| 2385 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2386 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2387 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2388 | if self.default in (unspecified, None): |
| 2389 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2390 | if isinstance(requires, str): |
| 2391 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2392 | else: |
| 2393 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2394 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2395 | class fildes_converter(CConverter): |
| 2396 | type = 'int' |
| 2397 | converter = 'fildes_converter' |
| 2398 | |
| 2399 | class uid_t_converter(CConverter): |
| 2400 | type = "uid_t" |
| 2401 | converter = '_Py_Uid_Converter' |
| 2402 | |
| 2403 | class gid_t_converter(CConverter): |
| 2404 | type = "gid_t" |
| 2405 | converter = '_Py_Gid_Converter' |
| 2406 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2407 | class dev_t_converter(CConverter): |
| 2408 | type = 'dev_t' |
| 2409 | converter = '_Py_Dev_Converter' |
| 2410 | |
| 2411 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2412 | type = 'dev_t' |
| 2413 | conversion_fn = '_PyLong_FromDev' |
| 2414 | unsigned_cast = '(dev_t)' |
| 2415 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2416 | class FSConverter_converter(CConverter): |
| 2417 | type = 'PyObject *' |
| 2418 | converter = 'PyUnicode_FSConverter' |
| 2419 | def converter_init(self): |
| 2420 | if self.default is not unspecified: |
| 2421 | fail("FSConverter_converter does not support default values") |
| 2422 | self.c_default = 'NULL' |
| 2423 | |
| 2424 | def cleanup(self): |
| 2425 | return "Py_XDECREF(" + self.name + ");\n" |
| 2426 | |
| 2427 | class pid_t_converter(CConverter): |
| 2428 | type = 'pid_t' |
| 2429 | format_unit = '" _Py_PARSE_PID "' |
| 2430 | |
| 2431 | class idtype_t_converter(int_converter): |
| 2432 | type = 'idtype_t' |
| 2433 | |
| 2434 | class id_t_converter(CConverter): |
| 2435 | type = 'id_t' |
| 2436 | format_unit = '" _Py_PARSE_PID "' |
| 2437 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2438 | class intptr_t_converter(CConverter): |
| 2439 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2440 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2441 | |
| 2442 | class Py_off_t_converter(CConverter): |
| 2443 | type = 'Py_off_t' |
| 2444 | converter = 'Py_off_t_converter' |
| 2445 | |
| 2446 | class Py_off_t_return_converter(long_return_converter): |
| 2447 | type = 'Py_off_t' |
| 2448 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2449 | |
| 2450 | class path_confname_converter(CConverter): |
| 2451 | type="int" |
| 2452 | converter="conv_path_confname" |
| 2453 | |
| 2454 | class confstr_confname_converter(path_confname_converter): |
| 2455 | converter='conv_confstr_confname' |
| 2456 | |
| 2457 | class sysconf_confname_converter(path_confname_converter): |
| 2458 | converter="conv_sysconf_confname" |
| 2459 | |
| 2460 | class sched_param_converter(CConverter): |
| 2461 | type = 'struct sched_param' |
| 2462 | converter = 'convert_sched_param' |
| 2463 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2464 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2465 | [python start generated code]*/ |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 2466 | /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2467 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2468 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2469 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2470 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2471 | |
| 2472 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2473 | 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] | 2474 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2475 | |
| 2476 | * |
| 2477 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2478 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2479 | If not None, it should be a file descriptor open to a directory, |
| 2480 | and path should be a relative string; path will then be relative to |
| 2481 | that directory. |
| 2482 | |
| 2483 | follow_symlinks: bool = True |
| 2484 | If False, and the last element of the path is a symbolic link, |
| 2485 | stat will examine the symbolic link itself instead of the file |
| 2486 | the link points to. |
| 2487 | |
| 2488 | Perform a stat system call on the given path. |
| 2489 | |
| 2490 | dir_fd and follow_symlinks may not be implemented |
| 2491 | on your platform. If they are unavailable, using them will raise a |
| 2492 | NotImplementedError. |
| 2493 | |
| 2494 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2495 | an open file descriptor. |
| 2496 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2497 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2498 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2499 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2500 | 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] | 2501 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2502 | { |
| 2503 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2504 | } |
| 2505 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2506 | |
| 2507 | /*[clinic input] |
| 2508 | os.lstat |
| 2509 | |
| 2510 | path : path_t |
| 2511 | |
| 2512 | * |
| 2513 | |
| 2514 | dir_fd : dir_fd(requires='fstatat') = None |
| 2515 | |
| 2516 | Perform a stat system call on the given path, without following symbolic links. |
| 2517 | |
| 2518 | Like stat(), but do not follow symbolic links. |
| 2519 | Equivalent to stat(path, follow_symlinks=False). |
| 2520 | [clinic start generated code]*/ |
| 2521 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2522 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2523 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2524 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2525 | { |
| 2526 | int follow_symlinks = 0; |
| 2527 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2528 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2529 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2530 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2531 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2532 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2533 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2534 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2535 | 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] | 2536 | |
| 2537 | mode: int |
| 2538 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2539 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2540 | |
| 2541 | * |
| 2542 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2543 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2544 | If not None, it should be a file descriptor open to a directory, |
| 2545 | and path should be relative; path will then be relative to that |
| 2546 | directory. |
| 2547 | |
| 2548 | effective_ids: bool = False |
| 2549 | If True, access will use the effective uid/gid instead of |
| 2550 | the real uid/gid. |
| 2551 | |
| 2552 | follow_symlinks: bool = True |
| 2553 | If False, and the last element of the path is a symbolic link, |
| 2554 | access will examine the symbolic link itself instead of the file |
| 2555 | the link points to. |
| 2556 | |
| 2557 | Use the real uid/gid to test for access to a path. |
| 2558 | |
| 2559 | {parameters} |
| 2560 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2561 | on your platform. If they are unavailable, using them will raise a |
| 2562 | NotImplementedError. |
| 2563 | |
| 2564 | Note that most operations will use the effective uid/gid, therefore this |
| 2565 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2566 | has the specified access to the path. |
| 2567 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2568 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2569 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2570 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2571 | 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] | 2572 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2573 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2574 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2575 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2576 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2577 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2578 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2579 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2580 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2581 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2582 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2583 | #ifndef HAVE_FACCESSAT |
| 2584 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2585 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2586 | |
| 2587 | if (effective_ids) { |
| 2588 | argument_unavailable_error("access", "effective_ids"); |
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 | #endif |
| 2592 | |
| 2593 | #ifdef MS_WINDOWS |
| 2594 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2595 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2596 | Py_END_ALLOW_THREADS |
| 2597 | |
| 2598 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2599 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2600 | * * we didn't get a -1, and |
| 2601 | * * write access wasn't requested, |
| 2602 | * * or the file isn't read-only, |
| 2603 | * * or it's a directory. |
| 2604 | * (Directories cannot be read-only on Windows.) |
| 2605 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2606 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2607 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2608 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2609 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2610 | #else |
| 2611 | |
| 2612 | Py_BEGIN_ALLOW_THREADS |
| 2613 | #ifdef HAVE_FACCESSAT |
| 2614 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2615 | effective_ids || |
| 2616 | !follow_symlinks) { |
| 2617 | int flags = 0; |
| 2618 | if (!follow_symlinks) |
| 2619 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2620 | if (effective_ids) |
| 2621 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2622 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2623 | } |
| 2624 | else |
| 2625 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2626 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2627 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2628 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2629 | #endif |
| 2630 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2631 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2634 | #ifndef F_OK |
| 2635 | #define F_OK 0 |
| 2636 | #endif |
| 2637 | #ifndef R_OK |
| 2638 | #define R_OK 4 |
| 2639 | #endif |
| 2640 | #ifndef W_OK |
| 2641 | #define W_OK 2 |
| 2642 | #endif |
| 2643 | #ifndef X_OK |
| 2644 | #define X_OK 1 |
| 2645 | #endif |
| 2646 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2647 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2648 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2649 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2650 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2651 | |
| 2652 | fd: int |
| 2653 | Integer file descriptor handle. |
| 2654 | |
| 2655 | / |
| 2656 | |
| 2657 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2658 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2659 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2660 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2661 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2662 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2663 | { |
| 2664 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2665 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2666 | ret = ttyname(fd); |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2667 | if (ret == NULL) { |
| 2668 | return posix_error(); |
| 2669 | } |
| 2670 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2671 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2672 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2673 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2674 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2675 | /*[clinic input] |
| 2676 | os.ctermid |
| 2677 | |
| 2678 | Return the name of the controlling terminal for this process. |
| 2679 | [clinic start generated code]*/ |
| 2680 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2681 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2682 | os_ctermid_impl(PyObject *module) |
| 2683 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2684 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2685 | char *ret; |
| 2686 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2687 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2688 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2689 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2690 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2691 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2692 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2693 | if (ret == NULL) |
| 2694 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2695 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2696 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2697 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2698 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2699 | |
| 2700 | /*[clinic input] |
| 2701 | os.chdir |
| 2702 | |
| 2703 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2704 | |
| 2705 | Change the current working directory to the specified path. |
| 2706 | |
| 2707 | path may always be specified as a string. |
| 2708 | On some platforms, path may also be specified as an open file descriptor. |
| 2709 | If this functionality is unavailable, using it raises an exception. |
| 2710 | [clinic start generated code]*/ |
| 2711 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2712 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2713 | os_chdir_impl(PyObject *module, path_t *path) |
| 2714 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2715 | { |
| 2716 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2717 | |
| 2718 | Py_BEGIN_ALLOW_THREADS |
| 2719 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2720 | /* on unix, success = 0, on windows, success = !0 */ |
| 2721 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2722 | #else |
| 2723 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2724 | if (path->fd != -1) |
| 2725 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2726 | else |
| 2727 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2728 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2729 | #endif |
| 2730 | Py_END_ALLOW_THREADS |
| 2731 | |
| 2732 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2733 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2734 | } |
| 2735 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2736 | Py_RETURN_NONE; |
| 2737 | } |
| 2738 | |
| 2739 | |
| 2740 | #ifdef HAVE_FCHDIR |
| 2741 | /*[clinic input] |
| 2742 | os.fchdir |
| 2743 | |
| 2744 | fd: fildes |
| 2745 | |
| 2746 | Change to the directory of the given file descriptor. |
| 2747 | |
| 2748 | fd must be opened on a directory, not a file. |
| 2749 | Equivalent to os.chdir(fd). |
| 2750 | |
| 2751 | [clinic start generated code]*/ |
| 2752 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2753 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2754 | os_fchdir_impl(PyObject *module, int fd) |
| 2755 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2756 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2757 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2758 | } |
| 2759 | #endif /* HAVE_FCHDIR */ |
| 2760 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2761 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2762 | /*[clinic input] |
| 2763 | os.chmod |
| 2764 | |
| 2765 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2766 | 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] | 2767 | On some platforms, path may also be specified as an open file descriptor. |
| 2768 | If this functionality is unavailable, using it raises an exception. |
| 2769 | |
| 2770 | mode: int |
| 2771 | Operating-system mode bitfield. |
| 2772 | |
| 2773 | * |
| 2774 | |
| 2775 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2776 | If not None, it should be a file descriptor open to a directory, |
| 2777 | and path should be relative; path will then be relative to that |
| 2778 | directory. |
| 2779 | |
| 2780 | follow_symlinks: bool = True |
| 2781 | If False, and the last element of the path is a symbolic link, |
| 2782 | chmod will modify the symbolic link itself instead of the file |
| 2783 | the link points to. |
| 2784 | |
| 2785 | Change the access permissions of a file. |
| 2786 | |
| 2787 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2788 | an open file descriptor. |
| 2789 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2790 | If they are unavailable, using them will raise a NotImplementedError. |
| 2791 | |
| 2792 | [clinic start generated code]*/ |
| 2793 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2794 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2795 | 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] | 2796 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2797 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2798 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2799 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2800 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2801 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2802 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2803 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2804 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2805 | #ifdef HAVE_FCHMODAT |
| 2806 | int fchmodat_nofollow_unsupported = 0; |
| 2807 | #endif |
| 2808 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2809 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2810 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2811 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2812 | #endif |
| 2813 | |
| 2814 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2815 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2816 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2817 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2818 | result = 0; |
| 2819 | else { |
| 2820 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2821 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2822 | else |
| 2823 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2824 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2825 | } |
| 2826 | Py_END_ALLOW_THREADS |
| 2827 | |
| 2828 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2829 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2830 | } |
| 2831 | #else /* MS_WINDOWS */ |
| 2832 | Py_BEGIN_ALLOW_THREADS |
| 2833 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2834 | if (path->fd != -1) |
| 2835 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2836 | else |
| 2837 | #endif |
| 2838 | #ifdef HAVE_LCHMOD |
| 2839 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2840 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2841 | else |
| 2842 | #endif |
| 2843 | #ifdef HAVE_FCHMODAT |
| 2844 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2845 | /* |
| 2846 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2847 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2848 | * and then says it isn't implemented yet. |
| 2849 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2850 | * |
| 2851 | * Once it is supported, os.chmod will automatically |
| 2852 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2853 | * Until then, we need to be careful what exception we raise. |
| 2854 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2855 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2856 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2857 | /* |
| 2858 | * But wait! We can't throw the exception without allowing threads, |
| 2859 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2860 | */ |
| 2861 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2862 | result && |
| 2863 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2864 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2865 | } |
| 2866 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2867 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2868 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2869 | Py_END_ALLOW_THREADS |
| 2870 | |
| 2871 | if (result) { |
| 2872 | #ifdef HAVE_FCHMODAT |
| 2873 | if (fchmodat_nofollow_unsupported) { |
| 2874 | if (dir_fd != DEFAULT_DIR_FD) |
| 2875 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2876 | dir_fd, follow_symlinks); |
| 2877 | else |
| 2878 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 2879 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2880 | } |
| 2881 | else |
| 2882 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2883 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2884 | } |
| 2885 | #endif |
| 2886 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2887 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2890 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2891 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2892 | /*[clinic input] |
| 2893 | os.fchmod |
| 2894 | |
| 2895 | fd: int |
| 2896 | mode: int |
| 2897 | |
| 2898 | Change the access permissions of the file given by file descriptor fd. |
| 2899 | |
| 2900 | Equivalent to os.chmod(fd, mode). |
| 2901 | [clinic start generated code]*/ |
| 2902 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2903 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2904 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 2905 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2906 | { |
| 2907 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2908 | int async_err = 0; |
| 2909 | |
| 2910 | do { |
| 2911 | Py_BEGIN_ALLOW_THREADS |
| 2912 | res = fchmod(fd, mode); |
| 2913 | Py_END_ALLOW_THREADS |
| 2914 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 2915 | if (res != 0) |
| 2916 | return (!async_err) ? posix_error() : NULL; |
| 2917 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2918 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2919 | } |
| 2920 | #endif /* HAVE_FCHMOD */ |
| 2921 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2922 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2923 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2924 | /*[clinic input] |
| 2925 | os.lchmod |
| 2926 | |
| 2927 | path: path_t |
| 2928 | mode: int |
| 2929 | |
| 2930 | Change the access permissions of a file, without following symbolic links. |
| 2931 | |
| 2932 | If path is a symlink, this affects the link itself rather than the target. |
| 2933 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 2934 | [clinic start generated code]*/ |
| 2935 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2936 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2937 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 2938 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2939 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2940 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2941 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2942 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2943 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2944 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2945 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2946 | return NULL; |
| 2947 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2948 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2949 | } |
| 2950 | #endif /* HAVE_LCHMOD */ |
| 2951 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2952 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2953 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2954 | /*[clinic input] |
| 2955 | os.chflags |
| 2956 | |
| 2957 | path: path_t |
| 2958 | flags: unsigned_long(bitwise=True) |
| 2959 | follow_symlinks: bool=True |
| 2960 | |
| 2961 | Set file flags. |
| 2962 | |
| 2963 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 2964 | link, chflags will change flags on the symbolic link itself instead of the |
| 2965 | file the link points to. |
| 2966 | follow_symlinks may not be implemented on your platform. If it is |
| 2967 | unavailable, using it will raise a NotImplementedError. |
| 2968 | |
| 2969 | [clinic start generated code]*/ |
| 2970 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2971 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2972 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2973 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2974 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2975 | { |
| 2976 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2977 | |
| 2978 | #ifndef HAVE_LCHFLAGS |
| 2979 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2980 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2981 | #endif |
| 2982 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2983 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2984 | #ifdef HAVE_LCHFLAGS |
| 2985 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2986 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2987 | else |
| 2988 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2989 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2990 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2991 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2992 | if (result) |
| 2993 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2994 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2995 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2996 | } |
| 2997 | #endif /* HAVE_CHFLAGS */ |
| 2998 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2999 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3000 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3001 | /*[clinic input] |
| 3002 | os.lchflags |
| 3003 | |
| 3004 | path: path_t |
| 3005 | flags: unsigned_long(bitwise=True) |
| 3006 | |
| 3007 | Set file flags. |
| 3008 | |
| 3009 | This function will not follow symbolic links. |
| 3010 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3011 | [clinic start generated code]*/ |
| 3012 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3013 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3014 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3015 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3016 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3017 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3018 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3019 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3020 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3021 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3022 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3023 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3024 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3025 | } |
| 3026 | #endif /* HAVE_LCHFLAGS */ |
| 3027 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3028 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3029 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3030 | /*[clinic input] |
| 3031 | os.chroot |
| 3032 | path: path_t |
| 3033 | |
| 3034 | Change root directory to path. |
| 3035 | |
| 3036 | [clinic start generated code]*/ |
| 3037 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3038 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3039 | os_chroot_impl(PyObject *module, path_t *path) |
| 3040 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3041 | { |
| 3042 | int res; |
| 3043 | Py_BEGIN_ALLOW_THREADS |
| 3044 | res = chroot(path->narrow); |
| 3045 | Py_END_ALLOW_THREADS |
| 3046 | if (res < 0) |
| 3047 | return path_error(path); |
| 3048 | Py_RETURN_NONE; |
| 3049 | } |
| 3050 | #endif /* HAVE_CHROOT */ |
| 3051 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3052 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3053 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3054 | /*[clinic input] |
| 3055 | os.fsync |
| 3056 | |
| 3057 | fd: fildes |
| 3058 | |
| 3059 | Force write of fd to disk. |
| 3060 | [clinic start generated code]*/ |
| 3061 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3062 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3063 | os_fsync_impl(PyObject *module, int fd) |
| 3064 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3065 | { |
| 3066 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3067 | } |
| 3068 | #endif /* HAVE_FSYNC */ |
| 3069 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3070 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3071 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3072 | /*[clinic input] |
| 3073 | os.sync |
| 3074 | |
| 3075 | Force write of everything to disk. |
| 3076 | [clinic start generated code]*/ |
| 3077 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3078 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3079 | os_sync_impl(PyObject *module) |
| 3080 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3081 | { |
| 3082 | Py_BEGIN_ALLOW_THREADS |
| 3083 | sync(); |
| 3084 | Py_END_ALLOW_THREADS |
| 3085 | Py_RETURN_NONE; |
| 3086 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3087 | #endif /* HAVE_SYNC */ |
| 3088 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3089 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3090 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3091 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3092 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3093 | #endif |
| 3094 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3095 | /*[clinic input] |
| 3096 | os.fdatasync |
| 3097 | |
| 3098 | fd: fildes |
| 3099 | |
| 3100 | Force write of fd to disk without forcing update of metadata. |
| 3101 | [clinic start generated code]*/ |
| 3102 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3103 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3104 | os_fdatasync_impl(PyObject *module, int fd) |
| 3105 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3106 | { |
| 3107 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3108 | } |
| 3109 | #endif /* HAVE_FDATASYNC */ |
| 3110 | |
| 3111 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3112 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3113 | /*[clinic input] |
| 3114 | os.chown |
| 3115 | |
| 3116 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3117 | 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] | 3118 | |
| 3119 | uid: uid_t |
| 3120 | |
| 3121 | gid: gid_t |
| 3122 | |
| 3123 | * |
| 3124 | |
| 3125 | dir_fd : dir_fd(requires='fchownat') = None |
| 3126 | If not None, it should be a file descriptor open to a directory, |
| 3127 | and path should be relative; path will then be relative to that |
| 3128 | directory. |
| 3129 | |
| 3130 | follow_symlinks: bool = True |
| 3131 | If False, and the last element of the path is a symbolic link, |
| 3132 | stat will examine the symbolic link itself instead of the file |
| 3133 | the link points to. |
| 3134 | |
| 3135 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3136 | |
| 3137 | path may always be specified as a string. |
| 3138 | On some platforms, path may also be specified as an open file descriptor. |
| 3139 | If this functionality is unavailable, using it raises an exception. |
| 3140 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3141 | and path should be relative; path will then be relative to that directory. |
| 3142 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3143 | link, chown will modify the symbolic link itself instead of the file the |
| 3144 | link points to. |
| 3145 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3146 | an open file descriptor. |
| 3147 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3148 | If they are unavailable, using them will raise a NotImplementedError. |
| 3149 | |
| 3150 | [clinic start generated code]*/ |
| 3151 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3152 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3153 | 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] | 3154 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3155 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3156 | { |
| 3157 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3158 | |
| 3159 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3160 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3161 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3162 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3163 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3164 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3165 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3166 | |
| 3167 | #ifdef __APPLE__ |
| 3168 | /* |
| 3169 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3170 | * (But we still have an lchown symbol because of weak-linking.) |
| 3171 | * It doesn't have fchownat either. So there's no possibility |
| 3172 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3173 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3174 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3175 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3176 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3177 | } |
| 3178 | #endif |
| 3179 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3180 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3181 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3182 | if (path->fd != -1) |
| 3183 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3184 | else |
| 3185 | #endif |
| 3186 | #ifdef HAVE_LCHOWN |
| 3187 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3188 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3189 | else |
| 3190 | #endif |
| 3191 | #ifdef HAVE_FCHOWNAT |
| 3192 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3193 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3194 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3195 | else |
| 3196 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3197 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3198 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3199 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3200 | if (result) |
| 3201 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3202 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3203 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3204 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3205 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3206 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3207 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3208 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3209 | /*[clinic input] |
| 3210 | os.fchown |
| 3211 | |
| 3212 | fd: int |
| 3213 | uid: uid_t |
| 3214 | gid: gid_t |
| 3215 | |
| 3216 | Change the owner and group id of the file specified by file descriptor. |
| 3217 | |
| 3218 | Equivalent to os.chown(fd, uid, gid). |
| 3219 | |
| 3220 | [clinic start generated code]*/ |
| 3221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3222 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3223 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3224 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3225 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3226 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3227 | int async_err = 0; |
| 3228 | |
| 3229 | do { |
| 3230 | Py_BEGIN_ALLOW_THREADS |
| 3231 | res = fchown(fd, uid, gid); |
| 3232 | Py_END_ALLOW_THREADS |
| 3233 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3234 | if (res != 0) |
| 3235 | return (!async_err) ? posix_error() : NULL; |
| 3236 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3237 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3238 | } |
| 3239 | #endif /* HAVE_FCHOWN */ |
| 3240 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3241 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3242 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3243 | /*[clinic input] |
| 3244 | os.lchown |
| 3245 | |
| 3246 | path : path_t |
| 3247 | uid: uid_t |
| 3248 | gid: gid_t |
| 3249 | |
| 3250 | Change the owner and group id of path to the numeric uid and gid. |
| 3251 | |
| 3252 | This function will not follow symbolic links. |
| 3253 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3254 | [clinic start generated code]*/ |
| 3255 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3256 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3257 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3258 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3259 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3260 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3261 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3262 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3263 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3264 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3265 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3266 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3267 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3268 | } |
| 3269 | #endif /* HAVE_LCHOWN */ |
| 3270 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3271 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3272 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3273 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3274 | { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3275 | char *buf, *tmpbuf; |
| 3276 | char *cwd; |
| 3277 | const size_t chunk = 1024; |
| 3278 | size_t buflen = 0; |
| 3279 | PyObject *obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3280 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3281 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3282 | if (!use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3283 | wchar_t wbuf[MAXPATHLEN]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3284 | wchar_t *wbuf2 = wbuf; |
| 3285 | PyObject *resobj; |
| 3286 | DWORD len; |
| 3287 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3288 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3289 | /* If the buffer is large enough, len does not include the |
| 3290 | terminating \0. If the buffer is too small, len includes |
| 3291 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3292 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3293 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3294 | if (wbuf2) |
| 3295 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3296 | } |
| 3297 | Py_END_ALLOW_THREADS |
| 3298 | if (!wbuf2) { |
| 3299 | PyErr_NoMemory(); |
| 3300 | return NULL; |
| 3301 | } |
| 3302 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3303 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3304 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3305 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3306 | } |
| 3307 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3308 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3309 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3310 | return resobj; |
| 3311 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3312 | |
| 3313 | if (win32_warn_bytes_api()) |
| 3314 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3315 | #endif |
| 3316 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3317 | buf = cwd = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3318 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3319 | do { |
| 3320 | buflen += chunk; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3321 | #ifdef MS_WINDOWS |
| 3322 | if (buflen > INT_MAX) { |
| 3323 | PyErr_NoMemory(); |
| 3324 | break; |
| 3325 | } |
| 3326 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3327 | tmpbuf = PyMem_RawRealloc(buf, buflen); |
| 3328 | if (tmpbuf == NULL) |
| 3329 | break; |
| 3330 | |
| 3331 | buf = tmpbuf; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3332 | #ifdef MS_WINDOWS |
| 3333 | cwd = getcwd(buf, (int)buflen); |
| 3334 | #else |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3335 | cwd = getcwd(buf, buflen); |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3336 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3337 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3338 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3339 | |
| 3340 | if (cwd == NULL) { |
| 3341 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3342 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3343 | } |
| 3344 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3345 | if (use_bytes) |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3346 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 3347 | else |
| 3348 | obj = PyUnicode_DecodeFSDefault(buf); |
| 3349 | PyMem_RawFree(buf); |
| 3350 | |
| 3351 | return obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3352 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3353 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3354 | |
| 3355 | /*[clinic input] |
| 3356 | os.getcwd |
| 3357 | |
| 3358 | Return a unicode string representing the current working directory. |
| 3359 | [clinic start generated code]*/ |
| 3360 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3361 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3362 | os_getcwd_impl(PyObject *module) |
| 3363 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3364 | { |
| 3365 | return posix_getcwd(0); |
| 3366 | } |
| 3367 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3368 | |
| 3369 | /*[clinic input] |
| 3370 | os.getcwdb |
| 3371 | |
| 3372 | Return a bytes string representing the current working directory. |
| 3373 | [clinic start generated code]*/ |
| 3374 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3375 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3376 | os_getcwdb_impl(PyObject *module) |
| 3377 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3378 | { |
| 3379 | return posix_getcwd(1); |
| 3380 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3381 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3382 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3383 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3384 | #define HAVE_LINK 1 |
| 3385 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3386 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3387 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3388 | /*[clinic input] |
| 3389 | |
| 3390 | os.link |
| 3391 | |
| 3392 | src : path_t |
| 3393 | dst : path_t |
| 3394 | * |
| 3395 | src_dir_fd : dir_fd = None |
| 3396 | dst_dir_fd : dir_fd = None |
| 3397 | follow_symlinks: bool = True |
| 3398 | |
| 3399 | Create a hard link to a file. |
| 3400 | |
| 3401 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3402 | descriptor open to a directory, and the respective path string (src or dst) |
| 3403 | should be relative; the path will then be relative to that directory. |
| 3404 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3405 | link, link will create a link to the symbolic link itself instead of the |
| 3406 | file the link points to. |
| 3407 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3408 | platform. If they are unavailable, using them will raise a |
| 3409 | NotImplementedError. |
| 3410 | [clinic start generated code]*/ |
| 3411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3412 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3413 | 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] | 3414 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3415 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3416 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3417 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3418 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3419 | #else |
| 3420 | int result; |
| 3421 | #endif |
| 3422 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3423 | #ifndef HAVE_LINKAT |
| 3424 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3425 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3426 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3427 | } |
| 3428 | #endif |
| 3429 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3430 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3431 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3432 | PyErr_SetString(PyExc_NotImplementedError, |
| 3433 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3434 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3435 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3436 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3437 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3438 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3439 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3440 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3441 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3442 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3443 | if (!result) |
| 3444 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3445 | #else |
| 3446 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3447 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3448 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3449 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3450 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3451 | result = linkat(src_dir_fd, src->narrow, |
| 3452 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3453 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3454 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3455 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3456 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3457 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3458 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3459 | if (result) |
| 3460 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3461 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3462 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3463 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3464 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3465 | #endif |
| 3466 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3467 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3468 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3469 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3470 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3471 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3472 | PyObject *v; |
| 3473 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3474 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3475 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3476 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3477 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3478 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3479 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3480 | WIN32_FIND_DATAW wFileData; |
| 3481 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3482 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3483 | if (!path->wide) { /* Default arg: "." */ |
| 3484 | po_wchars = L"."; |
| 3485 | len = 1; |
| 3486 | } else { |
| 3487 | po_wchars = path->wide; |
| 3488 | len = wcslen(path->wide); |
| 3489 | } |
| 3490 | /* The +5 is so we can append "\\*.*\0" */ |
| 3491 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3492 | if (!wnamebuf) { |
| 3493 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3494 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3495 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3496 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3497 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3498 | wchar_t wch = wnamebuf[len-1]; |
| 3499 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3500 | wnamebuf[len++] = SEP; |
| 3501 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3502 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3503 | if ((list = PyList_New(0)) == NULL) { |
| 3504 | goto exit; |
| 3505 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3506 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3507 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3508 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3509 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3510 | int error = GetLastError(); |
| 3511 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3512 | goto exit; |
| 3513 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3514 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3515 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3516 | } |
| 3517 | do { |
| 3518 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3519 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3520 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3521 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3522 | wcslen(wFileData.cFileName)); |
| 3523 | if (path->narrow && v) { |
| 3524 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3525 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3526 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3527 | Py_DECREF(list); |
| 3528 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3529 | break; |
| 3530 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3531 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3532 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3533 | Py_DECREF(list); |
| 3534 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3535 | break; |
| 3536 | } |
| 3537 | Py_DECREF(v); |
| 3538 | } |
| 3539 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3540 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3541 | Py_END_ALLOW_THREADS |
| 3542 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3543 | it got to the end of the directory. */ |
| 3544 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3545 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3546 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3547 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3548 | } |
| 3549 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3550 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3551 | exit: |
| 3552 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3553 | if (FindClose(hFindFile) == FALSE) { |
| 3554 | if (list != NULL) { |
| 3555 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3556 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3557 | } |
| 3558 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3559 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3560 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3561 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3562 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3563 | } /* end of _listdir_windows_no_opendir */ |
| 3564 | |
| 3565 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3566 | |
| 3567 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3568 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3569 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3570 | PyObject *v; |
| 3571 | DIR *dirp = NULL; |
| 3572 | struct dirent *ep; |
| 3573 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3574 | #ifdef HAVE_FDOPENDIR |
| 3575 | int fd = -1; |
| 3576 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3577 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3578 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3579 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3580 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3581 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3582 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3583 | if (fd == -1) |
| 3584 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3585 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3586 | return_str = 1; |
| 3587 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3588 | Py_BEGIN_ALLOW_THREADS |
| 3589 | dirp = fdopendir(fd); |
| 3590 | Py_END_ALLOW_THREADS |
| 3591 | } |
| 3592 | else |
| 3593 | #endif |
| 3594 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3595 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3596 | if (path->narrow) { |
| 3597 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3598 | /* only return bytes if they specified a bytes-like object */ |
| 3599 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3600 | } |
| 3601 | else { |
| 3602 | name = "."; |
| 3603 | return_str = 1; |
| 3604 | } |
| 3605 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3606 | Py_BEGIN_ALLOW_THREADS |
| 3607 | dirp = opendir(name); |
| 3608 | Py_END_ALLOW_THREADS |
| 3609 | } |
| 3610 | |
| 3611 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3612 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3613 | #ifdef HAVE_FDOPENDIR |
| 3614 | if (fd != -1) { |
| 3615 | Py_BEGIN_ALLOW_THREADS |
| 3616 | close(fd); |
| 3617 | Py_END_ALLOW_THREADS |
| 3618 | } |
| 3619 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3620 | goto exit; |
| 3621 | } |
| 3622 | if ((list = PyList_New(0)) == NULL) { |
| 3623 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3624 | } |
| 3625 | for (;;) { |
| 3626 | errno = 0; |
| 3627 | Py_BEGIN_ALLOW_THREADS |
| 3628 | ep = readdir(dirp); |
| 3629 | Py_END_ALLOW_THREADS |
| 3630 | if (ep == NULL) { |
| 3631 | if (errno == 0) { |
| 3632 | break; |
| 3633 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3634 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3635 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3636 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3637 | } |
| 3638 | } |
| 3639 | if (ep->d_name[0] == '.' && |
| 3640 | (NAMLEN(ep) == 1 || |
| 3641 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3642 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3643 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3644 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3645 | else |
| 3646 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3647 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3648 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3649 | break; |
| 3650 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3651 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3652 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3653 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3654 | break; |
| 3655 | } |
| 3656 | Py_DECREF(v); |
| 3657 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3658 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3659 | exit: |
| 3660 | if (dirp != NULL) { |
| 3661 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3662 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3663 | if (fd > -1) |
| 3664 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3665 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3666 | closedir(dirp); |
| 3667 | Py_END_ALLOW_THREADS |
| 3668 | } |
| 3669 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3670 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3671 | } /* end of _posix_listdir */ |
| 3672 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3673 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3674 | |
| 3675 | /*[clinic input] |
| 3676 | os.listdir |
| 3677 | |
| 3678 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3679 | |
| 3680 | Return a list containing the names of the files in the directory. |
| 3681 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3682 | 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] | 3683 | the filenames returned will also be bytes; in all other circumstances |
| 3684 | the filenames returned will be str. |
| 3685 | If path is None, uses the path='.'. |
| 3686 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3687 | the file descriptor must refer to a directory. |
| 3688 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3689 | |
| 3690 | The list is in arbitrary order. It does not include the special |
| 3691 | entries '.' and '..' even if they are present in the directory. |
| 3692 | |
| 3693 | |
| 3694 | [clinic start generated code]*/ |
| 3695 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3696 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3697 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3698 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3699 | { |
| 3700 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3701 | return _listdir_windows_no_opendir(path, NULL); |
| 3702 | #else |
| 3703 | return _posix_listdir(path, NULL); |
| 3704 | #endif |
| 3705 | } |
| 3706 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3707 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3708 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3709 | /*[clinic input] |
| 3710 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3711 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3712 | path: path_t |
| 3713 | / |
| 3714 | |
| 3715 | [clinic start generated code]*/ |
| 3716 | |
| 3717 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3718 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 3719 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3720 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3721 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
| 3722 | wchar_t *wtemp; |
| 3723 | DWORD result; |
| 3724 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3725 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3726 | result = GetFullPathNameW(path->wide, |
| 3727 | Py_ARRAY_LENGTH(woutbuf), |
| 3728 | woutbuf, &wtemp); |
| 3729 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
| 3730 | woutbufp = PyMem_New(wchar_t, result); |
| 3731 | if (!woutbufp) |
| 3732 | return PyErr_NoMemory(); |
| 3733 | result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3734 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3735 | if (result) { |
| 3736 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
| 3737 | if (path->narrow) |
| 3738 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3739 | } else |
| 3740 | v = win32_error_object("GetFullPathNameW", path->object); |
| 3741 | if (woutbufp != woutbuf) |
| 3742 | PyMem_Free(woutbufp); |
| 3743 | return v; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3744 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3745 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3746 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3747 | /*[clinic input] |
| 3748 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3749 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3750 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3751 | / |
| 3752 | |
| 3753 | A helper function for samepath on windows. |
| 3754 | [clinic start generated code]*/ |
| 3755 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3756 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3757 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 3758 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3759 | { |
| 3760 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3761 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 3762 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3763 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3764 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3765 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3766 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3767 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3768 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3769 | 0, /* desired access */ |
| 3770 | 0, /* share mode */ |
| 3771 | NULL, /* security attributes */ |
| 3772 | OPEN_EXISTING, |
| 3773 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3774 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3775 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3776 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3777 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3778 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3779 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3780 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3781 | |
| 3782 | /* We have a good handle to the target, use it to determine the |
| 3783 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3784 | while (1) { |
| 3785 | Py_BEGIN_ALLOW_THREADS |
| 3786 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 3787 | buf_size, VOLUME_NAME_DOS); |
| 3788 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3789 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3790 | if (!result_length) { |
| 3791 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 3792 | path->object); |
| 3793 | goto cleanup; |
| 3794 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3795 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3796 | if (result_length < buf_size) { |
| 3797 | break; |
| 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 | wchar_t *tmp; |
| 3801 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 3802 | result_length * sizeof(*tmp)); |
| 3803 | if (!tmp) { |
| 3804 | result = PyErr_NoMemory(); |
| 3805 | goto cleanup; |
| 3806 | } |
| 3807 | |
| 3808 | buf_size = result_length; |
| 3809 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3810 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3811 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3812 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3813 | if (path->narrow) |
| 3814 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3815 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3816 | cleanup: |
| 3817 | if (target_path != buf) { |
| 3818 | PyMem_Free(target_path); |
| 3819 | } |
| 3820 | CloseHandle(hFile); |
| 3821 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3822 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3823 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3824 | /*[clinic input] |
| 3825 | os._isdir |
| 3826 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3827 | path as arg: object |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3828 | / |
| 3829 | |
Serhiy Storchaka | 579f038 | 2016-11-08 20:21:22 +0200 | [diff] [blame] | 3830 | Return true if the pathname refers to an existing directory. |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3831 | [clinic start generated code]*/ |
| 3832 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3833 | static PyObject * |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3834 | os__isdir(PyObject *module, PyObject *arg) |
| 3835 | /*[clinic end generated code: output=404f334d85d4bf25 input=36cb6785874d479e]*/ |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3836 | { |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3837 | DWORD attributes; |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3838 | path_t path = PATH_T_INITIALIZE("_isdir", "path", 0, 0); |
| 3839 | |
| 3840 | if (!path_converter(arg, &path)) { |
| 3841 | if (PyErr_ExceptionMatches(PyExc_ValueError)) { |
| 3842 | PyErr_Clear(); |
| 3843 | Py_RETURN_FALSE; |
| 3844 | } |
| 3845 | return NULL; |
| 3846 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3847 | |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3848 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3849 | attributes = GetFileAttributesW(path.wide); |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3850 | Py_END_ALLOW_THREADS |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3851 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 3852 | path_cleanup(&path); |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3853 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3854 | Py_RETURN_FALSE; |
| 3855 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3856 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3857 | Py_RETURN_TRUE; |
| 3858 | else |
| 3859 | Py_RETURN_FALSE; |
| 3860 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3861 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3862 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3863 | /*[clinic input] |
| 3864 | os._getvolumepathname |
| 3865 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3866 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3867 | |
| 3868 | A helper function for ismount on Win32. |
| 3869 | [clinic start generated code]*/ |
| 3870 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3871 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3872 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 3873 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3874 | { |
| 3875 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3876 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3877 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3878 | BOOL ret; |
| 3879 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3880 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3881 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3882 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 3883 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3884 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 3885 | return NULL; |
| 3886 | } |
| 3887 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3888 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3889 | if (mountpath == NULL) |
| 3890 | return PyErr_NoMemory(); |
| 3891 | |
| 3892 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3893 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3894 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3895 | Py_END_ALLOW_THREADS |
| 3896 | |
| 3897 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3898 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3899 | goto exit; |
| 3900 | } |
| 3901 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3902 | if (path->narrow) |
| 3903 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3904 | |
| 3905 | exit: |
| 3906 | PyMem_Free(mountpath); |
| 3907 | return result; |
| 3908 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3909 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3910 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3911 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3912 | |
| 3913 | /*[clinic input] |
| 3914 | os.mkdir |
| 3915 | |
| 3916 | path : path_t |
| 3917 | |
| 3918 | mode: int = 0o777 |
| 3919 | |
| 3920 | * |
| 3921 | |
| 3922 | dir_fd : dir_fd(requires='mkdirat') = None |
| 3923 | |
| 3924 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3925 | |
| 3926 | Create a directory. |
| 3927 | |
| 3928 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3929 | and path should be relative; path will then be relative to that directory. |
| 3930 | dir_fd may not be implemented on your platform. |
| 3931 | If it is unavailable, using it will raise a NotImplementedError. |
| 3932 | |
| 3933 | The mode argument is ignored on Windows. |
| 3934 | [clinic start generated code]*/ |
| 3935 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3936 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3937 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 3938 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3939 | { |
| 3940 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3941 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3942 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3943 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3944 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3945 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3946 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3947 | if (!result) |
| 3948 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3949 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3950 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3951 | #if HAVE_MKDIRAT |
| 3952 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3953 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3954 | else |
| 3955 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 3956 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3957 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3958 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3959 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3960 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3961 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3962 | if (result < 0) |
| 3963 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3964 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3965 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3968 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3969 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3970 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3971 | #include <sys/resource.h> |
| 3972 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3973 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3974 | |
| 3975 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3976 | /*[clinic input] |
| 3977 | os.nice |
| 3978 | |
| 3979 | increment: int |
| 3980 | / |
| 3981 | |
| 3982 | Add increment to the priority of process and return the new priority. |
| 3983 | [clinic start generated code]*/ |
| 3984 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3985 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3986 | os_nice_impl(PyObject *module, int increment) |
| 3987 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3988 | { |
| 3989 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3990 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3991 | /* There are two flavours of 'nice': one that returns the new |
| 3992 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 3993 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3994 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3995 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3996 | If we are of the nice family that returns the new priority, we |
| 3997 | need to clear errno before the call, and check if errno is filled |
| 3998 | before calling posix_error() on a returnvalue of -1, because the |
| 3999 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4000 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4001 | errno = 0; |
| 4002 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4003 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4004 | if (value == 0) |
| 4005 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4006 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4007 | if (value == -1 && errno != 0) |
| 4008 | /* either nice() or getpriority() returned an error */ |
| 4009 | return posix_error(); |
| 4010 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4011 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4012 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4013 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4014 | |
| 4015 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4016 | /*[clinic input] |
| 4017 | os.getpriority |
| 4018 | |
| 4019 | which: int |
| 4020 | who: int |
| 4021 | |
| 4022 | Return program scheduling priority. |
| 4023 | [clinic start generated code]*/ |
| 4024 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4025 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4026 | os_getpriority_impl(PyObject *module, int which, int who) |
| 4027 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4028 | { |
| 4029 | int retval; |
| 4030 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4031 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4032 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4033 | if (errno != 0) |
| 4034 | return posix_error(); |
| 4035 | return PyLong_FromLong((long)retval); |
| 4036 | } |
| 4037 | #endif /* HAVE_GETPRIORITY */ |
| 4038 | |
| 4039 | |
| 4040 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4041 | /*[clinic input] |
| 4042 | os.setpriority |
| 4043 | |
| 4044 | which: int |
| 4045 | who: int |
| 4046 | priority: int |
| 4047 | |
| 4048 | Set program scheduling priority. |
| 4049 | [clinic start generated code]*/ |
| 4050 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4051 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4052 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4053 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4054 | { |
| 4055 | int retval; |
| 4056 | |
| 4057 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4058 | if (retval == -1) |
| 4059 | return posix_error(); |
| 4060 | Py_RETURN_NONE; |
| 4061 | } |
| 4062 | #endif /* HAVE_SETPRIORITY */ |
| 4063 | |
| 4064 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4065 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4066 | 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] | 4067 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4068 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4069 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4070 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4071 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4072 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4073 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4074 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4075 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4076 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4077 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4078 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4079 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4080 | #ifndef HAVE_RENAMEAT |
| 4081 | if (dir_fd_specified) { |
| 4082 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4083 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4084 | } |
| 4085 | #endif |
| 4086 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4087 | #ifdef MS_WINDOWS |
| 4088 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4089 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4090 | Py_END_ALLOW_THREADS |
| 4091 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4092 | if (!result) |
| 4093 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4094 | |
| 4095 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4096 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4097 | PyErr_Format(PyExc_ValueError, |
| 4098 | "%s: src and dst must be the same type", function_name); |
| 4099 | return NULL; |
| 4100 | } |
| 4101 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4102 | Py_BEGIN_ALLOW_THREADS |
| 4103 | #ifdef HAVE_RENAMEAT |
| 4104 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4105 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4106 | else |
| 4107 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4108 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4109 | Py_END_ALLOW_THREADS |
| 4110 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4111 | if (result) |
| 4112 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4113 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4114 | Py_RETURN_NONE; |
| 4115 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4116 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4117 | |
| 4118 | /*[clinic input] |
| 4119 | os.rename |
| 4120 | |
| 4121 | src : path_t |
| 4122 | dst : path_t |
| 4123 | * |
| 4124 | src_dir_fd : dir_fd = None |
| 4125 | dst_dir_fd : dir_fd = None |
| 4126 | |
| 4127 | Rename a file or directory. |
| 4128 | |
| 4129 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4130 | descriptor open to a directory, and the respective path string (src or dst) |
| 4131 | should be relative; the path will then be relative to that directory. |
| 4132 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4133 | If they are unavailable, using them will raise a NotImplementedError. |
| 4134 | [clinic start generated code]*/ |
| 4135 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4136 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4137 | 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] | 4138 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4139 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4140 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4141 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4142 | } |
| 4143 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4144 | |
| 4145 | /*[clinic input] |
| 4146 | os.replace = os.rename |
| 4147 | |
| 4148 | Rename a file or directory, overwriting the destination. |
| 4149 | |
| 4150 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4151 | descriptor open to a directory, and the respective path string (src or dst) |
| 4152 | should be relative; the path will then be relative to that directory. |
| 4153 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4154 | If they are unavailable, using them will raise a NotImplementedError." |
| 4155 | [clinic start generated code]*/ |
| 4156 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4157 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4158 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4159 | int dst_dir_fd) |
| 4160 | /*[clinic end generated code: output=1968c02e7857422b input=25515dfb107c8421]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4161 | { |
| 4162 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4163 | } |
| 4164 | |
| 4165 | |
| 4166 | /*[clinic input] |
| 4167 | os.rmdir |
| 4168 | |
| 4169 | path: path_t |
| 4170 | * |
| 4171 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4172 | |
| 4173 | Remove a directory. |
| 4174 | |
| 4175 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4176 | and path should be relative; path will then be relative to that directory. |
| 4177 | dir_fd may not be implemented on your platform. |
| 4178 | If it is unavailable, using it will raise a NotImplementedError. |
| 4179 | [clinic start generated code]*/ |
| 4180 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4181 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4182 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4183 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4184 | { |
| 4185 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4186 | |
| 4187 | Py_BEGIN_ALLOW_THREADS |
| 4188 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4189 | /* Windows, success=1, UNIX, success=0 */ |
| 4190 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4191 | #else |
| 4192 | #ifdef HAVE_UNLINKAT |
| 4193 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4194 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4195 | else |
| 4196 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4197 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4198 | #endif |
| 4199 | Py_END_ALLOW_THREADS |
| 4200 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4201 | if (result) |
| 4202 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4203 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4204 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4207 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4208 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4209 | #ifdef MS_WINDOWS |
| 4210 | /*[clinic input] |
| 4211 | os.system -> long |
| 4212 | |
| 4213 | command: Py_UNICODE |
| 4214 | |
| 4215 | Execute the command in a subshell. |
| 4216 | [clinic start generated code]*/ |
| 4217 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4218 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4219 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4220 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4221 | { |
| 4222 | long result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4223 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4224 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4225 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4226 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4227 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4228 | return result; |
| 4229 | } |
| 4230 | #else /* MS_WINDOWS */ |
| 4231 | /*[clinic input] |
| 4232 | os.system -> long |
| 4233 | |
| 4234 | command: FSConverter |
| 4235 | |
| 4236 | Execute the command in a subshell. |
| 4237 | [clinic start generated code]*/ |
| 4238 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4239 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4240 | os_system_impl(PyObject *module, PyObject *command) |
| 4241 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4242 | { |
| 4243 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4244 | const char *bytes = PyBytes_AsString(command); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4245 | Py_BEGIN_ALLOW_THREADS |
| 4246 | result = system(bytes); |
| 4247 | Py_END_ALLOW_THREADS |
| 4248 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4249 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4250 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4251 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4252 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4253 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4254 | /*[clinic input] |
| 4255 | os.umask |
| 4256 | |
| 4257 | mask: int |
| 4258 | / |
| 4259 | |
| 4260 | Set the current numeric umask and return the previous umask. |
| 4261 | [clinic start generated code]*/ |
| 4262 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4263 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4264 | os_umask_impl(PyObject *module, int mask) |
| 4265 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4266 | { |
| 4267 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4268 | if (i < 0) |
| 4269 | return posix_error(); |
| 4270 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4273 | #ifdef MS_WINDOWS |
| 4274 | |
| 4275 | /* override the default DeleteFileW behavior so that directory |
| 4276 | symlinks can be removed with this function, the same as with |
| 4277 | Unix symlinks */ |
| 4278 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4279 | { |
| 4280 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4281 | WIN32_FIND_DATAW find_data; |
| 4282 | HANDLE find_data_handle; |
| 4283 | int is_directory = 0; |
| 4284 | int is_link = 0; |
| 4285 | |
| 4286 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4287 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4288 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4289 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4290 | it is a symlink */ |
| 4291 | if(is_directory && |
| 4292 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4293 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4294 | |
| 4295 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4296 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4297 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4298 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4299 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4300 | FindClose(find_data_handle); |
| 4301 | } |
| 4302 | } |
| 4303 | } |
| 4304 | |
| 4305 | if (is_directory && is_link) |
| 4306 | return RemoveDirectoryW(lpFileName); |
| 4307 | |
| 4308 | return DeleteFileW(lpFileName); |
| 4309 | } |
| 4310 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4311 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4312 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4313 | /*[clinic input] |
| 4314 | os.unlink |
| 4315 | |
| 4316 | path: path_t |
| 4317 | * |
| 4318 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4319 | |
| 4320 | Remove a file (same as remove()). |
| 4321 | |
| 4322 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4323 | and path should be relative; path will then be relative to that directory. |
| 4324 | dir_fd may not be implemented on your platform. |
| 4325 | If it is unavailable, using it will raise a NotImplementedError. |
| 4326 | |
| 4327 | [clinic start generated code]*/ |
| 4328 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4329 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4330 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4331 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4332 | { |
| 4333 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4334 | |
| 4335 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4336 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4337 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4338 | /* Windows, success=1, UNIX, success=0 */ |
| 4339 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4340 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4341 | #ifdef HAVE_UNLINKAT |
| 4342 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4343 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4344 | else |
| 4345 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4346 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4347 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4348 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4349 | Py_END_ALLOW_THREADS |
| 4350 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4351 | if (result) |
| 4352 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4353 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4354 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4355 | } |
| 4356 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4357 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4358 | /*[clinic input] |
| 4359 | os.remove = os.unlink |
| 4360 | |
| 4361 | Remove a file (same as unlink()). |
| 4362 | |
| 4363 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4364 | and path should be relative; path will then be relative to that directory. |
| 4365 | dir_fd may not be implemented on your platform. |
| 4366 | If it is unavailable, using it will raise a NotImplementedError. |
| 4367 | [clinic start generated code]*/ |
| 4368 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4369 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4370 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4371 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4372 | { |
| 4373 | return os_unlink_impl(module, path, dir_fd); |
| 4374 | } |
| 4375 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4376 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4377 | static PyStructSequence_Field uname_result_fields[] = { |
| 4378 | {"sysname", "operating system name"}, |
| 4379 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4380 | {"release", "operating system release"}, |
| 4381 | {"version", "operating system version"}, |
| 4382 | {"machine", "hardware identifier"}, |
| 4383 | {NULL} |
| 4384 | }; |
| 4385 | |
| 4386 | PyDoc_STRVAR(uname_result__doc__, |
| 4387 | "uname_result: Result from os.uname().\n\n\ |
| 4388 | This object may be accessed either as a tuple of\n\ |
| 4389 | (sysname, nodename, release, version, machine),\n\ |
| 4390 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4391 | \n\ |
| 4392 | See os.uname for more information."); |
| 4393 | |
| 4394 | static PyStructSequence_Desc uname_result_desc = { |
| 4395 | "uname_result", /* name */ |
| 4396 | uname_result__doc__, /* doc */ |
| 4397 | uname_result_fields, |
| 4398 | 5 |
| 4399 | }; |
| 4400 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 4401 | static PyTypeObject* UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4402 | |
| 4403 | |
| 4404 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4405 | /*[clinic input] |
| 4406 | os.uname |
| 4407 | |
| 4408 | Return an object identifying the current operating system. |
| 4409 | |
| 4410 | The object behaves like a named tuple with the following fields: |
| 4411 | (sysname, nodename, release, version, machine) |
| 4412 | |
| 4413 | [clinic start generated code]*/ |
| 4414 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4415 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4416 | os_uname_impl(PyObject *module) |
| 4417 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4418 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4419 | struct utsname u; |
| 4420 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4421 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4422 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4423 | Py_BEGIN_ALLOW_THREADS |
| 4424 | res = uname(&u); |
| 4425 | Py_END_ALLOW_THREADS |
| 4426 | if (res < 0) |
| 4427 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4428 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 4429 | value = PyStructSequence_New(UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4430 | if (value == NULL) |
| 4431 | return NULL; |
| 4432 | |
| 4433 | #define SET(i, field) \ |
| 4434 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4435 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4436 | if (!o) { \ |
| 4437 | Py_DECREF(value); \ |
| 4438 | return NULL; \ |
| 4439 | } \ |
| 4440 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4441 | } \ |
| 4442 | |
| 4443 | SET(0, u.sysname); |
| 4444 | SET(1, u.nodename); |
| 4445 | SET(2, u.release); |
| 4446 | SET(3, u.version); |
| 4447 | SET(4, u.machine); |
| 4448 | |
| 4449 | #undef SET |
| 4450 | |
| 4451 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4452 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4453 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4454 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4455 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4456 | |
| 4457 | typedef struct { |
| 4458 | int now; |
| 4459 | time_t atime_s; |
| 4460 | long atime_ns; |
| 4461 | time_t mtime_s; |
| 4462 | long mtime_ns; |
| 4463 | } utime_t; |
| 4464 | |
| 4465 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4466 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4467 | * they also intentionally leak the declaration of a pointer named "time" |
| 4468 | */ |
| 4469 | #define UTIME_TO_TIMESPEC \ |
| 4470 | struct timespec ts[2]; \ |
| 4471 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4472 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4473 | time = NULL; \ |
| 4474 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4475 | ts[0].tv_sec = ut->atime_s; \ |
| 4476 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4477 | ts[1].tv_sec = ut->mtime_s; \ |
| 4478 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4479 | time = ts; \ |
| 4480 | } \ |
| 4481 | |
| 4482 | #define UTIME_TO_TIMEVAL \ |
| 4483 | struct timeval tv[2]; \ |
| 4484 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4485 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4486 | time = NULL; \ |
| 4487 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4488 | tv[0].tv_sec = ut->atime_s; \ |
| 4489 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4490 | tv[1].tv_sec = ut->mtime_s; \ |
| 4491 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4492 | time = tv; \ |
| 4493 | } \ |
| 4494 | |
| 4495 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4496 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4497 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4498 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4499 | time = NULL; \ |
| 4500 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4501 | u.actime = ut->atime_s; \ |
| 4502 | u.modtime = ut->mtime_s; \ |
| 4503 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4504 | } |
| 4505 | |
| 4506 | #define UTIME_TO_TIME_T \ |
| 4507 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4508 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4509 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4510 | time = NULL; \ |
| 4511 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4512 | timet[0] = ut->atime_s; \ |
| 4513 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4514 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4515 | } \ |
| 4516 | |
| 4517 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4518 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4519 | |
| 4520 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4521 | 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] | 4522 | { |
| 4523 | #ifdef HAVE_UTIMENSAT |
| 4524 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4525 | UTIME_TO_TIMESPEC; |
| 4526 | return utimensat(dir_fd, path, time, flags); |
| 4527 | #elif defined(HAVE_FUTIMESAT) |
| 4528 | UTIME_TO_TIMEVAL; |
| 4529 | /* |
| 4530 | * follow_symlinks will never be false here; |
| 4531 | * we only allow !follow_symlinks and dir_fd together |
| 4532 | * if we have utimensat() |
| 4533 | */ |
| 4534 | assert(follow_symlinks); |
| 4535 | return futimesat(dir_fd, path, time); |
| 4536 | #endif |
| 4537 | } |
| 4538 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4539 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4540 | #else |
| 4541 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4542 | #endif |
| 4543 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4544 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4545 | |
| 4546 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4547 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4548 | { |
| 4549 | #ifdef HAVE_FUTIMENS |
| 4550 | UTIME_TO_TIMESPEC; |
| 4551 | return futimens(fd, time); |
| 4552 | #else |
| 4553 | UTIME_TO_TIMEVAL; |
| 4554 | return futimes(fd, time); |
| 4555 | #endif |
| 4556 | } |
| 4557 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4558 | #define PATH_UTIME_HAVE_FD 1 |
| 4559 | #else |
| 4560 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4561 | #endif |
| 4562 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4563 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4564 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4565 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4566 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4567 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4568 | |
| 4569 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4570 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4571 | { |
| 4572 | #ifdef HAVE_UTIMENSAT |
| 4573 | UTIME_TO_TIMESPEC; |
| 4574 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4575 | #else |
| 4576 | UTIME_TO_TIMEVAL; |
| 4577 | return lutimes(path, time); |
| 4578 | #endif |
| 4579 | } |
| 4580 | |
| 4581 | #endif |
| 4582 | |
| 4583 | #ifndef MS_WINDOWS |
| 4584 | |
| 4585 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4586 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4587 | { |
| 4588 | #ifdef HAVE_UTIMENSAT |
| 4589 | UTIME_TO_TIMESPEC; |
| 4590 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4591 | #elif defined(HAVE_UTIMES) |
| 4592 | UTIME_TO_TIMEVAL; |
| 4593 | return utimes(path, time); |
| 4594 | #elif defined(HAVE_UTIME_H) |
| 4595 | UTIME_TO_UTIMBUF; |
| 4596 | return utime(path, time); |
| 4597 | #else |
| 4598 | UTIME_TO_TIME_T; |
| 4599 | return utime(path, time); |
| 4600 | #endif |
| 4601 | } |
| 4602 | |
| 4603 | #endif |
| 4604 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4605 | static int |
| 4606 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4607 | { |
| 4608 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4609 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4610 | divmod = PyNumber_Divmod(py_long, billion); |
| 4611 | if (!divmod) |
| 4612 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4613 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 4614 | PyErr_Format(PyExc_TypeError, |
| 4615 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
| 4616 | Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name); |
| 4617 | goto exit; |
| 4618 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4619 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4620 | if ((*s == -1) && PyErr_Occurred()) |
| 4621 | goto exit; |
| 4622 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4623 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4624 | goto exit; |
| 4625 | |
| 4626 | result = 1; |
| 4627 | exit: |
| 4628 | Py_XDECREF(divmod); |
| 4629 | return result; |
| 4630 | } |
| 4631 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4632 | |
| 4633 | /*[clinic input] |
| 4634 | os.utime |
| 4635 | |
| 4636 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
| 4637 | times: object = NULL |
| 4638 | * |
| 4639 | ns: object = NULL |
| 4640 | dir_fd: dir_fd(requires='futimensat') = None |
| 4641 | follow_symlinks: bool=True |
| 4642 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4643 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4644 | |
| 4645 | Set the access and modified time of path. |
| 4646 | |
| 4647 | path may always be specified as a string. |
| 4648 | On some platforms, path may also be specified as an open file descriptor. |
| 4649 | If this functionality is unavailable, using it raises an exception. |
| 4650 | |
| 4651 | If times is not None, it must be a tuple (atime, mtime); |
| 4652 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4653 | 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] | 4654 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4655 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4656 | 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] | 4657 | Specifying tuples for both times and ns is an error. |
| 4658 | |
| 4659 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4660 | and path should be relative; path will then be relative to that directory. |
| 4661 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4662 | link, utime will modify the symbolic link itself instead of the file the |
| 4663 | link points to. |
| 4664 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4665 | as an open file descriptor. |
| 4666 | dir_fd and follow_symlinks may not be available on your platform. |
| 4667 | If they are unavailable, using them will raise a NotImplementedError. |
| 4668 | |
| 4669 | [clinic start generated code]*/ |
| 4670 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4671 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4672 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4673 | int dir_fd, int follow_symlinks) |
| 4674 | /*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4675 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4676 | #ifdef MS_WINDOWS |
| 4677 | HANDLE hFile; |
| 4678 | FILETIME atime, mtime; |
| 4679 | #else |
| 4680 | int result; |
| 4681 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4682 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4683 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4684 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4685 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4686 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4687 | if (times && (times != Py_None) && ns) { |
| 4688 | PyErr_SetString(PyExc_ValueError, |
| 4689 | "utime: you may specify either 'times'" |
| 4690 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4691 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4695 | time_t a_sec, m_sec; |
| 4696 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4697 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4698 | PyErr_SetString(PyExc_TypeError, |
| 4699 | "utime: 'times' must be either" |
| 4700 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4701 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4702 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4703 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4704 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4705 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4706 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4707 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4708 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4709 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4710 | utime.atime_s = a_sec; |
| 4711 | utime.atime_ns = a_nsec; |
| 4712 | utime.mtime_s = m_sec; |
| 4713 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4714 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4715 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4716 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4717 | PyErr_SetString(PyExc_TypeError, |
| 4718 | "utime: 'ns' must be a tuple of two ints"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4719 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4720 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4721 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4722 | 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] | 4723 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4724 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4725 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4726 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4727 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4728 | } |
| 4729 | else { |
| 4730 | /* times and ns are both None/unspecified. use "now". */ |
| 4731 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4732 | } |
| 4733 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4734 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4735 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4736 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4737 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4738 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4739 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4740 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4741 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4742 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4743 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4744 | #if !defined(HAVE_UTIMENSAT) |
| 4745 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4746 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4747 | "utime: cannot use dir_fd and follow_symlinks " |
| 4748 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4749 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4750 | } |
| 4751 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4752 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4753 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4754 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4755 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 4756 | NULL, OPEN_EXISTING, |
| 4757 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4758 | Py_END_ALLOW_THREADS |
| 4759 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4760 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4761 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4762 | } |
| 4763 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4764 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4765 | GetSystemTimeAsFileTime(&mtime); |
| 4766 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4767 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4768 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4769 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4770 | _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] | 4771 | } |
| 4772 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4773 | /* Avoid putting the file name into the error here, |
| 4774 | as that may confuse the user into believing that |
| 4775 | something is wrong with the file, when it also |
| 4776 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4777 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4778 | CloseHandle(hFile); |
| 4779 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4780 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4781 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4782 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4783 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4784 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4785 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4786 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4787 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4788 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4789 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4790 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4791 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4792 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4793 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4794 | else |
| 4795 | #endif |
| 4796 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4797 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4798 | if (path->fd != -1) |
| 4799 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4800 | else |
| 4801 | #endif |
| 4802 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4803 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4804 | |
| 4805 | Py_END_ALLOW_THREADS |
| 4806 | |
| 4807 | if (result < 0) { |
| 4808 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4809 | posix_error(); |
| 4810 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4811 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4812 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4813 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4814 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4815 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4816 | } |
| 4817 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4818 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4819 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4820 | |
| 4821 | /*[clinic input] |
| 4822 | os._exit |
| 4823 | |
| 4824 | status: int |
| 4825 | |
| 4826 | Exit to the system with specified status, without normal exit processing. |
| 4827 | [clinic start generated code]*/ |
| 4828 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4829 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4830 | os__exit_impl(PyObject *module, int status) |
| 4831 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4832 | { |
| 4833 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4834 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4835 | } |
| 4836 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4837 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4838 | #define EXECV_CHAR wchar_t |
| 4839 | #else |
| 4840 | #define EXECV_CHAR char |
| 4841 | #endif |
| 4842 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4843 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4844 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4845 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4846 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4847 | Py_ssize_t i; |
| 4848 | for (i = 0; i < count; i++) |
| 4849 | PyMem_Free(array[i]); |
| 4850 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4851 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4852 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4853 | static int |
| 4854 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4855 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4856 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4857 | PyObject *ub; |
| 4858 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4859 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4860 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4861 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4862 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 4863 | if (*out) |
| 4864 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4865 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4866 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4867 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4868 | size = PyBytes_GET_SIZE(ub); |
| 4869 | *out = PyMem_Malloc(size + 1); |
| 4870 | if (*out) { |
| 4871 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 4872 | result = 1; |
| 4873 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4874 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4875 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4876 | Py_DECREF(ub); |
| 4877 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4878 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4879 | #endif |
| 4880 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4881 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4882 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4883 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4884 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4885 | Py_ssize_t i, pos, envc; |
| 4886 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4887 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4888 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4889 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4890 | i = PyMapping_Size(env); |
| 4891 | if (i < 0) |
| 4892 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4893 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4894 | if (envlist == NULL) { |
| 4895 | PyErr_NoMemory(); |
| 4896 | return NULL; |
| 4897 | } |
| 4898 | envc = 0; |
| 4899 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4900 | if (!keys) |
| 4901 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4902 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4903 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4904 | goto error; |
| 4905 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4906 | PyErr_Format(PyExc_TypeError, |
| 4907 | "env.keys() or env.values() is not a list"); |
| 4908 | goto error; |
| 4909 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4910 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4911 | for (pos = 0; pos < i; pos++) { |
| 4912 | key = PyList_GetItem(keys, pos); |
| 4913 | val = PyList_GetItem(vals, pos); |
| 4914 | if (!key || !val) |
| 4915 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4916 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4917 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4918 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 4919 | goto error; |
| 4920 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 4921 | Py_DECREF(key2); |
| 4922 | goto error; |
| 4923 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4924 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 4925 | defining hidden environment variables. */ |
| 4926 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 4927 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 4928 | { |
| 4929 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 4930 | Py_DECREF(key2); |
| 4931 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4932 | goto error; |
| 4933 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4934 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 4935 | #else |
| 4936 | if (!PyUnicode_FSConverter(key, &key2)) |
| 4937 | goto error; |
| 4938 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 4939 | Py_DECREF(key2); |
| 4940 | goto error; |
| 4941 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4942 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 4943 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 4944 | { |
| 4945 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 4946 | Py_DECREF(key2); |
| 4947 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4948 | goto error; |
| 4949 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4950 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 4951 | PyBytes_AS_STRING(val2)); |
| 4952 | #endif |
| 4953 | Py_DECREF(key2); |
| 4954 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4955 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4956 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4957 | |
| 4958 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 4959 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4960 | goto error; |
| 4961 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4962 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4963 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4964 | } |
| 4965 | Py_DECREF(vals); |
| 4966 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4967 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4968 | envlist[envc] = 0; |
| 4969 | *envc_ptr = envc; |
| 4970 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4971 | |
| 4972 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4973 | Py_XDECREF(keys); |
| 4974 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4975 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4976 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4977 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4978 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4979 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4980 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4981 | { |
| 4982 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4983 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4984 | if (argvlist == NULL) { |
| 4985 | PyErr_NoMemory(); |
| 4986 | return NULL; |
| 4987 | } |
| 4988 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4989 | PyObject* item = PySequence_ITEM(argv, i); |
| 4990 | if (item == NULL) |
| 4991 | goto fail; |
| 4992 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4993 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4994 | goto fail; |
| 4995 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4996 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4997 | } |
| 4998 | argvlist[*argc] = NULL; |
| 4999 | return argvlist; |
| 5000 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5001 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5002 | free_string_array(argvlist, *argc); |
| 5003 | return NULL; |
| 5004 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5005 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5006 | #endif |
| 5007 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5008 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5009 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5010 | /*[clinic input] |
| 5011 | os.execv |
| 5012 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5013 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5014 | Path of executable file. |
| 5015 | argv: object |
| 5016 | Tuple or list of strings. |
| 5017 | / |
| 5018 | |
| 5019 | Execute an executable path with arguments, replacing current process. |
| 5020 | [clinic start generated code]*/ |
| 5021 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5022 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5023 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5024 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5025 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5026 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5027 | Py_ssize_t argc; |
| 5028 | |
| 5029 | /* execv has two arguments: (path, argv), where |
| 5030 | argv is a list or tuple of strings. */ |
| 5031 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5032 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5033 | PyErr_SetString(PyExc_TypeError, |
| 5034 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5035 | return NULL; |
| 5036 | } |
| 5037 | argc = PySequence_Size(argv); |
| 5038 | if (argc < 1) { |
| 5039 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5040 | return NULL; |
| 5041 | } |
| 5042 | |
| 5043 | argvlist = parse_arglist(argv, &argc); |
| 5044 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5045 | return NULL; |
| 5046 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5047 | if (!argvlist[0][0]) { |
| 5048 | PyErr_SetString(PyExc_ValueError, |
| 5049 | "execv() arg 2 first element cannot be empty"); |
| 5050 | free_string_array(argvlist, argc); |
| 5051 | return NULL; |
| 5052 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5053 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5054 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5055 | #ifdef HAVE_WEXECV |
| 5056 | _wexecv(path->wide, argvlist); |
| 5057 | #else |
| 5058 | execv(path->narrow, argvlist); |
| 5059 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5060 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5061 | |
| 5062 | /* If we get here it's definitely an error */ |
| 5063 | |
| 5064 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5065 | return posix_error(); |
| 5066 | } |
| 5067 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5068 | |
| 5069 | /*[clinic input] |
| 5070 | os.execve |
| 5071 | |
| 5072 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5073 | Path of executable file. |
| 5074 | argv: object |
| 5075 | Tuple or list of strings. |
| 5076 | env: object |
| 5077 | Dictionary of strings mapping to strings. |
| 5078 | |
| 5079 | Execute an executable path with arguments, replacing current process. |
| 5080 | [clinic start generated code]*/ |
| 5081 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5082 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5083 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5084 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5085 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5086 | EXECV_CHAR **argvlist = NULL; |
| 5087 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5088 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5089 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5090 | /* execve has three arguments: (path, argv, env), where |
| 5091 | argv is a list or tuple of strings and env is a dictionary |
| 5092 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5093 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5094 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5095 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5096 | "execve: argv must be a tuple or list"); |
| 5097 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5098 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5099 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5100 | if (argc < 1) { |
| 5101 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5102 | return NULL; |
| 5103 | } |
| 5104 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5105 | if (!PyMapping_Check(env)) { |
| 5106 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5107 | "execve: environment must be a mapping object"); |
| 5108 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5109 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5110 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5111 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5112 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5113 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5114 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5115 | if (!argvlist[0][0]) { |
| 5116 | PyErr_SetString(PyExc_ValueError, |
| 5117 | "execve: argv first element cannot be empty"); |
| 5118 | goto fail; |
| 5119 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5120 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5121 | envlist = parse_envlist(env, &envc); |
| 5122 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5123 | goto fail; |
| 5124 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5125 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5126 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5127 | if (path->fd > -1) |
| 5128 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5129 | else |
| 5130 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5131 | #ifdef HAVE_WEXECV |
| 5132 | _wexecve(path->wide, argvlist, envlist); |
| 5133 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5134 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5135 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5136 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5137 | |
| 5138 | /* If we get here it's definitely an error */ |
| 5139 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5140 | posix_path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5141 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5142 | free_string_array(envlist, envc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5143 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5144 | if (argvlist) |
| 5145 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5146 | return NULL; |
| 5147 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5148 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5149 | #endif /* HAVE_EXECV */ |
| 5150 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5151 | #ifdef HAVE_POSIX_SPAWN |
| 5152 | |
| 5153 | enum posix_spawn_file_actions_identifier { |
| 5154 | POSIX_SPAWN_OPEN, |
| 5155 | POSIX_SPAWN_CLOSE, |
| 5156 | POSIX_SPAWN_DUP2 |
| 5157 | }; |
| 5158 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5159 | #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] | 5160 | static int |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5161 | convert_sched_param(PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5162 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5163 | |
| 5164 | static int |
| 5165 | parse_posix_spawn_flags(PyObject *setpgroup, int resetids, PyObject *setsigmask, |
| 5166 | PyObject *setsigdef, PyObject *scheduler, |
| 5167 | posix_spawnattr_t *attrp) |
| 5168 | { |
| 5169 | long all_flags = 0; |
| 5170 | |
| 5171 | errno = posix_spawnattr_init(attrp); |
| 5172 | if (errno) { |
| 5173 | posix_error(); |
| 5174 | return -1; |
| 5175 | } |
| 5176 | |
| 5177 | if (setpgroup) { |
| 5178 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5179 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5180 | goto fail; |
| 5181 | } |
| 5182 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5183 | if (errno) { |
| 5184 | posix_error(); |
| 5185 | goto fail; |
| 5186 | } |
| 5187 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5188 | } |
| 5189 | |
| 5190 | if (resetids) { |
| 5191 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5192 | } |
| 5193 | |
| 5194 | if (setsigmask) { |
| 5195 | sigset_t set; |
| 5196 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5197 | goto fail; |
| 5198 | } |
| 5199 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5200 | if (errno) { |
| 5201 | posix_error(); |
| 5202 | goto fail; |
| 5203 | } |
| 5204 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5205 | } |
| 5206 | |
| 5207 | if (setsigdef) { |
| 5208 | sigset_t set; |
| 5209 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5210 | goto fail; |
| 5211 | } |
| 5212 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5213 | if (errno) { |
| 5214 | posix_error(); |
| 5215 | goto fail; |
| 5216 | } |
| 5217 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5218 | } |
| 5219 | |
| 5220 | if (scheduler) { |
| 5221 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5222 | PyObject *py_schedpolicy; |
| 5223 | struct sched_param schedparam; |
| 5224 | |
| 5225 | if (!PyArg_ParseTuple(scheduler, "OO&" |
| 5226 | ";A scheduler tuple must have two elements", |
| 5227 | &py_schedpolicy, convert_sched_param, &schedparam)) { |
| 5228 | goto fail; |
| 5229 | } |
| 5230 | if (py_schedpolicy != Py_None) { |
| 5231 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5232 | |
| 5233 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5234 | goto fail; |
| 5235 | } |
| 5236 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5237 | if (errno) { |
| 5238 | posix_error(); |
| 5239 | goto fail; |
| 5240 | } |
| 5241 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5242 | } |
| 5243 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5244 | if (errno) { |
| 5245 | posix_error(); |
| 5246 | goto fail; |
| 5247 | } |
| 5248 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5249 | #else |
| 5250 | PyErr_SetString(PyExc_NotImplementedError, |
| 5251 | "The scheduler option is not supported in this system."); |
| 5252 | goto fail; |
| 5253 | #endif |
| 5254 | } |
| 5255 | |
| 5256 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5257 | if (errno) { |
| 5258 | posix_error(); |
| 5259 | goto fail; |
| 5260 | } |
| 5261 | |
| 5262 | return 0; |
| 5263 | |
| 5264 | fail: |
| 5265 | (void)posix_spawnattr_destroy(attrp); |
| 5266 | return -1; |
| 5267 | } |
| 5268 | |
| 5269 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5270 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5271 | posix_spawn_file_actions_t *file_actionsp, |
| 5272 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5273 | { |
| 5274 | PyObject *seq; |
| 5275 | PyObject *file_action = NULL; |
| 5276 | PyObject *tag_obj; |
| 5277 | |
| 5278 | seq = PySequence_Fast(file_actions, |
| 5279 | "file_actions must be a sequence or None"); |
| 5280 | if (seq == NULL) { |
| 5281 | return -1; |
| 5282 | } |
| 5283 | |
| 5284 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5285 | if (errno) { |
| 5286 | posix_error(); |
| 5287 | Py_DECREF(seq); |
| 5288 | return -1; |
| 5289 | } |
| 5290 | |
| 5291 | for (int i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) { |
| 5292 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5293 | Py_INCREF(file_action); |
| 5294 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5295 | PyErr_SetString(PyExc_TypeError, |
| 5296 | "Each file_actions element must be a non-empty tuple"); |
| 5297 | goto fail; |
| 5298 | } |
| 5299 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5300 | if (tag == -1 && PyErr_Occurred()) { |
| 5301 | goto fail; |
| 5302 | } |
| 5303 | |
| 5304 | /* Populate the file_actions object */ |
| 5305 | switch (tag) { |
| 5306 | case POSIX_SPAWN_OPEN: { |
| 5307 | int fd, oflag; |
| 5308 | PyObject *path; |
| 5309 | unsigned long mode; |
| 5310 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5311 | ";A open file_action tuple must have 5 elements", |
| 5312 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5313 | &oflag, &mode)) |
| 5314 | { |
| 5315 | goto fail; |
| 5316 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5317 | if (PyList_Append(temp_buffer, path)) { |
| 5318 | Py_DECREF(path); |
| 5319 | goto fail; |
| 5320 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5321 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5322 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5323 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5324 | if (errno) { |
| 5325 | posix_error(); |
| 5326 | goto fail; |
| 5327 | } |
| 5328 | break; |
| 5329 | } |
| 5330 | case POSIX_SPAWN_CLOSE: { |
| 5331 | int fd; |
| 5332 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5333 | ";A close file_action tuple must have 2 elements", |
| 5334 | &tag_obj, &fd)) |
| 5335 | { |
| 5336 | goto fail; |
| 5337 | } |
| 5338 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 5339 | if (errno) { |
| 5340 | posix_error(); |
| 5341 | goto fail; |
| 5342 | } |
| 5343 | break; |
| 5344 | } |
| 5345 | case POSIX_SPAWN_DUP2: { |
| 5346 | int fd1, fd2; |
| 5347 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 5348 | ";A dup2 file_action tuple must have 3 elements", |
| 5349 | &tag_obj, &fd1, &fd2)) |
| 5350 | { |
| 5351 | goto fail; |
| 5352 | } |
| 5353 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 5354 | fd1, fd2); |
| 5355 | if (errno) { |
| 5356 | posix_error(); |
| 5357 | goto fail; |
| 5358 | } |
| 5359 | break; |
| 5360 | } |
| 5361 | default: { |
| 5362 | PyErr_SetString(PyExc_TypeError, |
| 5363 | "Unknown file_actions identifier"); |
| 5364 | goto fail; |
| 5365 | } |
| 5366 | } |
| 5367 | Py_DECREF(file_action); |
| 5368 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5369 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5370 | Py_DECREF(seq); |
| 5371 | return 0; |
| 5372 | |
| 5373 | fail: |
| 5374 | Py_DECREF(seq); |
| 5375 | Py_DECREF(file_action); |
| 5376 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 5377 | return -1; |
| 5378 | } |
| 5379 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5380 | /*[clinic input] |
| 5381 | |
| 5382 | os.posix_spawn |
| 5383 | path: path_t |
| 5384 | Path of executable file. |
| 5385 | argv: object |
| 5386 | Tuple or list of strings. |
| 5387 | env: object |
| 5388 | Dictionary of strings mapping to strings. |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5389 | / |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5390 | * |
Serhiy Storchaka | d700f97 | 2018-09-08 14:48:18 +0300 | [diff] [blame] | 5391 | file_actions: object(c_default='NULL') = () |
| 5392 | A sequence of file action tuples. |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5393 | setpgroup: object = NULL |
| 5394 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5395 | resetids: bool(accept={int}) = False |
| 5396 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
| 5397 | setsigmask: object(c_default='NULL') = () |
| 5398 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5399 | setsigdef: object(c_default='NULL') = () |
| 5400 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5401 | scheduler: object = NULL |
| 5402 | A tuple with the scheduler policy (optional) and parameters. |
Serhiy Storchaka | d700f97 | 2018-09-08 14:48:18 +0300 | [diff] [blame] | 5403 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5404 | Execute the program specified by path in a new process. |
| 5405 | [clinic start generated code]*/ |
| 5406 | |
| 5407 | static PyObject * |
| 5408 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5409 | PyObject *env, PyObject *file_actions, |
| 5410 | PyObject *setpgroup, int resetids, PyObject *setsigmask, |
| 5411 | PyObject *setsigdef, PyObject *scheduler) |
Serhiy Storchaka | d700f97 | 2018-09-08 14:48:18 +0300 | [diff] [blame] | 5412 | /*[clinic end generated code: output=45dfa4c515d09f2c input=2891c2f1d457e39b]*/ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5413 | { |
| 5414 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5415 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5416 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5417 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5418 | posix_spawnattr_t attr; |
| 5419 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5420 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5421 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5422 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5423 | pid_t pid; |
| 5424 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5425 | |
| 5426 | /* posix_spawn has three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5427 | 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] | 5428 | like posix.environ. */ |
| 5429 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5430 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5431 | PyErr_SetString(PyExc_TypeError, |
| 5432 | "posix_spawn: argv must be a tuple or list"); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5433 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5434 | } |
| 5435 | argc = PySequence_Size(argv); |
| 5436 | if (argc < 1) { |
| 5437 | PyErr_SetString(PyExc_ValueError, "posix_spawn: argv must not be empty"); |
| 5438 | return NULL; |
| 5439 | } |
| 5440 | |
| 5441 | if (!PyMapping_Check(env)) { |
| 5442 | PyErr_SetString(PyExc_TypeError, |
| 5443 | "posix_spawn: environment must be a mapping object"); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5444 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5445 | } |
| 5446 | |
| 5447 | argvlist = parse_arglist(argv, &argc); |
| 5448 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5449 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5450 | } |
| 5451 | if (!argvlist[0][0]) { |
| 5452 | PyErr_SetString(PyExc_ValueError, |
| 5453 | "posix_spawn: argv first element cannot be empty"); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5454 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5455 | } |
| 5456 | |
| 5457 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5458 | if (envlist == NULL) { |
| 5459 | goto exit; |
| 5460 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5461 | |
Serhiy Storchaka | d700f97 | 2018-09-08 14:48:18 +0300 | [diff] [blame] | 5462 | if (file_actions != NULL) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5463 | /* There is a bug in old versions of glibc that makes some of the |
| 5464 | * helper functions for manipulating file actions not copy the provided |
| 5465 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 5466 | * copy the value of path for some old versions of glibc (<2.20). |
| 5467 | * The use of temp_buffer here is a workaround that keeps the |
| 5468 | * python objects that own the buffers alive until posix_spawn gets called. |
| 5469 | * Check https://bugs.python.org/issue33630 and |
| 5470 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 5471 | temp_buffer = PyList_New(0); |
| 5472 | if (!temp_buffer) { |
| 5473 | goto exit; |
| 5474 | } |
| 5475 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5476 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5477 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5478 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5479 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5480 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5481 | if (parse_posix_spawn_flags(setpgroup, resetids, setsigmask, |
| 5482 | setsigdef, scheduler, &attr)) { |
| 5483 | goto exit; |
| 5484 | } |
| 5485 | attrp = &attr; |
| 5486 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5487 | _Py_BEGIN_SUPPRESS_IPH |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5488 | err_code = posix_spawn(&pid, path->narrow, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5489 | file_actionsp, attrp, argvlist, envlist); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5490 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5491 | if (err_code) { |
| 5492 | errno = err_code; |
| 5493 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5494 | goto exit; |
| 5495 | } |
| 5496 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5497 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5498 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5499 | if (file_actionsp) { |
| 5500 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5501 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5502 | if (attrp) { |
| 5503 | (void)posix_spawnattr_destroy(attrp); |
| 5504 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5505 | if (envlist) { |
| 5506 | free_string_array(envlist, envc); |
| 5507 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5508 | if (argvlist) { |
| 5509 | free_string_array(argvlist, argc); |
| 5510 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5511 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5512 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5513 | } |
| 5514 | #endif /* HAVE_POSIX_SPAWN */ |
| 5515 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5516 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5517 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5518 | /*[clinic input] |
| 5519 | os.spawnv |
| 5520 | |
| 5521 | mode: int |
| 5522 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5523 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5524 | Path of executable file. |
| 5525 | argv: object |
| 5526 | Tuple or list of strings. |
| 5527 | / |
| 5528 | |
| 5529 | Execute the program specified by path in a new process. |
| 5530 | [clinic start generated code]*/ |
| 5531 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5532 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5533 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5534 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5535 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5536 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5537 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5538 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5539 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5540 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5541 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5542 | /* spawnv has three arguments: (mode, path, argv), where |
| 5543 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5544 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5545 | if (PyList_Check(argv)) { |
| 5546 | argc = PyList_Size(argv); |
| 5547 | getitem = PyList_GetItem; |
| 5548 | } |
| 5549 | else if (PyTuple_Check(argv)) { |
| 5550 | argc = PyTuple_Size(argv); |
| 5551 | getitem = PyTuple_GetItem; |
| 5552 | } |
| 5553 | else { |
| 5554 | PyErr_SetString(PyExc_TypeError, |
| 5555 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5556 | return NULL; |
| 5557 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5558 | if (argc == 0) { |
| 5559 | PyErr_SetString(PyExc_ValueError, |
| 5560 | "spawnv() arg 2 cannot be empty"); |
| 5561 | return NULL; |
| 5562 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5563 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5564 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5565 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5566 | return PyErr_NoMemory(); |
| 5567 | } |
| 5568 | for (i = 0; i < argc; i++) { |
| 5569 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5570 | &argvlist[i])) { |
| 5571 | free_string_array(argvlist, i); |
| 5572 | PyErr_SetString( |
| 5573 | PyExc_TypeError, |
| 5574 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5575 | return NULL; |
| 5576 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5577 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5578 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5579 | PyErr_SetString( |
| 5580 | PyExc_ValueError, |
| 5581 | "spawnv() arg 2 first element cannot be empty"); |
| 5582 | return NULL; |
| 5583 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5584 | } |
| 5585 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5586 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5587 | if (mode == _OLD_P_OVERLAY) |
| 5588 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5589 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5590 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5591 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5592 | #ifdef HAVE_WSPAWNV |
| 5593 | spawnval = _wspawnv(mode, path->wide, argvlist); |
| 5594 | #else |
| 5595 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 5596 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5597 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5598 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5599 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5600 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5601 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5602 | if (spawnval == -1) |
| 5603 | return posix_error(); |
| 5604 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5605 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5606 | } |
| 5607 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5608 | /*[clinic input] |
| 5609 | os.spawnve |
| 5610 | |
| 5611 | mode: int |
| 5612 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5613 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5614 | Path of executable file. |
| 5615 | argv: object |
| 5616 | Tuple or list of strings. |
| 5617 | env: object |
| 5618 | Dictionary of strings mapping to strings. |
| 5619 | / |
| 5620 | |
| 5621 | Execute the program specified by path in a new process. |
| 5622 | [clinic start generated code]*/ |
| 5623 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5624 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5625 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5626 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5627 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
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; |
| 5630 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5631 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5632 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5633 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5634 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5635 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5636 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5637 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5638 | argv is a list or tuple of strings and env is a dictionary |
| 5639 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5640 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5641 | if (PyList_Check(argv)) { |
| 5642 | argc = PyList_Size(argv); |
| 5643 | getitem = PyList_GetItem; |
| 5644 | } |
| 5645 | else if (PyTuple_Check(argv)) { |
| 5646 | argc = PyTuple_Size(argv); |
| 5647 | getitem = PyTuple_GetItem; |
| 5648 | } |
| 5649 | else { |
| 5650 | PyErr_SetString(PyExc_TypeError, |
| 5651 | "spawnve() arg 2 must be a tuple or list"); |
| 5652 | goto fail_0; |
| 5653 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5654 | if (argc == 0) { |
| 5655 | PyErr_SetString(PyExc_ValueError, |
| 5656 | "spawnve() arg 2 cannot be empty"); |
| 5657 | goto fail_0; |
| 5658 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5659 | if (!PyMapping_Check(env)) { |
| 5660 | PyErr_SetString(PyExc_TypeError, |
| 5661 | "spawnve() arg 3 must be a mapping object"); |
| 5662 | goto fail_0; |
| 5663 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5664 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5665 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5666 | if (argvlist == NULL) { |
| 5667 | PyErr_NoMemory(); |
| 5668 | goto fail_0; |
| 5669 | } |
| 5670 | for (i = 0; i < argc; i++) { |
| 5671 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5672 | &argvlist[i])) |
| 5673 | { |
| 5674 | lastarg = i; |
| 5675 | goto fail_1; |
| 5676 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5677 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5678 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5679 | PyErr_SetString( |
| 5680 | PyExc_ValueError, |
| 5681 | "spawnv() arg 2 first element cannot be empty"); |
| 5682 | goto fail_1; |
| 5683 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5684 | } |
| 5685 | lastarg = argc; |
| 5686 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5687 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5688 | envlist = parse_envlist(env, &envc); |
| 5689 | if (envlist == NULL) |
| 5690 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5691 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5692 | if (mode == _OLD_P_OVERLAY) |
| 5693 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5694 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5695 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5696 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5697 | #ifdef HAVE_WSPAWNV |
| 5698 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
| 5699 | #else |
| 5700 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 5701 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5702 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5703 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5704 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5705 | if (spawnval == -1) |
| 5706 | (void) posix_error(); |
| 5707 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5708 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5709 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5710 | while (--envc >= 0) |
| 5711 | PyMem_DEL(envlist[envc]); |
| 5712 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5713 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5714 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5715 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5716 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5717 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5718 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5719 | #endif /* HAVE_SPAWNV */ |
| 5720 | |
| 5721 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5722 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5723 | |
| 5724 | /* Helper function to validate arguments. |
| 5725 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 5726 | If obj is non-NULL it must be callable. */ |
| 5727 | static int |
| 5728 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 5729 | { |
| 5730 | if (obj && !PyCallable_Check(obj)) { |
| 5731 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
| 5732 | obj_name, Py_TYPE(obj)->tp_name); |
| 5733 | return -1; |
| 5734 | } |
| 5735 | return 0; |
| 5736 | } |
| 5737 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5738 | /*[clinic input] |
| 5739 | os.register_at_fork |
| 5740 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5741 | * |
| 5742 | before: object=NULL |
| 5743 | A callable to be called in the parent before the fork() syscall. |
| 5744 | after_in_child: object=NULL |
| 5745 | A callable to be called in the child after fork(). |
| 5746 | after_in_parent: object=NULL |
| 5747 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5748 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5749 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5750 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5751 | 'before' callbacks are called in reverse order. |
| 5752 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5753 | |
| 5754 | [clinic start generated code]*/ |
| 5755 | |
| 5756 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5757 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 5758 | PyObject *after_in_child, PyObject *after_in_parent) |
| 5759 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5760 | { |
| 5761 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5762 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5763 | if (!before && !after_in_child && !after_in_parent) { |
| 5764 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 5765 | return NULL; |
| 5766 | } |
| 5767 | if (check_null_or_callable(before, "before") || |
| 5768 | check_null_or_callable(after_in_child, "after_in_child") || |
| 5769 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5770 | return NULL; |
| 5771 | } |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 5772 | interp = _PyInterpreterState_Get(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5773 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5774 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5775 | return NULL; |
| 5776 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5777 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5778 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5779 | } |
| 5780 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 5781 | return NULL; |
| 5782 | } |
| 5783 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5784 | } |
| 5785 | #endif /* HAVE_FORK */ |
| 5786 | |
| 5787 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5788 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5789 | /*[clinic input] |
| 5790 | os.fork1 |
| 5791 | |
| 5792 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 5793 | |
| 5794 | Return 0 to child process and PID of child to parent process. |
| 5795 | [clinic start generated code]*/ |
| 5796 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5797 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5798 | os_fork1_impl(PyObject *module) |
| 5799 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5800 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5801 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5802 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 5803 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 5804 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 5805 | return NULL; |
| 5806 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5807 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5808 | pid = fork1(); |
| 5809 | if (pid == 0) { |
| 5810 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5811 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5812 | } else { |
| 5813 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5814 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5815 | } |
| 5816 | if (pid == -1) |
| 5817 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5818 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5819 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5820 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5821 | |
| 5822 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5823 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5824 | /*[clinic input] |
| 5825 | os.fork |
| 5826 | |
| 5827 | Fork a child process. |
| 5828 | |
| 5829 | Return 0 to child process and PID of child to parent process. |
| 5830 | [clinic start generated code]*/ |
| 5831 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5832 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5833 | os_fork_impl(PyObject *module) |
| 5834 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5835 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5836 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5837 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 5838 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 5839 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 5840 | return NULL; |
| 5841 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5842 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5843 | pid = fork(); |
| 5844 | if (pid == 0) { |
| 5845 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5846 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5847 | } else { |
| 5848 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5849 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5850 | } |
| 5851 | if (pid == -1) |
| 5852 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5853 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5854 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5855 | #endif /* HAVE_FORK */ |
| 5856 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5857 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5858 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5859 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5860 | /*[clinic input] |
| 5861 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5862 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5863 | policy: int |
| 5864 | |
| 5865 | Get the maximum scheduling priority for policy. |
| 5866 | [clinic start generated code]*/ |
| 5867 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5868 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5869 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 5870 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5871 | { |
| 5872 | int max; |
| 5873 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5874 | max = sched_get_priority_max(policy); |
| 5875 | if (max < 0) |
| 5876 | return posix_error(); |
| 5877 | return PyLong_FromLong(max); |
| 5878 | } |
| 5879 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5880 | |
| 5881 | /*[clinic input] |
| 5882 | os.sched_get_priority_min |
| 5883 | |
| 5884 | policy: int |
| 5885 | |
| 5886 | Get the minimum scheduling priority for policy. |
| 5887 | [clinic start generated code]*/ |
| 5888 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5889 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5890 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 5891 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5892 | { |
| 5893 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5894 | if (min < 0) |
| 5895 | return posix_error(); |
| 5896 | return PyLong_FromLong(min); |
| 5897 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5898 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5899 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5900 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5901 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5902 | /*[clinic input] |
| 5903 | os.sched_getscheduler |
| 5904 | pid: pid_t |
| 5905 | / |
| 5906 | |
| 5907 | Get the scheduling policy for the process identifiedy by pid. |
| 5908 | |
| 5909 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 5910 | [clinic start generated code]*/ |
| 5911 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5912 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5913 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
| 5914 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5915 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5916 | int policy; |
| 5917 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5918 | policy = sched_getscheduler(pid); |
| 5919 | if (policy < 0) |
| 5920 | return posix_error(); |
| 5921 | return PyLong_FromLong(policy); |
| 5922 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5923 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5924 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5925 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5926 | #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] | 5927 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 5928 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5929 | |
| 5930 | @classmethod |
| 5931 | os.sched_param.__new__ |
| 5932 | |
| 5933 | sched_priority: object |
| 5934 | A scheduling parameter. |
| 5935 | |
| 5936 | Current has only one field: sched_priority"); |
| 5937 | [clinic start generated code]*/ |
| 5938 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5939 | static PyObject * |
| 5940 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 5941 | /*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5942 | { |
| 5943 | PyObject *res; |
| 5944 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5945 | res = PyStructSequence_New(type); |
| 5946 | if (!res) |
| 5947 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5948 | Py_INCREF(sched_priority); |
| 5949 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5950 | return res; |
| 5951 | } |
| 5952 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5953 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5954 | PyDoc_VAR(os_sched_param__doc__); |
| 5955 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5956 | static PyStructSequence_Field sched_param_fields[] = { |
| 5957 | {"sched_priority", "the scheduling priority"}, |
| 5958 | {0} |
| 5959 | }; |
| 5960 | |
| 5961 | static PyStructSequence_Desc sched_param_desc = { |
| 5962 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5963 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5964 | sched_param_fields, |
| 5965 | 1 |
| 5966 | }; |
| 5967 | |
| 5968 | static int |
| 5969 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5970 | { |
| 5971 | long priority; |
| 5972 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 5973 | if (Py_TYPE(param) != SchedParamType) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5974 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5975 | return 0; |
| 5976 | } |
| 5977 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5978 | if (priority == -1 && PyErr_Occurred()) |
| 5979 | return 0; |
| 5980 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5981 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5982 | return 0; |
| 5983 | } |
| 5984 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5985 | return 1; |
| 5986 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5987 | #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] | 5988 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5989 | |
| 5990 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5991 | /*[clinic input] |
| 5992 | os.sched_setscheduler |
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 | pid: pid_t |
| 5995 | policy: int |
| 5996 | param: sched_param |
| 5997 | / |
| 5998 | |
| 5999 | Set the scheduling policy for the process identified by pid. |
| 6000 | |
| 6001 | If pid is 0, the calling process is changed. |
| 6002 | param is an instance of sched_param. |
| 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_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6007 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6008 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6009 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6010 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6011 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6012 | ** scheduling policy under Solaris/Illumos, and others. |
| 6013 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6014 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6015 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6016 | return posix_error(); |
| 6017 | Py_RETURN_NONE; |
| 6018 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6019 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6020 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6021 | |
| 6022 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6023 | /*[clinic input] |
| 6024 | os.sched_getparam |
| 6025 | pid: pid_t |
| 6026 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6027 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6028 | Returns scheduling parameters for the process identified by pid. |
| 6029 | |
| 6030 | If pid is 0, returns parameters for the calling process. |
| 6031 | Return value is an instance of sched_param. |
| 6032 | [clinic start generated code]*/ |
| 6033 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6034 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6035 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6036 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6037 | { |
| 6038 | struct sched_param param; |
| 6039 | PyObject *result; |
| 6040 | PyObject *priority; |
| 6041 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6042 | if (sched_getparam(pid, ¶m)) |
| 6043 | return posix_error(); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6044 | result = PyStructSequence_New(SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6045 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6046 | return NULL; |
| 6047 | priority = PyLong_FromLong(param.sched_priority); |
| 6048 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6049 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6050 | return NULL; |
| 6051 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6052 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6053 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6054 | } |
| 6055 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6056 | |
| 6057 | /*[clinic input] |
| 6058 | os.sched_setparam |
| 6059 | pid: pid_t |
| 6060 | param: sched_param |
| 6061 | / |
| 6062 | |
| 6063 | Set scheduling parameters for the process identified by pid. |
| 6064 | |
| 6065 | If pid is 0, sets parameters for the calling process. |
| 6066 | param should be an instance of sched_param. |
| 6067 | [clinic start generated code]*/ |
| 6068 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6069 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6070 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6071 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6072 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6073 | { |
| 6074 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6075 | return posix_error(); |
| 6076 | Py_RETURN_NONE; |
| 6077 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6078 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6079 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6080 | |
| 6081 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6082 | /*[clinic input] |
| 6083 | os.sched_rr_get_interval -> double |
| 6084 | pid: pid_t |
| 6085 | / |
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 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6088 | |
| 6089 | Value returned is a float. |
| 6090 | [clinic start generated code]*/ |
| 6091 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6092 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6093 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6094 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6095 | { |
| 6096 | struct timespec interval; |
| 6097 | if (sched_rr_get_interval(pid, &interval)) { |
| 6098 | posix_error(); |
| 6099 | return -1.0; |
| 6100 | } |
| 6101 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6102 | } |
| 6103 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6104 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6105 | |
| 6106 | /*[clinic input] |
| 6107 | os.sched_yield |
| 6108 | |
| 6109 | Voluntarily relinquish the CPU. |
| 6110 | [clinic start generated code]*/ |
| 6111 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6112 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6113 | os_sched_yield_impl(PyObject *module) |
| 6114 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6115 | { |
| 6116 | if (sched_yield()) |
| 6117 | return posix_error(); |
| 6118 | Py_RETURN_NONE; |
| 6119 | } |
| 6120 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6121 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6122 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6123 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6124 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6125 | /*[clinic input] |
| 6126 | os.sched_setaffinity |
| 6127 | pid: pid_t |
| 6128 | mask : object |
| 6129 | / |
| 6130 | |
| 6131 | Set the CPU affinity of the process identified by pid to mask. |
| 6132 | |
| 6133 | mask should be an iterable of integers identifying CPUs. |
| 6134 | [clinic start generated code]*/ |
| 6135 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6136 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6137 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6138 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6139 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6140 | int ncpus; |
| 6141 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6142 | cpu_set_t *cpu_set = NULL; |
| 6143 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6144 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6145 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6146 | if (iterator == NULL) |
| 6147 | return NULL; |
| 6148 | |
| 6149 | ncpus = NCPUS_START; |
| 6150 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6151 | cpu_set = CPU_ALLOC(ncpus); |
| 6152 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6153 | PyErr_NoMemory(); |
| 6154 | goto error; |
| 6155 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6156 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6157 | |
| 6158 | while ((item = PyIter_Next(iterator))) { |
| 6159 | long cpu; |
| 6160 | if (!PyLong_Check(item)) { |
| 6161 | PyErr_Format(PyExc_TypeError, |
| 6162 | "expected an iterator of ints, " |
| 6163 | "but iterator yielded %R", |
| 6164 | Py_TYPE(item)); |
| 6165 | Py_DECREF(item); |
| 6166 | goto error; |
| 6167 | } |
| 6168 | cpu = PyLong_AsLong(item); |
| 6169 | Py_DECREF(item); |
| 6170 | if (cpu < 0) { |
| 6171 | if (!PyErr_Occurred()) |
| 6172 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6173 | goto error; |
| 6174 | } |
| 6175 | if (cpu > INT_MAX - 1) { |
| 6176 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6177 | goto error; |
| 6178 | } |
| 6179 | if (cpu >= ncpus) { |
| 6180 | /* Grow CPU mask to fit the CPU number */ |
| 6181 | int newncpus = ncpus; |
| 6182 | cpu_set_t *newmask; |
| 6183 | size_t newsetsize; |
| 6184 | while (newncpus <= cpu) { |
| 6185 | if (newncpus > INT_MAX / 2) |
| 6186 | newncpus = cpu + 1; |
| 6187 | else |
| 6188 | newncpus = newncpus * 2; |
| 6189 | } |
| 6190 | newmask = CPU_ALLOC(newncpus); |
| 6191 | if (newmask == NULL) { |
| 6192 | PyErr_NoMemory(); |
| 6193 | goto error; |
| 6194 | } |
| 6195 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 6196 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6197 | memcpy(newmask, cpu_set, setsize); |
| 6198 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6199 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6200 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6201 | ncpus = newncpus; |
| 6202 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6203 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6204 | } |
| 6205 | Py_CLEAR(iterator); |
| 6206 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6207 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6208 | posix_error(); |
| 6209 | goto error; |
| 6210 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6211 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6212 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6213 | |
| 6214 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6215 | if (cpu_set) |
| 6216 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6217 | Py_XDECREF(iterator); |
| 6218 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6219 | } |
| 6220 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6221 | |
| 6222 | /*[clinic input] |
| 6223 | os.sched_getaffinity |
| 6224 | pid: pid_t |
| 6225 | / |
| 6226 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6227 | 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] | 6228 | |
| 6229 | The affinity is returned as a set of CPU identifiers. |
| 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_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6234 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
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 cpu, ncpus, count; |
| 6237 | size_t setsize; |
| 6238 | cpu_set_t *mask = NULL; |
| 6239 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6240 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6241 | ncpus = NCPUS_START; |
| 6242 | while (1) { |
| 6243 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6244 | mask = CPU_ALLOC(ncpus); |
| 6245 | if (mask == NULL) |
| 6246 | return PyErr_NoMemory(); |
| 6247 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6248 | break; |
| 6249 | CPU_FREE(mask); |
| 6250 | if (errno != EINVAL) |
| 6251 | return posix_error(); |
| 6252 | if (ncpus > INT_MAX / 2) { |
| 6253 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6254 | "a large enough CPU set"); |
| 6255 | return NULL; |
| 6256 | } |
| 6257 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6258 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6259 | |
| 6260 | res = PySet_New(NULL); |
| 6261 | if (res == NULL) |
| 6262 | goto error; |
| 6263 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6264 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6265 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6266 | --count; |
| 6267 | if (cpu_num == NULL) |
| 6268 | goto error; |
| 6269 | if (PySet_Add(res, cpu_num)) { |
| 6270 | Py_DECREF(cpu_num); |
| 6271 | goto error; |
| 6272 | } |
| 6273 | Py_DECREF(cpu_num); |
| 6274 | } |
| 6275 | } |
| 6276 | CPU_FREE(mask); |
| 6277 | return res; |
| 6278 | |
| 6279 | error: |
| 6280 | if (mask) |
| 6281 | CPU_FREE(mask); |
| 6282 | Py_XDECREF(res); |
| 6283 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6284 | } |
| 6285 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6286 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6287 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6288 | #endif /* HAVE_SCHED_H */ |
| 6289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6290 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6291 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6292 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6293 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6294 | #define DEV_PTY_FILE "/dev/ptc" |
| 6295 | #define HAVE_DEV_PTMX |
| 6296 | #else |
| 6297 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6298 | #endif |
| 6299 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6300 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6301 | #ifdef HAVE_PTY_H |
| 6302 | #include <pty.h> |
| 6303 | #else |
| 6304 | #ifdef HAVE_LIBUTIL_H |
| 6305 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6306 | #else |
| 6307 | #ifdef HAVE_UTIL_H |
| 6308 | #include <util.h> |
| 6309 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6310 | #endif /* HAVE_LIBUTIL_H */ |
| 6311 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6312 | #ifdef HAVE_STROPTS_H |
| 6313 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6314 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6315 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6316 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6317 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6318 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6319 | /*[clinic input] |
| 6320 | os.openpty |
| 6321 | |
| 6322 | Open a pseudo-terminal. |
| 6323 | |
| 6324 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6325 | for both the master and slave ends. |
| 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_openpty_impl(PyObject *module) |
| 6330 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6331 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6332 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6333 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6334 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6335 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6336 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6337 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame^] | 6338 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6339 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6340 | #endif |
| 6341 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6342 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6343 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6344 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6345 | goto posix_error; |
| 6346 | |
| 6347 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6348 | goto error; |
| 6349 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6350 | goto error; |
| 6351 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6352 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6353 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6354 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6355 | goto posix_error; |
| 6356 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6357 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6358 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6359 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6360 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6361 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6362 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6363 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6364 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6365 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6366 | goto posix_error; |
| 6367 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6368 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6369 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6370 | /* change permission of slave */ |
| 6371 | if (grantpt(master_fd) < 0) { |
| 6372 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6373 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6374 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6375 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6376 | /* unlock slave */ |
| 6377 | if (unlockpt(master_fd) < 0) { |
| 6378 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6379 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6380 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6381 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6382 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6383 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6384 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6385 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6386 | goto posix_error; |
| 6387 | |
| 6388 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6389 | if (slave_fd == -1) |
| 6390 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6391 | |
| 6392 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6393 | goto posix_error; |
| 6394 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6395 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6396 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6397 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6398 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6399 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6400 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6401 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6402 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6403 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6404 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6405 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6406 | posix_error: |
| 6407 | posix_error(); |
| 6408 | error: |
| 6409 | if (master_fd != -1) |
| 6410 | close(master_fd); |
| 6411 | if (slave_fd != -1) |
| 6412 | close(slave_fd); |
| 6413 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6414 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6415 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6416 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6417 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6418 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6419 | /*[clinic input] |
| 6420 | os.forkpty |
| 6421 | |
| 6422 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6423 | |
| 6424 | Returns a tuple of (pid, master_fd). |
| 6425 | Like fork(), return pid of 0 to the child process, |
| 6426 | and pid of child to the parent process. |
| 6427 | To both, return fd of newly opened pseudo-terminal. |
| 6428 | [clinic start generated code]*/ |
| 6429 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6430 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6431 | os_forkpty_impl(PyObject *module) |
| 6432 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6433 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6434 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6435 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6436 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6437 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6438 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6439 | return NULL; |
| 6440 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6441 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6442 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6443 | if (pid == 0) { |
| 6444 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6445 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6446 | } else { |
| 6447 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6448 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6449 | } |
| 6450 | if (pid == -1) |
| 6451 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6452 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6453 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6454 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6455 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6456 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6457 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6458 | /*[clinic input] |
| 6459 | os.getegid |
| 6460 | |
| 6461 | Return the current process's effective group id. |
| 6462 | [clinic start generated code]*/ |
| 6463 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6464 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6465 | os_getegid_impl(PyObject *module) |
| 6466 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6467 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6468 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6469 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6470 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6471 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6472 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6473 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6474 | /*[clinic input] |
| 6475 | os.geteuid |
| 6476 | |
| 6477 | Return the current process's effective user id. |
| 6478 | [clinic start generated code]*/ |
| 6479 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6480 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6481 | os_geteuid_impl(PyObject *module) |
| 6482 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6483 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6484 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6485 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6486 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6487 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6488 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6489 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6490 | /*[clinic input] |
| 6491 | os.getgid |
| 6492 | |
| 6493 | Return the current process's group id. |
| 6494 | [clinic start generated code]*/ |
| 6495 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6496 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6497 | os_getgid_impl(PyObject *module) |
| 6498 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6499 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6500 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6501 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6502 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6503 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6504 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6505 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6506 | /*[clinic input] |
| 6507 | os.getpid |
| 6508 | |
| 6509 | Return the current process id. |
| 6510 | [clinic start generated code]*/ |
| 6511 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6512 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6513 | os_getpid_impl(PyObject *module) |
| 6514 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6515 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6516 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6517 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6518 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6519 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6520 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6521 | |
| 6522 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6523 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6524 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6525 | Returns a list of groups to which a user belongs.\n\n\ |
| 6526 | user: username to lookup\n\ |
| 6527 | group: base group id of the user"); |
| 6528 | |
| 6529 | static PyObject * |
| 6530 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6531 | { |
| 6532 | #ifdef NGROUPS_MAX |
| 6533 | #define MAX_GROUPS NGROUPS_MAX |
| 6534 | #else |
| 6535 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6536 | #define MAX_GROUPS 64 |
| 6537 | #endif |
| 6538 | |
| 6539 | const char *user; |
| 6540 | int i, ngroups; |
| 6541 | PyObject *list; |
| 6542 | #ifdef __APPLE__ |
| 6543 | int *groups, basegid; |
| 6544 | #else |
| 6545 | gid_t *groups, basegid; |
| 6546 | #endif |
| 6547 | ngroups = MAX_GROUPS; |
| 6548 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6549 | #ifdef __APPLE__ |
| 6550 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6551 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6552 | #else |
| 6553 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6554 | _Py_Gid_Converter, &basegid)) |
| 6555 | return NULL; |
| 6556 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6557 | |
| 6558 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6559 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6560 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6561 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6562 | #endif |
| 6563 | if (groups == NULL) |
| 6564 | return PyErr_NoMemory(); |
| 6565 | |
| 6566 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6567 | PyMem_Del(groups); |
| 6568 | return posix_error(); |
| 6569 | } |
| 6570 | |
| 6571 | list = PyList_New(ngroups); |
| 6572 | if (list == NULL) { |
| 6573 | PyMem_Del(groups); |
| 6574 | return NULL; |
| 6575 | } |
| 6576 | |
| 6577 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6578 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6579 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6580 | #else |
| 6581 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6582 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6583 | if (o == NULL) { |
| 6584 | Py_DECREF(list); |
| 6585 | PyMem_Del(groups); |
| 6586 | return NULL; |
| 6587 | } |
| 6588 | PyList_SET_ITEM(list, i, o); |
| 6589 | } |
| 6590 | |
| 6591 | PyMem_Del(groups); |
| 6592 | |
| 6593 | return list; |
| 6594 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6595 | #endif /* HAVE_GETGROUPLIST */ |
| 6596 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6597 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6598 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6599 | /*[clinic input] |
| 6600 | os.getgroups |
| 6601 | |
| 6602 | Return list of supplemental group IDs for the process. |
| 6603 | [clinic start generated code]*/ |
| 6604 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6605 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6606 | os_getgroups_impl(PyObject *module) |
| 6607 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6608 | { |
| 6609 | PyObject *result = NULL; |
| 6610 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6611 | #ifdef NGROUPS_MAX |
| 6612 | #define MAX_GROUPS NGROUPS_MAX |
| 6613 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6614 | /* 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] | 6615 | #define MAX_GROUPS 64 |
| 6616 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6617 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6618 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6619 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6620 | * This is a helper variable to store the intermediate result when |
| 6621 | * that happens. |
| 6622 | * |
| 6623 | * To keep the code readable the OSX behaviour is unconditional, |
| 6624 | * according to the POSIX spec this should be safe on all unix-y |
| 6625 | * systems. |
| 6626 | */ |
| 6627 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6628 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6629 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6630 | #ifdef __APPLE__ |
| 6631 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6632 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6633 | * always first call getgroups with length 0 to get the actual number |
| 6634 | * of groups. |
| 6635 | */ |
| 6636 | n = getgroups(0, NULL); |
| 6637 | if (n < 0) { |
| 6638 | return posix_error(); |
| 6639 | } else if (n <= MAX_GROUPS) { |
| 6640 | /* groups will fit in existing array */ |
| 6641 | alt_grouplist = grouplist; |
| 6642 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6643 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6644 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 6645 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6646 | } |
| 6647 | } |
| 6648 | |
| 6649 | n = getgroups(n, alt_grouplist); |
| 6650 | if (n == -1) { |
| 6651 | if (alt_grouplist != grouplist) { |
| 6652 | PyMem_Free(alt_grouplist); |
| 6653 | } |
| 6654 | return posix_error(); |
| 6655 | } |
| 6656 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6657 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6658 | if (n < 0) { |
| 6659 | if (errno == EINVAL) { |
| 6660 | n = getgroups(0, NULL); |
| 6661 | if (n == -1) { |
| 6662 | return posix_error(); |
| 6663 | } |
| 6664 | if (n == 0) { |
| 6665 | /* Avoid malloc(0) */ |
| 6666 | alt_grouplist = grouplist; |
| 6667 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6668 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6669 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 6670 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6671 | } |
| 6672 | n = getgroups(n, alt_grouplist); |
| 6673 | if (n == -1) { |
| 6674 | PyMem_Free(alt_grouplist); |
| 6675 | return posix_error(); |
| 6676 | } |
| 6677 | } |
| 6678 | } else { |
| 6679 | return posix_error(); |
| 6680 | } |
| 6681 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6682 | #endif |
| 6683 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6684 | result = PyList_New(n); |
| 6685 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6686 | int i; |
| 6687 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6688 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6689 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6690 | Py_DECREF(result); |
| 6691 | result = NULL; |
| 6692 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6693 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6694 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6695 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6696 | } |
| 6697 | |
| 6698 | if (alt_grouplist != grouplist) { |
| 6699 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6700 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6701 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6702 | return result; |
| 6703 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6704 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6705 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6706 | #ifdef HAVE_INITGROUPS |
| 6707 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6708 | "initgroups(username, gid) -> None\n\n\ |
| 6709 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6710 | the groups of which the specified username is a member, plus the specified\n\ |
| 6711 | group id."); |
| 6712 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6713 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6714 | static PyObject * |
| 6715 | posix_initgroups(PyObject *self, PyObject *args) |
| 6716 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6717 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6718 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6719 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6720 | #ifdef __APPLE__ |
| 6721 | int gid; |
| 6722 | #else |
| 6723 | gid_t gid; |
| 6724 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6725 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6726 | #ifdef __APPLE__ |
| 6727 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6728 | PyUnicode_FSConverter, &oname, |
| 6729 | &gid)) |
| 6730 | #else |
| 6731 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6732 | PyUnicode_FSConverter, &oname, |
| 6733 | _Py_Gid_Converter, &gid)) |
| 6734 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6735 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6736 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6737 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6738 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6739 | Py_DECREF(oname); |
| 6740 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6741 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6742 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 6743 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6744 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6745 | #endif /* HAVE_INITGROUPS */ |
| 6746 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6747 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6748 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6749 | /*[clinic input] |
| 6750 | os.getpgid |
| 6751 | |
| 6752 | pid: pid_t |
| 6753 | |
| 6754 | Call the system call getpgid(), and return the result. |
| 6755 | [clinic start generated code]*/ |
| 6756 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6757 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6758 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 6759 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6760 | { |
| 6761 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6762 | if (pgid < 0) |
| 6763 | return posix_error(); |
| 6764 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6765 | } |
| 6766 | #endif /* HAVE_GETPGID */ |
| 6767 | |
| 6768 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6769 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6770 | /*[clinic input] |
| 6771 | os.getpgrp |
| 6772 | |
| 6773 | Return the current process group id. |
| 6774 | [clinic start generated code]*/ |
| 6775 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6776 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6777 | os_getpgrp_impl(PyObject *module) |
| 6778 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6779 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6780 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6781 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6782 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6783 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6784 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6785 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6786 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6787 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6788 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6789 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6790 | /*[clinic input] |
| 6791 | os.setpgrp |
| 6792 | |
| 6793 | Make the current process the leader of its process group. |
| 6794 | [clinic start generated code]*/ |
| 6795 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6796 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6797 | os_setpgrp_impl(PyObject *module) |
| 6798 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6799 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6800 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6801 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6802 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6803 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6804 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6805 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 6806 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6807 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6808 | #endif /* HAVE_SETPGRP */ |
| 6809 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6810 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6811 | |
| 6812 | #ifdef MS_WINDOWS |
| 6813 | #include <tlhelp32.h> |
| 6814 | |
| 6815 | static PyObject* |
| 6816 | win32_getppid() |
| 6817 | { |
| 6818 | HANDLE snapshot; |
| 6819 | pid_t mypid; |
| 6820 | PyObject* result = NULL; |
| 6821 | BOOL have_record; |
| 6822 | PROCESSENTRY32 pe; |
| 6823 | |
| 6824 | mypid = getpid(); /* This function never fails */ |
| 6825 | |
| 6826 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6827 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6828 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6829 | |
| 6830 | pe.dwSize = sizeof(pe); |
| 6831 | have_record = Process32First(snapshot, &pe); |
| 6832 | while (have_record) { |
| 6833 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6834 | /* We could cache the ulong value in a static variable. */ |
| 6835 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6836 | break; |
| 6837 | } |
| 6838 | |
| 6839 | have_record = Process32Next(snapshot, &pe); |
| 6840 | } |
| 6841 | |
| 6842 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6843 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6844 | * error anyway, so let's raise it. */ |
| 6845 | if (!result) |
| 6846 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6847 | |
| 6848 | CloseHandle(snapshot); |
| 6849 | |
| 6850 | return result; |
| 6851 | } |
| 6852 | #endif /*MS_WINDOWS*/ |
| 6853 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6854 | |
| 6855 | /*[clinic input] |
| 6856 | os.getppid |
| 6857 | |
| 6858 | Return the parent's process id. |
| 6859 | |
| 6860 | If the parent process has already exited, Windows machines will still |
| 6861 | return its id; others systems will return the id of the 'init' process (1). |
| 6862 | [clinic start generated code]*/ |
| 6863 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6864 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6865 | os_getppid_impl(PyObject *module) |
| 6866 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6867 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6868 | #ifdef MS_WINDOWS |
| 6869 | return win32_getppid(); |
| 6870 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6871 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6872 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6873 | } |
| 6874 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6875 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6876 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6877 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6878 | /*[clinic input] |
| 6879 | os.getlogin |
| 6880 | |
| 6881 | Return the actual login name. |
| 6882 | [clinic start generated code]*/ |
| 6883 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6884 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6885 | os_getlogin_impl(PyObject *module) |
| 6886 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6887 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6888 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6889 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6890 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6891 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6892 | |
| 6893 | if (GetUserNameW(user_name, &num_chars)) { |
| 6894 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6895 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6896 | } |
| 6897 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6898 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6899 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6900 | char *name; |
| 6901 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6902 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6903 | errno = 0; |
| 6904 | name = getlogin(); |
| 6905 | if (name == NULL) { |
| 6906 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6907 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6908 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6909 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6910 | } |
| 6911 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6912 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6913 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6914 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6915 | return result; |
| 6916 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6917 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6918 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6919 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6920 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6921 | /*[clinic input] |
| 6922 | os.getuid |
| 6923 | |
| 6924 | Return the current process's user id. |
| 6925 | [clinic start generated code]*/ |
| 6926 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6927 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6928 | os_getuid_impl(PyObject *module) |
| 6929 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6930 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6931 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6932 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6933 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6934 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6935 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6936 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6937 | #define HAVE_KILL |
| 6938 | #endif /* MS_WINDOWS */ |
| 6939 | |
| 6940 | #ifdef HAVE_KILL |
| 6941 | /*[clinic input] |
| 6942 | os.kill |
| 6943 | |
| 6944 | pid: pid_t |
| 6945 | signal: Py_ssize_t |
| 6946 | / |
| 6947 | |
| 6948 | Kill a process with a signal. |
| 6949 | [clinic start generated code]*/ |
| 6950 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6951 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6952 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 6953 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6954 | #ifndef MS_WINDOWS |
| 6955 | { |
| 6956 | if (kill(pid, (int)signal) == -1) |
| 6957 | return posix_error(); |
| 6958 | Py_RETURN_NONE; |
| 6959 | } |
| 6960 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6961 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6962 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6963 | DWORD sig = (DWORD)signal; |
| 6964 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6965 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6966 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6967 | /* Console processes which share a common console can be sent CTRL+C or |
| 6968 | CTRL+BREAK events, provided they handle said events. */ |
| 6969 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6970 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6971 | err = GetLastError(); |
| 6972 | PyErr_SetFromWindowsErr(err); |
| 6973 | } |
| 6974 | else |
| 6975 | Py_RETURN_NONE; |
| 6976 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6977 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6978 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6979 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6980 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6981 | if (handle == NULL) { |
| 6982 | err = GetLastError(); |
| 6983 | return PyErr_SetFromWindowsErr(err); |
| 6984 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6985 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6986 | if (TerminateProcess(handle, sig) == 0) { |
| 6987 | err = GetLastError(); |
| 6988 | result = PyErr_SetFromWindowsErr(err); |
| 6989 | } else { |
| 6990 | Py_INCREF(Py_None); |
| 6991 | result = Py_None; |
| 6992 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6993 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6994 | CloseHandle(handle); |
| 6995 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6996 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6997 | #endif /* !MS_WINDOWS */ |
| 6998 | #endif /* HAVE_KILL */ |
| 6999 | |
| 7000 | |
| 7001 | #ifdef HAVE_KILLPG |
| 7002 | /*[clinic input] |
| 7003 | os.killpg |
| 7004 | |
| 7005 | pgid: pid_t |
| 7006 | signal: int |
| 7007 | / |
| 7008 | |
| 7009 | Kill a process group with a signal. |
| 7010 | [clinic start generated code]*/ |
| 7011 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7012 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7013 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7014 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7015 | { |
| 7016 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7017 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7018 | take the same type. Moreover, pid_t is always at least as wide as |
| 7019 | int (else compilation of this module fails), which is safe. */ |
| 7020 | if (killpg(pgid, signal) == -1) |
| 7021 | return posix_error(); |
| 7022 | Py_RETURN_NONE; |
| 7023 | } |
| 7024 | #endif /* HAVE_KILLPG */ |
| 7025 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7026 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7027 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7028 | #ifdef HAVE_SYS_LOCK_H |
| 7029 | #include <sys/lock.h> |
| 7030 | #endif |
| 7031 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7032 | /*[clinic input] |
| 7033 | os.plock |
| 7034 | op: int |
| 7035 | / |
| 7036 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7037 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7038 | [clinic start generated code]*/ |
| 7039 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7040 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7041 | os_plock_impl(PyObject *module, int op) |
| 7042 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7043 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7044 | if (plock(op) == -1) |
| 7045 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7046 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7047 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7048 | #endif /* HAVE_PLOCK */ |
| 7049 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7050 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7051 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7052 | /*[clinic input] |
| 7053 | os.setuid |
| 7054 | |
| 7055 | uid: uid_t |
| 7056 | / |
| 7057 | |
| 7058 | Set the current process's user id. |
| 7059 | [clinic start generated code]*/ |
| 7060 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7061 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7062 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7063 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7064 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7065 | if (setuid(uid) < 0) |
| 7066 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7067 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7068 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7069 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7070 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7071 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7072 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7073 | /*[clinic input] |
| 7074 | os.seteuid |
| 7075 | |
| 7076 | euid: uid_t |
| 7077 | / |
| 7078 | |
| 7079 | Set the current process's effective user id. |
| 7080 | [clinic start generated code]*/ |
| 7081 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7082 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7083 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7084 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7085 | { |
| 7086 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7087 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7088 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7089 | } |
| 7090 | #endif /* HAVE_SETEUID */ |
| 7091 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7092 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7093 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7094 | /*[clinic input] |
| 7095 | os.setegid |
| 7096 | |
| 7097 | egid: gid_t |
| 7098 | / |
| 7099 | |
| 7100 | Set the current process's effective group id. |
| 7101 | [clinic start generated code]*/ |
| 7102 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7103 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7104 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7105 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7106 | { |
| 7107 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7108 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7109 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7110 | } |
| 7111 | #endif /* HAVE_SETEGID */ |
| 7112 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7113 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7114 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7115 | /*[clinic input] |
| 7116 | os.setreuid |
| 7117 | |
| 7118 | ruid: uid_t |
| 7119 | euid: uid_t |
| 7120 | / |
| 7121 | |
| 7122 | Set the current process's real and effective user ids. |
| 7123 | [clinic start generated code]*/ |
| 7124 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7125 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7126 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 7127 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7128 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7129 | if (setreuid(ruid, euid) < 0) { |
| 7130 | return posix_error(); |
| 7131 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7132 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7133 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7134 | } |
| 7135 | #endif /* HAVE_SETREUID */ |
| 7136 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7137 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7138 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7139 | /*[clinic input] |
| 7140 | os.setregid |
| 7141 | |
| 7142 | rgid: gid_t |
| 7143 | egid: gid_t |
| 7144 | / |
| 7145 | |
| 7146 | Set the current process's real and effective group ids. |
| 7147 | [clinic start generated code]*/ |
| 7148 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7149 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7150 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 7151 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7152 | { |
| 7153 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7154 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7155 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7156 | } |
| 7157 | #endif /* HAVE_SETREGID */ |
| 7158 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7159 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7160 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7161 | /*[clinic input] |
| 7162 | os.setgid |
| 7163 | gid: gid_t |
| 7164 | / |
| 7165 | |
| 7166 | Set the current process's group id. |
| 7167 | [clinic start generated code]*/ |
| 7168 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7169 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7170 | os_setgid_impl(PyObject *module, gid_t gid) |
| 7171 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7172 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7173 | if (setgid(gid) < 0) |
| 7174 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7175 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7176 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7177 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7178 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7179 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7180 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7181 | /*[clinic input] |
| 7182 | os.setgroups |
| 7183 | |
| 7184 | groups: object |
| 7185 | / |
| 7186 | |
| 7187 | Set the groups of the current process to list. |
| 7188 | [clinic start generated code]*/ |
| 7189 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7190 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7191 | os_setgroups(PyObject *module, PyObject *groups) |
| 7192 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7193 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7194 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7195 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7196 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7197 | if (!PySequence_Check(groups)) { |
| 7198 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 7199 | return NULL; |
| 7200 | } |
| 7201 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7202 | if (len < 0) { |
| 7203 | return NULL; |
| 7204 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7205 | if (len > MAX_GROUPS) { |
| 7206 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7207 | return NULL; |
| 7208 | } |
| 7209 | for(i = 0; i < len; i++) { |
| 7210 | PyObject *elem; |
| 7211 | elem = PySequence_GetItem(groups, i); |
| 7212 | if (!elem) |
| 7213 | return NULL; |
| 7214 | if (!PyLong_Check(elem)) { |
| 7215 | PyErr_SetString(PyExc_TypeError, |
| 7216 | "groups must be integers"); |
| 7217 | Py_DECREF(elem); |
| 7218 | return NULL; |
| 7219 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7220 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7221 | Py_DECREF(elem); |
| 7222 | return NULL; |
| 7223 | } |
| 7224 | } |
| 7225 | Py_DECREF(elem); |
| 7226 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7227 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7228 | if (setgroups(len, grouplist) < 0) |
| 7229 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7230 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7231 | } |
| 7232 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7233 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7234 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7235 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7236 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7237 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7238 | PyObject *result; |
| 7239 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 7240 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7241 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7242 | if (pid == -1) |
| 7243 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7244 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7245 | if (struct_rusage == NULL) { |
| 7246 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7247 | if (m == NULL) |
| 7248 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 7249 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7250 | Py_DECREF(m); |
| 7251 | if (struct_rusage == NULL) |
| 7252 | return NULL; |
| 7253 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7254 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7255 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7256 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 7257 | if (!result) |
| 7258 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7259 | |
| 7260 | #ifndef doubletime |
| 7261 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7262 | #endif |
| 7263 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7264 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7265 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7266 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7267 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7268 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7269 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7270 | SET_INT(result, 2, ru->ru_maxrss); |
| 7271 | SET_INT(result, 3, ru->ru_ixrss); |
| 7272 | SET_INT(result, 4, ru->ru_idrss); |
| 7273 | SET_INT(result, 5, ru->ru_isrss); |
| 7274 | SET_INT(result, 6, ru->ru_minflt); |
| 7275 | SET_INT(result, 7, ru->ru_majflt); |
| 7276 | SET_INT(result, 8, ru->ru_nswap); |
| 7277 | SET_INT(result, 9, ru->ru_inblock); |
| 7278 | SET_INT(result, 10, ru->ru_oublock); |
| 7279 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7280 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7281 | SET_INT(result, 13, ru->ru_nsignals); |
| 7282 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7283 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7284 | #undef SET_INT |
| 7285 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7286 | if (PyErr_Occurred()) { |
| 7287 | Py_DECREF(result); |
| 7288 | return NULL; |
| 7289 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7290 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7291 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7292 | } |
| 7293 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7294 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7295 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7296 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7297 | /*[clinic input] |
| 7298 | os.wait3 |
| 7299 | |
| 7300 | options: int |
| 7301 | Wait for completion of a child process. |
| 7302 | |
| 7303 | Returns a tuple of information about the child process: |
| 7304 | (pid, status, rusage) |
| 7305 | [clinic start generated code]*/ |
| 7306 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7307 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7308 | os_wait3_impl(PyObject *module, int options) |
| 7309 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7310 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7311 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7312 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7313 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7314 | WAIT_TYPE status; |
| 7315 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7316 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7317 | do { |
| 7318 | Py_BEGIN_ALLOW_THREADS |
| 7319 | pid = wait3(&status, options, &ru); |
| 7320 | Py_END_ALLOW_THREADS |
| 7321 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7322 | if (pid < 0) |
| 7323 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7324 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7325 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7326 | } |
| 7327 | #endif /* HAVE_WAIT3 */ |
| 7328 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7329 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7330 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7331 | /*[clinic input] |
| 7332 | |
| 7333 | os.wait4 |
| 7334 | |
| 7335 | pid: pid_t |
| 7336 | options: int |
| 7337 | |
| 7338 | Wait for completion of a specific child process. |
| 7339 | |
| 7340 | Returns a tuple of information about the child process: |
| 7341 | (pid, status, rusage) |
| 7342 | [clinic start generated code]*/ |
| 7343 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7344 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7345 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7346 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7347 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7348 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7349 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7350 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7351 | WAIT_TYPE status; |
| 7352 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7353 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7354 | do { |
| 7355 | Py_BEGIN_ALLOW_THREADS |
| 7356 | res = wait4(pid, &status, options, &ru); |
| 7357 | Py_END_ALLOW_THREADS |
| 7358 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7359 | if (res < 0) |
| 7360 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7361 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7362 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7363 | } |
| 7364 | #endif /* HAVE_WAIT4 */ |
| 7365 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7366 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7367 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7368 | /*[clinic input] |
| 7369 | os.waitid |
| 7370 | |
| 7371 | idtype: idtype_t |
| 7372 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7373 | id: id_t |
| 7374 | The id to wait on. |
| 7375 | options: int |
| 7376 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7377 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7378 | / |
| 7379 | |
| 7380 | Returns the result of waiting for a process or processes. |
| 7381 | |
| 7382 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7383 | no children in a waitable state. |
| 7384 | [clinic start generated code]*/ |
| 7385 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7386 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7387 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7388 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7389 | { |
| 7390 | PyObject *result; |
| 7391 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7392 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7393 | siginfo_t si; |
| 7394 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7395 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7396 | do { |
| 7397 | Py_BEGIN_ALLOW_THREADS |
| 7398 | res = waitid(idtype, id, &si, options); |
| 7399 | Py_END_ALLOW_THREADS |
| 7400 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7401 | if (res < 0) |
| 7402 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7403 | |
| 7404 | if (si.si_pid == 0) |
| 7405 | Py_RETURN_NONE; |
| 7406 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 7407 | result = PyStructSequence_New(WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7408 | if (!result) |
| 7409 | return NULL; |
| 7410 | |
| 7411 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7412 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7413 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7414 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7415 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7416 | if (PyErr_Occurred()) { |
| 7417 | Py_DECREF(result); |
| 7418 | return NULL; |
| 7419 | } |
| 7420 | |
| 7421 | return result; |
| 7422 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7423 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7424 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7425 | |
| 7426 | #if defined(HAVE_WAITPID) |
| 7427 | /*[clinic input] |
| 7428 | os.waitpid |
| 7429 | pid: pid_t |
| 7430 | options: int |
| 7431 | / |
| 7432 | |
| 7433 | Wait for completion of a given child process. |
| 7434 | |
| 7435 | Returns a tuple of information regarding the child process: |
| 7436 | (pid, status) |
| 7437 | |
| 7438 | The options argument is ignored on Windows. |
| 7439 | [clinic start generated code]*/ |
| 7440 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7441 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7442 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7443 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7444 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7445 | pid_t res; |
| 7446 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7447 | WAIT_TYPE status; |
| 7448 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7449 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7450 | do { |
| 7451 | Py_BEGIN_ALLOW_THREADS |
| 7452 | res = waitpid(pid, &status, options); |
| 7453 | Py_END_ALLOW_THREADS |
| 7454 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7455 | if (res < 0) |
| 7456 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7457 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7458 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7459 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7460 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7461 | /* 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] | 7462 | /*[clinic input] |
| 7463 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7464 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7465 | options: int |
| 7466 | / |
| 7467 | |
| 7468 | Wait for completion of a given process. |
| 7469 | |
| 7470 | Returns a tuple of information regarding the process: |
| 7471 | (pid, status << 8) |
| 7472 | |
| 7473 | The options argument is ignored on Windows. |
| 7474 | [clinic start generated code]*/ |
| 7475 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7476 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7477 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7478 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7479 | { |
| 7480 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7481 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7482 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7483 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7484 | do { |
| 7485 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7486 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7487 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7488 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7489 | Py_END_ALLOW_THREADS |
| 7490 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7491 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7492 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7493 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7494 | /* 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] | 7495 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7496 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7497 | #endif |
| 7498 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7499 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7500 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7501 | /*[clinic input] |
| 7502 | os.wait |
| 7503 | |
| 7504 | Wait for completion of a child process. |
| 7505 | |
| 7506 | Returns a tuple of information about the child process: |
| 7507 | (pid, status) |
| 7508 | [clinic start generated code]*/ |
| 7509 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7510 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7511 | os_wait_impl(PyObject *module) |
| 7512 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7513 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7514 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7515 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7516 | WAIT_TYPE status; |
| 7517 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7518 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7519 | do { |
| 7520 | Py_BEGIN_ALLOW_THREADS |
| 7521 | pid = wait(&status); |
| 7522 | Py_END_ALLOW_THREADS |
| 7523 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7524 | if (pid < 0) |
| 7525 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7526 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7527 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7528 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7529 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7530 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7531 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7532 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7533 | /*[clinic input] |
| 7534 | os.readlink |
| 7535 | |
| 7536 | path: path_t |
| 7537 | * |
| 7538 | dir_fd: dir_fd(requires='readlinkat') = None |
| 7539 | |
| 7540 | Return a string representing the path to which the symbolic link points. |
| 7541 | |
| 7542 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7543 | and path should be relative; path will then be relative to that directory. |
| 7544 | |
| 7545 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 7546 | using it will raise a NotImplementedError. |
| 7547 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7548 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7549 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7550 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 7551 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7552 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7553 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 7554 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7555 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7556 | |
| 7557 | Py_BEGIN_ALLOW_THREADS |
| 7558 | #ifdef HAVE_READLINKAT |
| 7559 | if (dir_fd != DEFAULT_DIR_FD) |
| 7560 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 7561 | else |
| 7562 | #endif |
| 7563 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 7564 | Py_END_ALLOW_THREADS |
| 7565 | |
| 7566 | if (length < 0) { |
| 7567 | return path_error(path); |
| 7568 | } |
| 7569 | buffer[length] = '\0'; |
| 7570 | |
| 7571 | if (PyUnicode_Check(path->object)) |
| 7572 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7573 | else |
| 7574 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7575 | #elif defined(MS_WINDOWS) |
| 7576 | DWORD n_bytes_returned; |
| 7577 | DWORD io_result; |
| 7578 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7579 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7580 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
| 7581 | const wchar_t *print_name; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7582 | PyObject *result; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7583 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7584 | /* First get a handle to the reparse point */ |
| 7585 | Py_BEGIN_ALLOW_THREADS |
| 7586 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7587 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7588 | 0, |
| 7589 | 0, |
| 7590 | 0, |
| 7591 | OPEN_EXISTING, |
| 7592 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7593 | 0); |
| 7594 | Py_END_ALLOW_THREADS |
| 7595 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7596 | if (reparse_point_handle == INVALID_HANDLE_VALUE) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7597 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7598 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7599 | |
| 7600 | Py_BEGIN_ALLOW_THREADS |
| 7601 | /* New call DeviceIoControl to read the reparse point */ |
| 7602 | io_result = DeviceIoControl( |
| 7603 | reparse_point_handle, |
| 7604 | FSCTL_GET_REPARSE_POINT, |
| 7605 | 0, 0, /* in buffer */ |
| 7606 | target_buffer, sizeof(target_buffer), |
| 7607 | &n_bytes_returned, |
| 7608 | 0 /* we're not using OVERLAPPED_IO */ |
| 7609 | ); |
| 7610 | CloseHandle(reparse_point_handle); |
| 7611 | Py_END_ALLOW_THREADS |
| 7612 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7613 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7614 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7615 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7616 | |
| 7617 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7618 | { |
| 7619 | PyErr_SetString(PyExc_ValueError, |
| 7620 | "not a symbolic link"); |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7621 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7622 | } |
SSE4 | 3c34aad | 2018-02-13 00:10:35 +0700 | [diff] [blame] | 7623 | print_name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7624 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7625 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7626 | result = PyUnicode_FromWideChar(print_name, |
| 7627 | rdb->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t)); |
| 7628 | if (path->narrow) { |
| 7629 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7630 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7631 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7632 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7633 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7634 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7635 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7636 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7637 | |
| 7638 | #if defined(MS_WINDOWS) |
| 7639 | |
| 7640 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7641 | static BOOLEAN (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7642 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7643 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7644 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7645 | { |
| 7646 | HINSTANCE hKernel32; |
| 7647 | /* only recheck */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7648 | if (Py_CreateSymbolicLinkW) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7649 | return 1; |
| 7650 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7651 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7652 | "CreateSymbolicLinkW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7653 | return Py_CreateSymbolicLinkW != NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7654 | } |
| 7655 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7656 | /* Remove the last portion of the path - return 0 on success */ |
| 7657 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7658 | _dirnameW(WCHAR *path) |
| 7659 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7660 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7661 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 7662 | if (length == MAX_PATH) { |
| 7663 | return -1; |
| 7664 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7665 | |
| 7666 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7667 | for(ptr = path + length; ptr != path; ptr--) { |
| 7668 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7669 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7670 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7671 | } |
| 7672 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7673 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7674 | } |
| 7675 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7676 | /* Is this path absolute? */ |
| 7677 | static int |
| 7678 | _is_absW(const WCHAR *path) |
| 7679 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7680 | return path[0] == L'\\' || path[0] == L'/' || |
| 7681 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7682 | } |
| 7683 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7684 | /* join root and rest with a backslash - return 0 on success */ |
| 7685 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7686 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7687 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7688 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7689 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7690 | } |
| 7691 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7692 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 7693 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7694 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7695 | |
| 7696 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 7697 | return -1; |
| 7698 | } |
| 7699 | |
| 7700 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7701 | } |
| 7702 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7703 | /* Return True if the path at src relative to dest is a directory */ |
| 7704 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7705 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7706 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7707 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7708 | WCHAR dest_parent[MAX_PATH]; |
| 7709 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7710 | |
| 7711 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7712 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 7713 | _dirnameW(dest_parent)) { |
| 7714 | return 0; |
| 7715 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7716 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7717 | if (_joinW(src_resolved, dest_parent, src)) { |
| 7718 | return 0; |
| 7719 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7720 | return ( |
| 7721 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7722 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7723 | ); |
| 7724 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7725 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7726 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7727 | |
| 7728 | /*[clinic input] |
| 7729 | os.symlink |
| 7730 | src: path_t |
| 7731 | dst: path_t |
| 7732 | target_is_directory: bool = False |
| 7733 | * |
| 7734 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7735 | |
| 7736 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7737 | |
| 7738 | Create a symbolic link pointing to src named dst. |
| 7739 | |
| 7740 | target_is_directory is required on Windows if the target is to be |
| 7741 | interpreted as a directory. (On Windows, symlink requires |
| 7742 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7743 | target_is_directory is ignored on non-Windows platforms. |
| 7744 | |
| 7745 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7746 | and path should be relative; path will then be relative to that directory. |
| 7747 | dir_fd may not be implemented on your platform. |
| 7748 | If it is unavailable, using it will raise a NotImplementedError. |
| 7749 | |
| 7750 | [clinic start generated code]*/ |
| 7751 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7752 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7753 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7754 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7755 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7756 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7757 | #ifdef MS_WINDOWS |
| 7758 | DWORD result; |
| 7759 | #else |
| 7760 | int result; |
| 7761 | #endif |
| 7762 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7763 | #ifdef MS_WINDOWS |
| 7764 | if (!check_CreateSymbolicLink()) { |
| 7765 | PyErr_SetString(PyExc_NotImplementedError, |
| 7766 | "CreateSymbolicLink functions not found"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7767 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7768 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7769 | if (!win32_can_symlink) { |
| 7770 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7771 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7772 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7773 | #endif |
| 7774 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7775 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7776 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7777 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7778 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7779 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7780 | target_is_directory |= _check_dirW(src->wide, dst->wide); |
| 7781 | result = Py_CreateSymbolicLinkW(dst->wide, src->wide, |
| 7782 | target_is_directory); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7783 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7784 | Py_END_ALLOW_THREADS |
| 7785 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7786 | if (!result) |
| 7787 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7788 | |
| 7789 | #else |
| 7790 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7791 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 7792 | PyErr_SetString(PyExc_ValueError, |
| 7793 | "symlink: src and dst must be the same type"); |
| 7794 | return NULL; |
| 7795 | } |
| 7796 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7797 | Py_BEGIN_ALLOW_THREADS |
| 7798 | #if HAVE_SYMLINKAT |
| 7799 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7800 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7801 | else |
| 7802 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7803 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7804 | Py_END_ALLOW_THREADS |
| 7805 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7806 | if (result) |
| 7807 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7808 | #endif |
| 7809 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7810 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7811 | } |
| 7812 | #endif /* HAVE_SYMLINK */ |
| 7813 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7814 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7815 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7816 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7817 | static PyStructSequence_Field times_result_fields[] = { |
| 7818 | {"user", "user time"}, |
| 7819 | {"system", "system time"}, |
| 7820 | {"children_user", "user time of children"}, |
| 7821 | {"children_system", "system time of children"}, |
| 7822 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7823 | {NULL} |
| 7824 | }; |
| 7825 | |
| 7826 | PyDoc_STRVAR(times_result__doc__, |
| 7827 | "times_result: Result from os.times().\n\n\ |
| 7828 | This object may be accessed either as a tuple of\n\ |
| 7829 | (user, system, children_user, children_system, elapsed),\n\ |
| 7830 | or via the attributes user, system, children_user, children_system,\n\ |
| 7831 | and elapsed.\n\ |
| 7832 | \n\ |
| 7833 | See os.times for more information."); |
| 7834 | |
| 7835 | static PyStructSequence_Desc times_result_desc = { |
| 7836 | "times_result", /* name */ |
| 7837 | times_result__doc__, /* doc */ |
| 7838 | times_result_fields, |
| 7839 | 5 |
| 7840 | }; |
| 7841 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 7842 | static PyTypeObject* TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7843 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7844 | #ifdef MS_WINDOWS |
| 7845 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7846 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7847 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7848 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7849 | |
| 7850 | static PyObject * |
| 7851 | build_times_result(double user, double system, |
| 7852 | double children_user, double children_system, |
| 7853 | double elapsed) |
| 7854 | { |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 7855 | PyObject *value = PyStructSequence_New(TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7856 | if (value == NULL) |
| 7857 | return NULL; |
| 7858 | |
| 7859 | #define SET(i, field) \ |
| 7860 | { \ |
| 7861 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7862 | if (!o) { \ |
| 7863 | Py_DECREF(value); \ |
| 7864 | return NULL; \ |
| 7865 | } \ |
| 7866 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7867 | } \ |
| 7868 | |
| 7869 | SET(0, user); |
| 7870 | SET(1, system); |
| 7871 | SET(2, children_user); |
| 7872 | SET(3, children_system); |
| 7873 | SET(4, elapsed); |
| 7874 | |
| 7875 | #undef SET |
| 7876 | |
| 7877 | return value; |
| 7878 | } |
| 7879 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7880 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7881 | #ifndef MS_WINDOWS |
| 7882 | #define NEED_TICKS_PER_SECOND |
| 7883 | static long ticks_per_second = -1; |
| 7884 | #endif /* MS_WINDOWS */ |
| 7885 | |
| 7886 | /*[clinic input] |
| 7887 | os.times |
| 7888 | |
| 7889 | Return a collection containing process timing information. |
| 7890 | |
| 7891 | The object returned behaves like a named tuple with these fields: |
| 7892 | (utime, stime, cutime, cstime, elapsed_time) |
| 7893 | All fields are floating point numbers. |
| 7894 | [clinic start generated code]*/ |
| 7895 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7896 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7897 | os_times_impl(PyObject *module) |
| 7898 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7899 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7900 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7901 | FILETIME create, exit, kernel, user; |
| 7902 | HANDLE hProc; |
| 7903 | hProc = GetCurrentProcess(); |
| 7904 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7905 | /* The fields of a FILETIME structure are the hi and lo part |
| 7906 | of a 64-bit value expressed in 100 nanosecond units. |
| 7907 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7908 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7909 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7910 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7911 | (double)(user.dwHighDateTime*429.4967296 + |
| 7912 | user.dwLowDateTime*1e-7), |
| 7913 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7914 | kernel.dwLowDateTime*1e-7), |
| 7915 | (double)0, |
| 7916 | (double)0, |
| 7917 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7918 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7919 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7920 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7921 | |
| 7922 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7923 | struct tms t; |
| 7924 | clock_t c; |
| 7925 | errno = 0; |
| 7926 | c = times(&t); |
| 7927 | if (c == (clock_t) -1) |
| 7928 | return posix_error(); |
| 7929 | return build_times_result( |
| 7930 | (double)t.tms_utime / ticks_per_second, |
| 7931 | (double)t.tms_stime / ticks_per_second, |
| 7932 | (double)t.tms_cutime / ticks_per_second, |
| 7933 | (double)t.tms_cstime / ticks_per_second, |
| 7934 | (double)c / ticks_per_second); |
| 7935 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7936 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7937 | #endif /* HAVE_TIMES */ |
| 7938 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7939 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7940 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7941 | /*[clinic input] |
| 7942 | os.getsid |
| 7943 | |
| 7944 | pid: pid_t |
| 7945 | / |
| 7946 | |
| 7947 | Call the system call getsid(pid) and return the result. |
| 7948 | [clinic start generated code]*/ |
| 7949 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7950 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7951 | os_getsid_impl(PyObject *module, pid_t pid) |
| 7952 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7953 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7954 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7955 | sid = getsid(pid); |
| 7956 | if (sid < 0) |
| 7957 | return posix_error(); |
| 7958 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7959 | } |
| 7960 | #endif /* HAVE_GETSID */ |
| 7961 | |
| 7962 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7963 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7964 | /*[clinic input] |
| 7965 | os.setsid |
| 7966 | |
| 7967 | Call the system call setsid(). |
| 7968 | [clinic start generated code]*/ |
| 7969 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7970 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7971 | os_setsid_impl(PyObject *module) |
| 7972 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7973 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7974 | if (setsid() < 0) |
| 7975 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7976 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7977 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7978 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7979 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7980 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7981 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7982 | /*[clinic input] |
| 7983 | os.setpgid |
| 7984 | |
| 7985 | pid: pid_t |
| 7986 | pgrp: pid_t |
| 7987 | / |
| 7988 | |
| 7989 | Call the system call setpgid(pid, pgrp). |
| 7990 | [clinic start generated code]*/ |
| 7991 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7992 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7993 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 7994 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7995 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7996 | if (setpgid(pid, pgrp) < 0) |
| 7997 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7998 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7999 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8000 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8001 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8002 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8003 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8004 | /*[clinic input] |
| 8005 | os.tcgetpgrp |
| 8006 | |
| 8007 | fd: int |
| 8008 | / |
| 8009 | |
| 8010 | Return the process group associated with the terminal specified by fd. |
| 8011 | [clinic start generated code]*/ |
| 8012 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8013 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8014 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8015 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8016 | { |
| 8017 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8018 | if (pgid < 0) |
| 8019 | return posix_error(); |
| 8020 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8021 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8022 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8023 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8024 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8025 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8026 | /*[clinic input] |
| 8027 | os.tcsetpgrp |
| 8028 | |
| 8029 | fd: int |
| 8030 | pgid: pid_t |
| 8031 | / |
| 8032 | |
| 8033 | Set the process group associated with the terminal specified by fd. |
| 8034 | [clinic start generated code]*/ |
| 8035 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8036 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8037 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8038 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8039 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8040 | if (tcsetpgrp(fd, pgid) < 0) |
| 8041 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8042 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8043 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8044 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8045 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8046 | /* Functions acting on file descriptors */ |
| 8047 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8048 | #ifdef O_CLOEXEC |
| 8049 | extern int _Py_open_cloexec_works; |
| 8050 | #endif |
| 8051 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8052 | |
| 8053 | /*[clinic input] |
| 8054 | os.open -> int |
| 8055 | path: path_t |
| 8056 | flags: int |
| 8057 | mode: int = 0o777 |
| 8058 | * |
| 8059 | dir_fd: dir_fd(requires='openat') = None |
| 8060 | |
| 8061 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 8062 | |
| 8063 | Open a file for low level IO. Returns a file descriptor (integer). |
| 8064 | |
| 8065 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8066 | and path should be relative; path will then be relative to that directory. |
| 8067 | dir_fd may not be implemented on your platform. |
| 8068 | If it is unavailable, using it will raise a NotImplementedError. |
| 8069 | [clinic start generated code]*/ |
| 8070 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8071 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8072 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 8073 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8074 | { |
| 8075 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8076 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8077 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8078 | #ifdef O_CLOEXEC |
| 8079 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 8080 | #elif !defined(MS_WINDOWS) |
| 8081 | int *atomic_flag_works = NULL; |
| 8082 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8083 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8084 | #ifdef MS_WINDOWS |
| 8085 | flags |= O_NOINHERIT; |
| 8086 | #elif defined(O_CLOEXEC) |
| 8087 | flags |= O_CLOEXEC; |
| 8088 | #endif |
| 8089 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8090 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8091 | do { |
| 8092 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8093 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8094 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8095 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8096 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8097 | if (dir_fd != DEFAULT_DIR_FD) |
| 8098 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 8099 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8100 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8101 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8102 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8103 | Py_END_ALLOW_THREADS |
| 8104 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8105 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8106 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8107 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8108 | if (!async_err) |
| 8109 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8110 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8111 | } |
| 8112 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8113 | #ifndef MS_WINDOWS |
| 8114 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 8115 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8116 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8117 | } |
| 8118 | #endif |
| 8119 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8120 | return fd; |
| 8121 | } |
| 8122 | |
| 8123 | |
| 8124 | /*[clinic input] |
| 8125 | os.close |
| 8126 | |
| 8127 | fd: int |
| 8128 | |
| 8129 | Close a file descriptor. |
| 8130 | [clinic start generated code]*/ |
| 8131 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8132 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8133 | os_close_impl(PyObject *module, int fd) |
| 8134 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8135 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8136 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8137 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 8138 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 8139 | * for more details. |
| 8140 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8141 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8142 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8143 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8144 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8145 | Py_END_ALLOW_THREADS |
| 8146 | if (res < 0) |
| 8147 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8148 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8149 | } |
| 8150 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8151 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8152 | /*[clinic input] |
| 8153 | os.closerange |
| 8154 | |
| 8155 | fd_low: int |
| 8156 | fd_high: int |
| 8157 | / |
| 8158 | |
| 8159 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 8160 | [clinic start generated code]*/ |
| 8161 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8162 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8163 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 8164 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8165 | { |
| 8166 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8167 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8168 | _Py_BEGIN_SUPPRESS_IPH |
Benjamin Peterson | 207116b | 2016-09-08 11:28:06 -0700 | [diff] [blame] | 8169 | for (i = Py_MAX(fd_low, 0); i < fd_high; i++) |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 8170 | close(i); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8171 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8172 | Py_END_ALLOW_THREADS |
| 8173 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8174 | } |
| 8175 | |
| 8176 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8177 | /*[clinic input] |
| 8178 | os.dup -> int |
| 8179 | |
| 8180 | fd: int |
| 8181 | / |
| 8182 | |
| 8183 | Return a duplicate of a file descriptor. |
| 8184 | [clinic start generated code]*/ |
| 8185 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8186 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8187 | os_dup_impl(PyObject *module, int fd) |
| 8188 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8189 | { |
| 8190 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8191 | } |
| 8192 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8193 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8194 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8195 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8196 | fd: int |
| 8197 | fd2: int |
| 8198 | inheritable: bool=True |
| 8199 | |
| 8200 | Duplicate file descriptor. |
| 8201 | [clinic start generated code]*/ |
| 8202 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8203 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8204 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8205 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8206 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8207 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8208 | #if defined(HAVE_DUP3) && \ |
| 8209 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8210 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8211 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8212 | #endif |
| 8213 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8214 | if (fd < 0 || fd2 < 0) { |
| 8215 | posix_error(); |
| 8216 | return -1; |
| 8217 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8218 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8219 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8220 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8221 | * upon close(), and therefore below. |
| 8222 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8223 | #ifdef MS_WINDOWS |
| 8224 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8225 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8226 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8227 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8228 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8229 | if (res < 0) { |
| 8230 | posix_error(); |
| 8231 | return -1; |
| 8232 | } |
| 8233 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8234 | |
| 8235 | /* Character files like console cannot be make non-inheritable */ |
| 8236 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8237 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8238 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8239 | } |
| 8240 | |
| 8241 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8242 | Py_BEGIN_ALLOW_THREADS |
| 8243 | if (!inheritable) |
| 8244 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8245 | else |
| 8246 | res = dup2(fd, fd2); |
| 8247 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8248 | if (res < 0) { |
| 8249 | posix_error(); |
| 8250 | return -1; |
| 8251 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8252 | |
| 8253 | #else |
| 8254 | |
| 8255 | #ifdef HAVE_DUP3 |
| 8256 | if (!inheritable && dup3_works != 0) { |
| 8257 | Py_BEGIN_ALLOW_THREADS |
| 8258 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8259 | Py_END_ALLOW_THREADS |
| 8260 | if (res < 0) { |
| 8261 | if (dup3_works == -1) |
| 8262 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8263 | if (dup3_works) { |
| 8264 | posix_error(); |
| 8265 | return -1; |
| 8266 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8267 | } |
| 8268 | } |
| 8269 | |
| 8270 | if (inheritable || dup3_works == 0) |
| 8271 | { |
| 8272 | #endif |
| 8273 | Py_BEGIN_ALLOW_THREADS |
| 8274 | res = dup2(fd, fd2); |
| 8275 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8276 | if (res < 0) { |
| 8277 | posix_error(); |
| 8278 | return -1; |
| 8279 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8280 | |
| 8281 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8282 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8283 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8284 | } |
| 8285 | #ifdef HAVE_DUP3 |
| 8286 | } |
| 8287 | #endif |
| 8288 | |
| 8289 | #endif |
| 8290 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8291 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8292 | } |
| 8293 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8294 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8295 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8296 | /*[clinic input] |
| 8297 | os.lockf |
| 8298 | |
| 8299 | fd: int |
| 8300 | An open file descriptor. |
| 8301 | command: int |
| 8302 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8303 | length: Py_off_t |
| 8304 | The number of bytes to lock, starting at the current position. |
| 8305 | / |
| 8306 | |
| 8307 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8308 | |
| 8309 | [clinic start generated code]*/ |
| 8310 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8311 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8312 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8313 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8314 | { |
| 8315 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8316 | |
| 8317 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8318 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8319 | Py_END_ALLOW_THREADS |
| 8320 | |
| 8321 | if (res < 0) |
| 8322 | return posix_error(); |
| 8323 | |
| 8324 | Py_RETURN_NONE; |
| 8325 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8326 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8327 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8328 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8329 | /*[clinic input] |
| 8330 | os.lseek -> Py_off_t |
| 8331 | |
| 8332 | fd: int |
| 8333 | position: Py_off_t |
| 8334 | how: int |
| 8335 | / |
| 8336 | |
| 8337 | Set the position of a file descriptor. Return the new position. |
| 8338 | |
| 8339 | Return the new cursor position in number of bytes |
| 8340 | relative to the beginning of the file. |
| 8341 | [clinic start generated code]*/ |
| 8342 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8343 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8344 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8345 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8346 | { |
| 8347 | Py_off_t result; |
| 8348 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8349 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8350 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8351 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8352 | case 0: how = SEEK_SET; break; |
| 8353 | case 1: how = SEEK_CUR; break; |
| 8354 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8355 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8356 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8357 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8358 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8359 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8360 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8361 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8362 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8363 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8364 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8365 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8366 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8367 | if (result < 0) |
| 8368 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8369 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8370 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8371 | } |
| 8372 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8373 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8374 | /*[clinic input] |
| 8375 | os.read |
| 8376 | fd: int |
| 8377 | length: Py_ssize_t |
| 8378 | / |
| 8379 | |
| 8380 | Read from a file descriptor. Returns a bytes object. |
| 8381 | [clinic start generated code]*/ |
| 8382 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8383 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8384 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 8385 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8386 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8387 | Py_ssize_t n; |
| 8388 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8389 | |
| 8390 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8391 | errno = EINVAL; |
| 8392 | return posix_error(); |
| 8393 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8394 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 8395 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8396 | |
| 8397 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8398 | if (buffer == NULL) |
| 8399 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8400 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8401 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8402 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8403 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8404 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8405 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8406 | |
| 8407 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8408 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8409 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8410 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8411 | } |
| 8412 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8413 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8414 | || defined(__APPLE__))) \ |
| 8415 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 8416 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 8417 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8418 | 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] | 8419 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8420 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8421 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8422 | *iov = PyMem_New(struct iovec, cnt); |
| 8423 | if (*iov == NULL) { |
| 8424 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8425 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8426 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8427 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8428 | *buf = PyMem_New(Py_buffer, cnt); |
| 8429 | if (*buf == NULL) { |
| 8430 | PyMem_Del(*iov); |
| 8431 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8432 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8433 | } |
| 8434 | |
| 8435 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8436 | PyObject *item = PySequence_GetItem(seq, i); |
| 8437 | if (item == NULL) |
| 8438 | goto fail; |
| 8439 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8440 | Py_DECREF(item); |
| 8441 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8442 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8443 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8444 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8445 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8446 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8447 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8448 | |
| 8449 | fail: |
| 8450 | PyMem_Del(*iov); |
| 8451 | for (j = 0; j < i; j++) { |
| 8452 | PyBuffer_Release(&(*buf)[j]); |
| 8453 | } |
| 8454 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8455 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8456 | } |
| 8457 | |
| 8458 | static void |
| 8459 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8460 | { |
| 8461 | int i; |
| 8462 | PyMem_Del(iov); |
| 8463 | for (i = 0; i < cnt; i++) { |
| 8464 | PyBuffer_Release(&buf[i]); |
| 8465 | } |
| 8466 | PyMem_Del(buf); |
| 8467 | } |
| 8468 | #endif |
| 8469 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8470 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8471 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8472 | /*[clinic input] |
| 8473 | os.readv -> Py_ssize_t |
| 8474 | |
| 8475 | fd: int |
| 8476 | buffers: object |
| 8477 | / |
| 8478 | |
| 8479 | Read from a file descriptor fd into an iterable of buffers. |
| 8480 | |
| 8481 | The buffers should be mutable buffers accepting bytes. |
| 8482 | readv will transfer data into each buffer until it is full |
| 8483 | and then move on to the next buffer in the sequence to hold |
| 8484 | the rest of the data. |
| 8485 | |
| 8486 | readv returns the total number of bytes read, |
| 8487 | which may be less than the total capacity of all the buffers. |
| 8488 | [clinic start generated code]*/ |
| 8489 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8490 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8491 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 8492 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8493 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8494 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8495 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8496 | struct iovec *iov; |
| 8497 | Py_buffer *buf; |
| 8498 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8499 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8500 | PyErr_SetString(PyExc_TypeError, |
| 8501 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8502 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8503 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8504 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8505 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8506 | if (cnt < 0) |
| 8507 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8508 | |
| 8509 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 8510 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8511 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8512 | do { |
| 8513 | Py_BEGIN_ALLOW_THREADS |
| 8514 | n = readv(fd, iov, cnt); |
| 8515 | Py_END_ALLOW_THREADS |
| 8516 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8517 | |
| 8518 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8519 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8520 | if (!async_err) |
| 8521 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8522 | return -1; |
| 8523 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8524 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8525 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8526 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8527 | #endif /* HAVE_READV */ |
| 8528 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8529 | |
| 8530 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8531 | /*[clinic input] |
| 8532 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 8533 | os.pread |
| 8534 | |
| 8535 | fd: int |
| 8536 | length: int |
| 8537 | offset: Py_off_t |
| 8538 | / |
| 8539 | |
| 8540 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 8541 | |
| 8542 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 8543 | the beginning of the file. The file offset remains unchanged. |
| 8544 | [clinic start generated code]*/ |
| 8545 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8546 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8547 | os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset) |
| 8548 | /*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8549 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8550 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8551 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8552 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8553 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8554 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8555 | errno = EINVAL; |
| 8556 | return posix_error(); |
| 8557 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8558 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8559 | if (buffer == NULL) |
| 8560 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8561 | |
| 8562 | do { |
| 8563 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8564 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8565 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8566 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8567 | Py_END_ALLOW_THREADS |
| 8568 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8569 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8570 | if (n < 0) { |
| 8571 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8572 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8573 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8574 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8575 | _PyBytes_Resize(&buffer, n); |
| 8576 | return buffer; |
| 8577 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8578 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8579 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 8580 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 8581 | /*[clinic input] |
| 8582 | os.preadv -> Py_ssize_t |
| 8583 | |
| 8584 | fd: int |
| 8585 | buffers: object |
| 8586 | offset: Py_off_t |
| 8587 | flags: int = 0 |
| 8588 | / |
| 8589 | |
| 8590 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 8591 | |
| 8592 | Combines the functionality of readv() and pread(). As readv(), it will |
| 8593 | transfer data into each buffer until it is full and then move on to the next |
| 8594 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 8595 | specifies the file offset at which the input operation is to be performed. It |
| 8596 | will return the total number of bytes read (which can be less than the total |
| 8597 | capacity of all the objects). |
| 8598 | |
| 8599 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 8600 | |
| 8601 | - RWF_HIPRI |
| 8602 | - RWF_NOWAIT |
| 8603 | |
| 8604 | Using non-zero flags requires Linux 4.6 or newer. |
| 8605 | [clinic start generated code]*/ |
| 8606 | |
| 8607 | static Py_ssize_t |
| 8608 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 8609 | int flags) |
| 8610 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 8611 | { |
| 8612 | Py_ssize_t cnt, n; |
| 8613 | int async_err = 0; |
| 8614 | struct iovec *iov; |
| 8615 | Py_buffer *buf; |
| 8616 | |
| 8617 | if (!PySequence_Check(buffers)) { |
| 8618 | PyErr_SetString(PyExc_TypeError, |
| 8619 | "preadv2() arg 2 must be a sequence"); |
| 8620 | return -1; |
| 8621 | } |
| 8622 | |
| 8623 | cnt = PySequence_Size(buffers); |
| 8624 | if (cnt < 0) { |
| 8625 | return -1; |
| 8626 | } |
| 8627 | |
| 8628 | #ifndef HAVE_PREADV2 |
| 8629 | if(flags != 0) { |
| 8630 | argument_unavailable_error("preadv2", "flags"); |
| 8631 | return -1; |
| 8632 | } |
| 8633 | #endif |
| 8634 | |
| 8635 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 8636 | return -1; |
| 8637 | } |
| 8638 | #ifdef HAVE_PREADV2 |
| 8639 | do { |
| 8640 | Py_BEGIN_ALLOW_THREADS |
| 8641 | _Py_BEGIN_SUPPRESS_IPH |
| 8642 | n = preadv2(fd, iov, cnt, offset, flags); |
| 8643 | _Py_END_SUPPRESS_IPH |
| 8644 | Py_END_ALLOW_THREADS |
| 8645 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8646 | #else |
| 8647 | do { |
| 8648 | Py_BEGIN_ALLOW_THREADS |
| 8649 | _Py_BEGIN_SUPPRESS_IPH |
| 8650 | n = preadv(fd, iov, cnt, offset); |
| 8651 | _Py_END_SUPPRESS_IPH |
| 8652 | Py_END_ALLOW_THREADS |
| 8653 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8654 | #endif |
| 8655 | |
| 8656 | iov_cleanup(iov, buf, cnt); |
| 8657 | if (n < 0) { |
| 8658 | if (!async_err) { |
| 8659 | posix_error(); |
| 8660 | } |
| 8661 | return -1; |
| 8662 | } |
| 8663 | |
| 8664 | return n; |
| 8665 | } |
| 8666 | #endif /* HAVE_PREADV */ |
| 8667 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8668 | |
| 8669 | /*[clinic input] |
| 8670 | os.write -> Py_ssize_t |
| 8671 | |
| 8672 | fd: int |
| 8673 | data: Py_buffer |
| 8674 | / |
| 8675 | |
| 8676 | Write a bytes object to a file descriptor. |
| 8677 | [clinic start generated code]*/ |
| 8678 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8679 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8680 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 8681 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8682 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8683 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8684 | } |
| 8685 | |
| 8686 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8687 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8688 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8689 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8690 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8691 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8692 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8693 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8694 | static PyObject * |
| 8695 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8696 | { |
| 8697 | int in, out; |
| 8698 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8699 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8700 | off_t offset; |
| 8701 | |
| 8702 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8703 | #ifndef __APPLE__ |
| 8704 | Py_ssize_t len; |
| 8705 | #endif |
| 8706 | PyObject *headers = NULL, *trailers = NULL; |
| 8707 | Py_buffer *hbuf, *tbuf; |
| 8708 | off_t sbytes; |
| 8709 | struct sf_hdtr sf; |
| 8710 | int flags = 0; |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8711 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8712 | static char *keywords[] = {"out", "in", |
| 8713 | "offset", "count", |
| 8714 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8715 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8716 | sf.headers = NULL; |
| 8717 | sf.trailers = NULL; |
| 8718 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8719 | #ifdef __APPLE__ |
| 8720 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8721 | 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] | 8722 | #else |
| 8723 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8724 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8725 | #endif |
| 8726 | &headers, &trailers, &flags)) |
| 8727 | return NULL; |
| 8728 | if (headers != NULL) { |
| 8729 | if (!PySequence_Check(headers)) { |
| 8730 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8731 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8732 | return NULL; |
| 8733 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8734 | Py_ssize_t i = PySequence_Size(headers); |
| 8735 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8736 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8737 | if (i > INT_MAX) { |
| 8738 | PyErr_SetString(PyExc_OverflowError, |
| 8739 | "sendfile() header is too large"); |
| 8740 | return NULL; |
| 8741 | } |
| 8742 | if (i > 0) { |
| 8743 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8744 | if (iov_setup(&(sf.headers), &hbuf, |
| 8745 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8746 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8747 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8748 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 8749 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 8750 | # define OFF_T_MAX 0x7fffffffffffffff |
| 8751 | if (sbytes >= OFF_T_MAX - blen) { |
| 8752 | PyErr_SetString(PyExc_OverflowError, |
| 8753 | "sendfile() header is too large"); |
| 8754 | return NULL; |
| 8755 | } |
| 8756 | sbytes += blen; |
| 8757 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8758 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8759 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8760 | } |
| 8761 | } |
| 8762 | if (trailers != NULL) { |
| 8763 | if (!PySequence_Check(trailers)) { |
| 8764 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8765 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8766 | return NULL; |
| 8767 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8768 | Py_ssize_t i = PySequence_Size(trailers); |
| 8769 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8770 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8771 | if (i > INT_MAX) { |
| 8772 | PyErr_SetString(PyExc_OverflowError, |
| 8773 | "sendfile() trailer is too large"); |
| 8774 | return NULL; |
| 8775 | } |
| 8776 | if (i > 0) { |
| 8777 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8778 | if (iov_setup(&(sf.trailers), &tbuf, |
| 8779 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8780 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8781 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8782 | } |
| 8783 | } |
| 8784 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8785 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8786 | do { |
| 8787 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8788 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8789 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8790 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8791 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8792 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8793 | Py_END_ALLOW_THREADS |
| 8794 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8795 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8796 | |
| 8797 | if (sf.headers != NULL) |
| 8798 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8799 | if (sf.trailers != NULL) |
| 8800 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8801 | |
| 8802 | if (ret < 0) { |
| 8803 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8804 | if (sbytes != 0) { |
| 8805 | // some data has been sent |
| 8806 | goto done; |
| 8807 | } |
| 8808 | else { |
| 8809 | // no data has been sent; upper application is supposed |
| 8810 | // to retry on EAGAIN or EBUSY |
| 8811 | return posix_error(); |
| 8812 | } |
| 8813 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8814 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8815 | } |
| 8816 | goto done; |
| 8817 | |
| 8818 | done: |
| 8819 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8820 | return Py_BuildValue("l", sbytes); |
| 8821 | #else |
| 8822 | return Py_BuildValue("L", sbytes); |
| 8823 | #endif |
| 8824 | |
| 8825 | #else |
| 8826 | Py_ssize_t count; |
| 8827 | PyObject *offobj; |
| 8828 | static char *keywords[] = {"out", "in", |
| 8829 | "offset", "count", NULL}; |
| 8830 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8831 | keywords, &out, &in, &offobj, &count)) |
| 8832 | return NULL; |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 8833 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8834 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8835 | do { |
| 8836 | Py_BEGIN_ALLOW_THREADS |
| 8837 | ret = sendfile(out, in, NULL, count); |
| 8838 | Py_END_ALLOW_THREADS |
| 8839 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8840 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8841 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8842 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8843 | } |
| 8844 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8845 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8846 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8847 | |
| 8848 | do { |
| 8849 | Py_BEGIN_ALLOW_THREADS |
| 8850 | ret = sendfile(out, in, &offset, count); |
| 8851 | Py_END_ALLOW_THREADS |
| 8852 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8853 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8854 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8855 | return Py_BuildValue("n", ret); |
| 8856 | #endif |
| 8857 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8858 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8859 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8860 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 8861 | #if defined(__APPLE__) |
| 8862 | /*[clinic input] |
| 8863 | os._fcopyfile |
| 8864 | |
| 8865 | infd: int |
| 8866 | outfd: int |
| 8867 | flags: int |
| 8868 | / |
| 8869 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 8870 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 8871 | [clinic start generated code]*/ |
| 8872 | |
| 8873 | static PyObject * |
| 8874 | os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags) |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 8875 | /*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 8876 | { |
| 8877 | int ret; |
| 8878 | |
| 8879 | Py_BEGIN_ALLOW_THREADS |
| 8880 | ret = fcopyfile(infd, outfd, NULL, flags); |
| 8881 | Py_END_ALLOW_THREADS |
| 8882 | if (ret < 0) |
| 8883 | return posix_error(); |
| 8884 | Py_RETURN_NONE; |
| 8885 | } |
| 8886 | #endif |
| 8887 | |
| 8888 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8889 | /*[clinic input] |
| 8890 | os.fstat |
| 8891 | |
| 8892 | fd : int |
| 8893 | |
| 8894 | Perform a stat system call on the given file descriptor. |
| 8895 | |
| 8896 | Like stat(), but for an open file descriptor. |
| 8897 | Equivalent to os.stat(fd). |
| 8898 | [clinic start generated code]*/ |
| 8899 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8900 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8901 | os_fstat_impl(PyObject *module, int fd) |
| 8902 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8903 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8904 | STRUCT_STAT st; |
| 8905 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8906 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8907 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8908 | do { |
| 8909 | Py_BEGIN_ALLOW_THREADS |
| 8910 | res = FSTAT(fd, &st); |
| 8911 | Py_END_ALLOW_THREADS |
| 8912 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8913 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8914 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8915 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8916 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8917 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8918 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8919 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8920 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8921 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8922 | } |
| 8923 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8924 | |
| 8925 | /*[clinic input] |
| 8926 | os.isatty -> bool |
| 8927 | fd: int |
| 8928 | / |
| 8929 | |
| 8930 | Return True if the fd is connected to a terminal. |
| 8931 | |
| 8932 | Return True if the file descriptor is an open file descriptor |
| 8933 | connected to the slave end of a terminal. |
| 8934 | [clinic start generated code]*/ |
| 8935 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8936 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8937 | os_isatty_impl(PyObject *module, int fd) |
| 8938 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8939 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8940 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8941 | _Py_BEGIN_SUPPRESS_IPH |
| 8942 | return_value = isatty(fd); |
| 8943 | _Py_END_SUPPRESS_IPH |
| 8944 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8945 | } |
| 8946 | |
| 8947 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8948 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8949 | /*[clinic input] |
| 8950 | os.pipe |
| 8951 | |
| 8952 | Create a pipe. |
| 8953 | |
| 8954 | Returns a tuple of two file descriptors: |
| 8955 | (read_fd, write_fd) |
| 8956 | [clinic start generated code]*/ |
| 8957 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8958 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8959 | os_pipe_impl(PyObject *module) |
| 8960 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8961 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8962 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8963 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8964 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8965 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8966 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8967 | #else |
| 8968 | int res; |
| 8969 | #endif |
| 8970 | |
| 8971 | #ifdef MS_WINDOWS |
| 8972 | attr.nLength = sizeof(attr); |
| 8973 | attr.lpSecurityDescriptor = NULL; |
| 8974 | attr.bInheritHandle = FALSE; |
| 8975 | |
| 8976 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 8977 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8978 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8979 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 8980 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 8981 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8982 | if (fds[0] == -1 || fds[1] == -1) { |
| 8983 | CloseHandle(read); |
| 8984 | CloseHandle(write); |
| 8985 | ok = 0; |
| 8986 | } |
| 8987 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 8988 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8989 | Py_END_ALLOW_THREADS |
| 8990 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8991 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8992 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8993 | #else |
| 8994 | |
| 8995 | #ifdef HAVE_PIPE2 |
| 8996 | Py_BEGIN_ALLOW_THREADS |
| 8997 | res = pipe2(fds, O_CLOEXEC); |
| 8998 | Py_END_ALLOW_THREADS |
| 8999 | |
| 9000 | if (res != 0 && errno == ENOSYS) |
| 9001 | { |
| 9002 | #endif |
| 9003 | Py_BEGIN_ALLOW_THREADS |
| 9004 | res = pipe(fds); |
| 9005 | Py_END_ALLOW_THREADS |
| 9006 | |
| 9007 | if (res == 0) { |
| 9008 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 9009 | close(fds[0]); |
| 9010 | close(fds[1]); |
| 9011 | return NULL; |
| 9012 | } |
| 9013 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 9014 | close(fds[0]); |
| 9015 | close(fds[1]); |
| 9016 | return NULL; |
| 9017 | } |
| 9018 | } |
| 9019 | #ifdef HAVE_PIPE2 |
| 9020 | } |
| 9021 | #endif |
| 9022 | |
| 9023 | if (res != 0) |
| 9024 | return PyErr_SetFromErrno(PyExc_OSError); |
| 9025 | #endif /* !MS_WINDOWS */ |
| 9026 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9027 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9028 | #endif /* HAVE_PIPE */ |
| 9029 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9030 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9031 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9032 | /*[clinic input] |
| 9033 | os.pipe2 |
| 9034 | |
| 9035 | flags: int |
| 9036 | / |
| 9037 | |
| 9038 | Create a pipe with flags set atomically. |
| 9039 | |
| 9040 | Returns a tuple of two file descriptors: |
| 9041 | (read_fd, write_fd) |
| 9042 | |
| 9043 | flags can be constructed by ORing together one or more of these values: |
| 9044 | O_NONBLOCK, O_CLOEXEC. |
| 9045 | [clinic start generated code]*/ |
| 9046 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9047 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9048 | os_pipe2_impl(PyObject *module, int flags) |
| 9049 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9050 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9051 | int fds[2]; |
| 9052 | int res; |
| 9053 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9054 | res = pipe2(fds, flags); |
| 9055 | if (res != 0) |
| 9056 | return posix_error(); |
| 9057 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 9058 | } |
| 9059 | #endif /* HAVE_PIPE2 */ |
| 9060 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9061 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9062 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9063 | /*[clinic input] |
| 9064 | os.writev -> Py_ssize_t |
| 9065 | fd: int |
| 9066 | buffers: object |
| 9067 | / |
| 9068 | |
| 9069 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 9070 | |
| 9071 | Returns the total number of bytes written. |
| 9072 | buffers must be a sequence of bytes-like objects. |
| 9073 | [clinic start generated code]*/ |
| 9074 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9075 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9076 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 9077 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9078 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9079 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9080 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9081 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9082 | struct iovec *iov; |
| 9083 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9084 | |
| 9085 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9086 | PyErr_SetString(PyExc_TypeError, |
| 9087 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9088 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9089 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9090 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9091 | if (cnt < 0) |
| 9092 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9093 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9094 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9095 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9096 | } |
| 9097 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9098 | do { |
| 9099 | Py_BEGIN_ALLOW_THREADS |
| 9100 | result = writev(fd, iov, cnt); |
| 9101 | Py_END_ALLOW_THREADS |
| 9102 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9103 | |
| 9104 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9105 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9106 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9107 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9108 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9109 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9110 | #endif /* HAVE_WRITEV */ |
| 9111 | |
| 9112 | |
| 9113 | #ifdef HAVE_PWRITE |
| 9114 | /*[clinic input] |
| 9115 | os.pwrite -> Py_ssize_t |
| 9116 | |
| 9117 | fd: int |
| 9118 | buffer: Py_buffer |
| 9119 | offset: Py_off_t |
| 9120 | / |
| 9121 | |
| 9122 | Write bytes to a file descriptor starting at a particular offset. |
| 9123 | |
| 9124 | Write buffer to fd, starting at offset bytes from the beginning of |
| 9125 | the file. Returns the number of bytes writte. Does not change the |
| 9126 | current file offset. |
| 9127 | [clinic start generated code]*/ |
| 9128 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9129 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9130 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 9131 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9132 | { |
| 9133 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9134 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9135 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9136 | do { |
| 9137 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9138 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9139 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9140 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9141 | Py_END_ALLOW_THREADS |
| 9142 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9143 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9144 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9145 | posix_error(); |
| 9146 | return size; |
| 9147 | } |
| 9148 | #endif /* HAVE_PWRITE */ |
| 9149 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9150 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9151 | /*[clinic input] |
| 9152 | os.pwritev -> Py_ssize_t |
| 9153 | |
| 9154 | fd: int |
| 9155 | buffers: object |
| 9156 | offset: Py_off_t |
| 9157 | flags: int = 0 |
| 9158 | / |
| 9159 | |
| 9160 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 9161 | |
| 9162 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 9163 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 9164 | buffer is written before proceeding to second, and so on. The operating system may |
| 9165 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 9166 | This function writes the contents of each object to the file descriptor and returns |
| 9167 | the total number of bytes written. |
| 9168 | |
| 9169 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9170 | |
| 9171 | - RWF_DSYNC |
| 9172 | - RWF_SYNC |
| 9173 | |
| 9174 | Using non-zero flags requires Linux 4.7 or newer. |
| 9175 | [clinic start generated code]*/ |
| 9176 | |
| 9177 | static Py_ssize_t |
| 9178 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9179 | int flags) |
| 9180 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/ |
| 9181 | { |
| 9182 | Py_ssize_t cnt; |
| 9183 | Py_ssize_t result; |
| 9184 | int async_err = 0; |
| 9185 | struct iovec *iov; |
| 9186 | Py_buffer *buf; |
| 9187 | |
| 9188 | if (!PySequence_Check(buffers)) { |
| 9189 | PyErr_SetString(PyExc_TypeError, |
| 9190 | "pwritev() arg 2 must be a sequence"); |
| 9191 | return -1; |
| 9192 | } |
| 9193 | |
| 9194 | cnt = PySequence_Size(buffers); |
| 9195 | if (cnt < 0) { |
| 9196 | return -1; |
| 9197 | } |
| 9198 | |
| 9199 | #ifndef HAVE_PWRITEV2 |
| 9200 | if(flags != 0) { |
| 9201 | argument_unavailable_error("pwritev2", "flags"); |
| 9202 | return -1; |
| 9203 | } |
| 9204 | #endif |
| 9205 | |
| 9206 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9207 | return -1; |
| 9208 | } |
| 9209 | #ifdef HAVE_PWRITEV2 |
| 9210 | do { |
| 9211 | Py_BEGIN_ALLOW_THREADS |
| 9212 | _Py_BEGIN_SUPPRESS_IPH |
| 9213 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9214 | _Py_END_SUPPRESS_IPH |
| 9215 | Py_END_ALLOW_THREADS |
| 9216 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9217 | #else |
| 9218 | do { |
| 9219 | Py_BEGIN_ALLOW_THREADS |
| 9220 | _Py_BEGIN_SUPPRESS_IPH |
| 9221 | result = pwritev(fd, iov, cnt, offset); |
| 9222 | _Py_END_SUPPRESS_IPH |
| 9223 | Py_END_ALLOW_THREADS |
| 9224 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9225 | #endif |
| 9226 | |
| 9227 | iov_cleanup(iov, buf, cnt); |
| 9228 | if (result < 0) { |
| 9229 | if (!async_err) { |
| 9230 | posix_error(); |
| 9231 | } |
| 9232 | return -1; |
| 9233 | } |
| 9234 | |
| 9235 | return result; |
| 9236 | } |
| 9237 | #endif /* HAVE_PWRITEV */ |
| 9238 | |
| 9239 | |
| 9240 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9241 | |
| 9242 | #ifdef HAVE_MKFIFO |
| 9243 | /*[clinic input] |
| 9244 | os.mkfifo |
| 9245 | |
| 9246 | path: path_t |
| 9247 | mode: int=0o666 |
| 9248 | * |
| 9249 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9250 | |
| 9251 | Create a "fifo" (a POSIX named pipe). |
| 9252 | |
| 9253 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9254 | and path should be relative; path will then be relative to that directory. |
| 9255 | dir_fd may not be implemented on your platform. |
| 9256 | If it is unavailable, using it will raise a NotImplementedError. |
| 9257 | [clinic start generated code]*/ |
| 9258 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9259 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9260 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 9261 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9262 | { |
| 9263 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9264 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9265 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9266 | do { |
| 9267 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9268 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9269 | if (dir_fd != DEFAULT_DIR_FD) |
| 9270 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 9271 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9272 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9273 | result = mkfifo(path->narrow, mode); |
| 9274 | Py_END_ALLOW_THREADS |
| 9275 | } while (result != 0 && errno == EINTR && |
| 9276 | !(async_err = PyErr_CheckSignals())); |
| 9277 | if (result != 0) |
| 9278 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9279 | |
| 9280 | Py_RETURN_NONE; |
| 9281 | } |
| 9282 | #endif /* HAVE_MKFIFO */ |
| 9283 | |
| 9284 | |
| 9285 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 9286 | /*[clinic input] |
| 9287 | os.mknod |
| 9288 | |
| 9289 | path: path_t |
| 9290 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9291 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9292 | * |
| 9293 | dir_fd: dir_fd(requires='mknodat')=None |
| 9294 | |
| 9295 | Create a node in the file system. |
| 9296 | |
| 9297 | Create a node in the file system (file, device special file or named pipe) |
| 9298 | at path. mode specifies both the permissions to use and the |
| 9299 | type of node to be created, being combined (bitwise OR) with one of |
| 9300 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 9301 | device defines the newly created device special file (probably using |
| 9302 | os.makedev()). Otherwise device is ignored. |
| 9303 | |
| 9304 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9305 | and path should be relative; path will then be relative to that directory. |
| 9306 | dir_fd may not be implemented on your platform. |
| 9307 | If it is unavailable, using it will raise a NotImplementedError. |
| 9308 | [clinic start generated code]*/ |
| 9309 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9310 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9311 | 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] | 9312 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9313 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9314 | { |
| 9315 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9316 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9317 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9318 | do { |
| 9319 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9320 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9321 | if (dir_fd != DEFAULT_DIR_FD) |
| 9322 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 9323 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9324 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9325 | result = mknod(path->narrow, mode, device); |
| 9326 | Py_END_ALLOW_THREADS |
| 9327 | } while (result != 0 && errno == EINTR && |
| 9328 | !(async_err = PyErr_CheckSignals())); |
| 9329 | if (result != 0) |
| 9330 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9331 | |
| 9332 | Py_RETURN_NONE; |
| 9333 | } |
| 9334 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 9335 | |
| 9336 | |
| 9337 | #ifdef HAVE_DEVICE_MACROS |
| 9338 | /*[clinic input] |
| 9339 | os.major -> unsigned_int |
| 9340 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9341 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9342 | / |
| 9343 | |
| 9344 | Extracts a device major number from a raw device number. |
| 9345 | [clinic start generated code]*/ |
| 9346 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9347 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9348 | os_major_impl(PyObject *module, dev_t device) |
| 9349 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9350 | { |
| 9351 | return major(device); |
| 9352 | } |
| 9353 | |
| 9354 | |
| 9355 | /*[clinic input] |
| 9356 | os.minor -> unsigned_int |
| 9357 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9358 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9359 | / |
| 9360 | |
| 9361 | Extracts a device minor number from a raw device number. |
| 9362 | [clinic start generated code]*/ |
| 9363 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9364 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9365 | os_minor_impl(PyObject *module, dev_t device) |
| 9366 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9367 | { |
| 9368 | return minor(device); |
| 9369 | } |
| 9370 | |
| 9371 | |
| 9372 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9373 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9374 | |
| 9375 | major: int |
| 9376 | minor: int |
| 9377 | / |
| 9378 | |
| 9379 | Composes a raw device number from the major and minor device numbers. |
| 9380 | [clinic start generated code]*/ |
| 9381 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9382 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9383 | os_makedev_impl(PyObject *module, int major, int minor) |
| 9384 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9385 | { |
| 9386 | return makedev(major, minor); |
| 9387 | } |
| 9388 | #endif /* HAVE_DEVICE_MACROS */ |
| 9389 | |
| 9390 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9391 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9392 | /*[clinic input] |
| 9393 | os.ftruncate |
| 9394 | |
| 9395 | fd: int |
| 9396 | length: Py_off_t |
| 9397 | / |
| 9398 | |
| 9399 | Truncate a file, specified by file descriptor, to a specific length. |
| 9400 | [clinic start generated code]*/ |
| 9401 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9402 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9403 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 9404 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9405 | { |
| 9406 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9407 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9408 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9409 | do { |
| 9410 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9411 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9412 | #ifdef MS_WINDOWS |
| 9413 | result = _chsize_s(fd, length); |
| 9414 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9415 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9416 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9417 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9418 | Py_END_ALLOW_THREADS |
| 9419 | } while (result != 0 && errno == EINTR && |
| 9420 | !(async_err = PyErr_CheckSignals())); |
| 9421 | if (result != 0) |
| 9422 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9423 | Py_RETURN_NONE; |
| 9424 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9425 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9426 | |
| 9427 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9428 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9429 | /*[clinic input] |
| 9430 | os.truncate |
| 9431 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 9432 | length: Py_off_t |
| 9433 | |
| 9434 | Truncate a file, specified by path, to a specific length. |
| 9435 | |
| 9436 | On some platforms, path may also be specified as an open file descriptor. |
| 9437 | If this functionality is unavailable, using it raises an exception. |
| 9438 | [clinic start generated code]*/ |
| 9439 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9440 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9441 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 9442 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9443 | { |
| 9444 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9445 | #ifdef MS_WINDOWS |
| 9446 | int fd; |
| 9447 | #endif |
| 9448 | |
| 9449 | if (path->fd != -1) |
| 9450 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9451 | |
| 9452 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9453 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9454 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 9455 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 9456 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9457 | result = -1; |
| 9458 | else { |
| 9459 | result = _chsize_s(fd, length); |
| 9460 | close(fd); |
| 9461 | if (result < 0) |
| 9462 | errno = result; |
| 9463 | } |
| 9464 | #else |
| 9465 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9466 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9467 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9468 | Py_END_ALLOW_THREADS |
| 9469 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 9470 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9471 | |
| 9472 | Py_RETURN_NONE; |
| 9473 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9474 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9475 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9476 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9477 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 9478 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 9479 | defined, which is the case in Python on AIX. AIX bug report: |
| 9480 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 9481 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 9482 | # define POSIX_FADVISE_AIX_BUG |
| 9483 | #endif |
| 9484 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9485 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9486 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9487 | /*[clinic input] |
| 9488 | os.posix_fallocate |
| 9489 | |
| 9490 | fd: int |
| 9491 | offset: Py_off_t |
| 9492 | length: Py_off_t |
| 9493 | / |
| 9494 | |
| 9495 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 9496 | |
| 9497 | Ensure that the file specified by fd encompasses a range of bytes |
| 9498 | starting at offset bytes from the beginning and continuing for length bytes. |
| 9499 | [clinic start generated code]*/ |
| 9500 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9501 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9502 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9503 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9504 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9505 | { |
| 9506 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9507 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9508 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9509 | do { |
| 9510 | Py_BEGIN_ALLOW_THREADS |
| 9511 | result = posix_fallocate(fd, offset, length); |
| 9512 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9513 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9514 | |
| 9515 | if (result == 0) |
| 9516 | Py_RETURN_NONE; |
| 9517 | |
| 9518 | if (async_err) |
| 9519 | return NULL; |
| 9520 | |
| 9521 | errno = result; |
| 9522 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9523 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9524 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9525 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9526 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9527 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9528 | /*[clinic input] |
| 9529 | os.posix_fadvise |
| 9530 | |
| 9531 | fd: int |
| 9532 | offset: Py_off_t |
| 9533 | length: Py_off_t |
| 9534 | advice: int |
| 9535 | / |
| 9536 | |
| 9537 | Announce an intention to access data in a specific pattern. |
| 9538 | |
| 9539 | Announce an intention to access data in a specific pattern, thus allowing |
| 9540 | the kernel to make optimizations. |
| 9541 | The advice applies to the region of the file specified by fd starting at |
| 9542 | offset and continuing for length bytes. |
| 9543 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 9544 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 9545 | POSIX_FADV_DONTNEED. |
| 9546 | [clinic start generated code]*/ |
| 9547 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9548 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9549 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9550 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9551 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9552 | { |
| 9553 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9554 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9555 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9556 | do { |
| 9557 | Py_BEGIN_ALLOW_THREADS |
| 9558 | result = posix_fadvise(fd, offset, length, advice); |
| 9559 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9560 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9561 | |
| 9562 | if (result == 0) |
| 9563 | Py_RETURN_NONE; |
| 9564 | |
| 9565 | if (async_err) |
| 9566 | return NULL; |
| 9567 | |
| 9568 | errno = result; |
| 9569 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9570 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9571 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9572 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9573 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9574 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 9575 | /* Save putenv() parameters as values here, so we can collect them when they |
| 9576 | * get re-set with another call for the same key. */ |
| 9577 | static PyObject *posix_putenv_garbage; |
| 9578 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9579 | static void |
| 9580 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9581 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9582 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 9583 | * this will cause previous value to be collected. This has to |
| 9584 | * happen after the real putenv() call because the old value |
| 9585 | * was still accessible until then. */ |
| 9586 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 9587 | /* really not much we can do; just leak */ |
| 9588 | PyErr_Clear(); |
| 9589 | else |
| 9590 | Py_DECREF(value); |
| 9591 | } |
| 9592 | |
| 9593 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 9594 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9595 | /*[clinic input] |
| 9596 | os.putenv |
| 9597 | |
| 9598 | name: unicode |
| 9599 | value: unicode |
| 9600 | / |
| 9601 | |
| 9602 | Change or add an environment variable. |
| 9603 | [clinic start generated code]*/ |
| 9604 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9605 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9606 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 9607 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9608 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 9609 | const wchar_t *env; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9610 | Py_ssize_t size; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9611 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9612 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 9613 | defining hidden environment variables. */ |
| 9614 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 9615 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 9616 | { |
| 9617 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 9618 | return NULL; |
| 9619 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9620 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 9621 | if (unicode == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9622 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9623 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9624 | |
| 9625 | env = PyUnicode_AsUnicodeAndSize(unicode, &size); |
| 9626 | if (env == NULL) |
| 9627 | goto error; |
| 9628 | if (size > _MAX_ENV) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9629 | PyErr_Format(PyExc_ValueError, |
| 9630 | "the environment variable is longer than %u characters", |
| 9631 | _MAX_ENV); |
| 9632 | goto error; |
| 9633 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9634 | if (wcslen(env) != (size_t)size) { |
| 9635 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9636 | goto error; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9637 | } |
| 9638 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9639 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9640 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9641 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9642 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9643 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9644 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9645 | Py_RETURN_NONE; |
| 9646 | |
| 9647 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9648 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9649 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9650 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9651 | #else /* MS_WINDOWS */ |
| 9652 | /*[clinic input] |
| 9653 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9654 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9655 | name: FSConverter |
| 9656 | value: FSConverter |
| 9657 | / |
| 9658 | |
| 9659 | Change or add an environment variable. |
| 9660 | [clinic start generated code]*/ |
| 9661 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9662 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9663 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 9664 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9665 | { |
| 9666 | PyObject *bytes = NULL; |
| 9667 | char *env; |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9668 | const char *name_string = PyBytes_AS_STRING(name); |
| 9669 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9670 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9671 | if (strchr(name_string, '=') != NULL) { |
| 9672 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 9673 | return NULL; |
| 9674 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9675 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 9676 | if (bytes == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9677 | return NULL; |
| 9678 | } |
| 9679 | |
| 9680 | env = PyBytes_AS_STRING(bytes); |
| 9681 | if (putenv(env)) { |
| 9682 | Py_DECREF(bytes); |
| 9683 | return posix_error(); |
| 9684 | } |
| 9685 | |
| 9686 | posix_putenv_garbage_setitem(name, bytes); |
| 9687 | Py_RETURN_NONE; |
| 9688 | } |
| 9689 | #endif /* MS_WINDOWS */ |
| 9690 | #endif /* HAVE_PUTENV */ |
| 9691 | |
| 9692 | |
| 9693 | #ifdef HAVE_UNSETENV |
| 9694 | /*[clinic input] |
| 9695 | os.unsetenv |
| 9696 | name: FSConverter |
| 9697 | / |
| 9698 | |
| 9699 | Delete an environment variable. |
| 9700 | [clinic start generated code]*/ |
| 9701 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9702 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9703 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 9704 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9705 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9706 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9707 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9708 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9709 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9710 | #ifdef HAVE_BROKEN_UNSETENV |
| 9711 | unsetenv(PyBytes_AS_STRING(name)); |
| 9712 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9713 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9714 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9715 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9716 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9717 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9718 | /* Remove the key from posix_putenv_garbage; |
| 9719 | * this will cause it to be collected. This has to |
| 9720 | * happen after the real unsetenv() call because the |
| 9721 | * old value was still accessible until then. |
| 9722 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9723 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9724 | /* really not much we can do; just leak */ |
| 9725 | PyErr_Clear(); |
| 9726 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9727 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9728 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9729 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9730 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9731 | |
| 9732 | /*[clinic input] |
| 9733 | os.strerror |
| 9734 | |
| 9735 | code: int |
| 9736 | / |
| 9737 | |
| 9738 | Translate an error code to a message string. |
| 9739 | [clinic start generated code]*/ |
| 9740 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9741 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9742 | os_strerror_impl(PyObject *module, int code) |
| 9743 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9744 | { |
| 9745 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9746 | if (message == NULL) { |
| 9747 | PyErr_SetString(PyExc_ValueError, |
| 9748 | "strerror() argument out of range"); |
| 9749 | return NULL; |
| 9750 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 9751 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9752 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9753 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9754 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9755 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9756 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9757 | /*[clinic input] |
| 9758 | os.WCOREDUMP -> bool |
| 9759 | |
| 9760 | status: int |
| 9761 | / |
| 9762 | |
| 9763 | Return True if the process returning status was dumped to a core file. |
| 9764 | [clinic start generated code]*/ |
| 9765 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9766 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9767 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 9768 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9769 | { |
| 9770 | WAIT_TYPE wait_status; |
| 9771 | WAIT_STATUS_INT(wait_status) = status; |
| 9772 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9773 | } |
| 9774 | #endif /* WCOREDUMP */ |
| 9775 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9776 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9777 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9778 | /*[clinic input] |
| 9779 | os.WIFCONTINUED -> bool |
| 9780 | |
| 9781 | status: int |
| 9782 | |
| 9783 | Return True if a particular process was continued from a job control stop. |
| 9784 | |
| 9785 | Return True if the process returning status was continued from a |
| 9786 | job control stop. |
| 9787 | [clinic start generated code]*/ |
| 9788 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9789 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9790 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 9791 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9792 | { |
| 9793 | WAIT_TYPE wait_status; |
| 9794 | WAIT_STATUS_INT(wait_status) = status; |
| 9795 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9796 | } |
| 9797 | #endif /* WIFCONTINUED */ |
| 9798 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9799 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9800 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9801 | /*[clinic input] |
| 9802 | os.WIFSTOPPED -> bool |
| 9803 | |
| 9804 | status: int |
| 9805 | |
| 9806 | Return True if the process returning status was stopped. |
| 9807 | [clinic start generated code]*/ |
| 9808 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9809 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9810 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 9811 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9812 | { |
| 9813 | WAIT_TYPE wait_status; |
| 9814 | WAIT_STATUS_INT(wait_status) = status; |
| 9815 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9816 | } |
| 9817 | #endif /* WIFSTOPPED */ |
| 9818 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9819 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9820 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9821 | /*[clinic input] |
| 9822 | os.WIFSIGNALED -> bool |
| 9823 | |
| 9824 | status: int |
| 9825 | |
| 9826 | Return True if the process returning status was terminated by a signal. |
| 9827 | [clinic start generated code]*/ |
| 9828 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9829 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9830 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 9831 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9832 | { |
| 9833 | WAIT_TYPE wait_status; |
| 9834 | WAIT_STATUS_INT(wait_status) = status; |
| 9835 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9836 | } |
| 9837 | #endif /* WIFSIGNALED */ |
| 9838 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9839 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9840 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9841 | /*[clinic input] |
| 9842 | os.WIFEXITED -> bool |
| 9843 | |
| 9844 | status: int |
| 9845 | |
| 9846 | Return True if the process returning status exited via the exit() system call. |
| 9847 | [clinic start generated code]*/ |
| 9848 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9849 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9850 | os_WIFEXITED_impl(PyObject *module, int status) |
| 9851 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9852 | { |
| 9853 | WAIT_TYPE wait_status; |
| 9854 | WAIT_STATUS_INT(wait_status) = status; |
| 9855 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9856 | } |
| 9857 | #endif /* WIFEXITED */ |
| 9858 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9859 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9860 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9861 | /*[clinic input] |
| 9862 | os.WEXITSTATUS -> int |
| 9863 | |
| 9864 | status: int |
| 9865 | |
| 9866 | Return the process return code from status. |
| 9867 | [clinic start generated code]*/ |
| 9868 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9869 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9870 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 9871 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9872 | { |
| 9873 | WAIT_TYPE wait_status; |
| 9874 | WAIT_STATUS_INT(wait_status) = status; |
| 9875 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9876 | } |
| 9877 | #endif /* WEXITSTATUS */ |
| 9878 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9879 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9880 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9881 | /*[clinic input] |
| 9882 | os.WTERMSIG -> int |
| 9883 | |
| 9884 | status: int |
| 9885 | |
| 9886 | Return the signal that terminated the process that provided the status value. |
| 9887 | [clinic start generated code]*/ |
| 9888 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9889 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9890 | os_WTERMSIG_impl(PyObject *module, int status) |
| 9891 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9892 | { |
| 9893 | WAIT_TYPE wait_status; |
| 9894 | WAIT_STATUS_INT(wait_status) = status; |
| 9895 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9896 | } |
| 9897 | #endif /* WTERMSIG */ |
| 9898 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9899 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9900 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9901 | /*[clinic input] |
| 9902 | os.WSTOPSIG -> int |
| 9903 | |
| 9904 | status: int |
| 9905 | |
| 9906 | Return the signal that stopped the process that provided the status value. |
| 9907 | [clinic start generated code]*/ |
| 9908 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9909 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9910 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 9911 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9912 | { |
| 9913 | WAIT_TYPE wait_status; |
| 9914 | WAIT_STATUS_INT(wait_status) = status; |
| 9915 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9916 | } |
| 9917 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9918 | #endif /* HAVE_SYS_WAIT_H */ |
| 9919 | |
| 9920 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9921 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9922 | #ifdef _SCO_DS |
| 9923 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9924 | needed definitions in sys/statvfs.h */ |
| 9925 | #define _SVID3 |
| 9926 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9927 | #include <sys/statvfs.h> |
| 9928 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9929 | static PyObject* |
| 9930 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 9931 | PyObject *v = PyStructSequence_New(StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9932 | if (v == NULL) |
| 9933 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9934 | |
| 9935 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9936 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9937 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9938 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9939 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9940 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9941 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9942 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9943 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9944 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9945 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9946 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9947 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9948 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9949 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9950 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9951 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9952 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9953 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9954 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9955 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9956 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9957 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9958 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9959 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9960 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9961 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9962 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9963 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 9964 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 9965 | * (issue #32390). */ |
| 9966 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 9967 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 9968 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 9969 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 9970 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9971 | if (PyErr_Occurred()) { |
| 9972 | Py_DECREF(v); |
| 9973 | return NULL; |
| 9974 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9975 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9976 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9977 | } |
| 9978 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9979 | |
| 9980 | /*[clinic input] |
| 9981 | os.fstatvfs |
| 9982 | fd: int |
| 9983 | / |
| 9984 | |
| 9985 | Perform an fstatvfs system call on the given fd. |
| 9986 | |
| 9987 | Equivalent to statvfs(fd). |
| 9988 | [clinic start generated code]*/ |
| 9989 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9990 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9991 | os_fstatvfs_impl(PyObject *module, int fd) |
| 9992 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9993 | { |
| 9994 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9995 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9996 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9997 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9998 | do { |
| 9999 | Py_BEGIN_ALLOW_THREADS |
| 10000 | result = fstatvfs(fd, &st); |
| 10001 | Py_END_ALLOW_THREADS |
| 10002 | } while (result != 0 && errno == EINTR && |
| 10003 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10004 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10005 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10006 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10007 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10008 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10009 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10010 | |
| 10011 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10012 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10013 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10014 | /*[clinic input] |
| 10015 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10016 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10017 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 10018 | |
| 10019 | Perform a statvfs system call on the given path. |
| 10020 | |
| 10021 | path may always be specified as a string. |
| 10022 | On some platforms, path may also be specified as an open file descriptor. |
| 10023 | If this functionality is unavailable, using it raises an exception. |
| 10024 | [clinic start generated code]*/ |
| 10025 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10026 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10027 | os_statvfs_impl(PyObject *module, path_t *path) |
| 10028 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10029 | { |
| 10030 | int result; |
| 10031 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10032 | |
| 10033 | Py_BEGIN_ALLOW_THREADS |
| 10034 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10035 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10036 | #ifdef __APPLE__ |
| 10037 | /* handle weak-linking on Mac OS X 10.3 */ |
| 10038 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10039 | fd_specified("statvfs", path->fd); |
| 10040 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10041 | } |
| 10042 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10043 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10044 | } |
| 10045 | else |
| 10046 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10047 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10048 | Py_END_ALLOW_THREADS |
| 10049 | |
| 10050 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10051 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10052 | } |
| 10053 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10054 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10055 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10056 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 10057 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10058 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10059 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10060 | /*[clinic input] |
| 10061 | os._getdiskusage |
| 10062 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10063 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10064 | |
| 10065 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 10066 | [clinic start generated code]*/ |
| 10067 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10068 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10069 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 10070 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10071 | { |
| 10072 | BOOL retval; |
| 10073 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10074 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10075 | |
| 10076 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10077 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10078 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10079 | if (retval == 0) { |
| 10080 | if (GetLastError() == ERROR_DIRECTORY) { |
| 10081 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10082 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10083 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 10084 | if (dir_path == NULL) { |
| 10085 | return PyErr_NoMemory(); |
| 10086 | } |
| 10087 | |
| 10088 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 10089 | |
| 10090 | if (_dirnameW(dir_path) != -1) { |
| 10091 | Py_BEGIN_ALLOW_THREADS |
| 10092 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 10093 | Py_END_ALLOW_THREADS |
| 10094 | } |
| 10095 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 10096 | err = GetLastError(); |
| 10097 | PyMem_Free(dir_path); |
| 10098 | if (retval) { |
| 10099 | goto success; |
| 10100 | } |
| 10101 | } |
| 10102 | return PyErr_SetFromWindowsErr(err); |
| 10103 | } |
| 10104 | |
| 10105 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10106 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 10107 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10108 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10109 | |
| 10110 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10111 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 10112 | * It maps strings representing configuration variable names to |
| 10113 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10114 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10115 | * rarely-used constants. There are three separate tables that use |
| 10116 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10117 | * |
| 10118 | * This code is always included, even if none of the interfaces that |
| 10119 | * need it are included. The #if hackery needed to avoid it would be |
| 10120 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10121 | */ |
| 10122 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10123 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10124 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10125 | }; |
| 10126 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10127 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10128 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10129 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10130 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10131 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10132 | int value = _PyLong_AsInt(arg); |
| 10133 | if (value == -1 && PyErr_Occurred()) |
| 10134 | return 0; |
| 10135 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10136 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10137 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10138 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10139 | /* look up the value in the table using a binary search */ |
| 10140 | size_t lo = 0; |
| 10141 | size_t mid; |
| 10142 | size_t hi = tablesize; |
| 10143 | int cmp; |
| 10144 | const char *confname; |
| 10145 | if (!PyUnicode_Check(arg)) { |
| 10146 | PyErr_SetString(PyExc_TypeError, |
| 10147 | "configuration names must be strings or integers"); |
| 10148 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10149 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10150 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10151 | if (confname == NULL) |
| 10152 | return 0; |
| 10153 | while (lo < hi) { |
| 10154 | mid = (lo + hi) / 2; |
| 10155 | cmp = strcmp(confname, table[mid].name); |
| 10156 | if (cmp < 0) |
| 10157 | hi = mid; |
| 10158 | else if (cmp > 0) |
| 10159 | lo = mid + 1; |
| 10160 | else { |
| 10161 | *valuep = table[mid].value; |
| 10162 | return 1; |
| 10163 | } |
| 10164 | } |
| 10165 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 10166 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10167 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10168 | } |
| 10169 | |
| 10170 | |
| 10171 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 10172 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10173 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10174 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10175 | #endif |
| 10176 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10177 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10178 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10179 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10180 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10181 | #endif |
| 10182 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10183 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10184 | #endif |
| 10185 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10186 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10187 | #endif |
| 10188 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10189 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10190 | #endif |
| 10191 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10192 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10193 | #endif |
| 10194 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10195 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10196 | #endif |
| 10197 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10198 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10199 | #endif |
| 10200 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10201 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10202 | #endif |
| 10203 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10204 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10205 | #endif |
| 10206 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10207 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10208 | #endif |
| 10209 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10210 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10211 | #endif |
| 10212 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10213 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10214 | #endif |
| 10215 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10216 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10217 | #endif |
| 10218 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10219 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10220 | #endif |
| 10221 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10222 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10223 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10224 | #ifdef _PC_ACL_ENABLED |
| 10225 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10226 | #endif |
| 10227 | #ifdef _PC_MIN_HOLE_SIZE |
| 10228 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10229 | #endif |
| 10230 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10231 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10232 | #endif |
| 10233 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10234 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10235 | #endif |
| 10236 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10237 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10238 | #endif |
| 10239 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10240 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10241 | #endif |
| 10242 | #ifdef _PC_REC_XFER_ALIGN |
| 10243 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10244 | #endif |
| 10245 | #ifdef _PC_SYMLINK_MAX |
| 10246 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 10247 | #endif |
| 10248 | #ifdef _PC_XATTR_ENABLED |
| 10249 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 10250 | #endif |
| 10251 | #ifdef _PC_XATTR_EXISTS |
| 10252 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 10253 | #endif |
| 10254 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 10255 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 10256 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10257 | }; |
| 10258 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10259 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10260 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10261 | { |
| 10262 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 10263 | sizeof(posix_constants_pathconf) |
| 10264 | / sizeof(struct constdef)); |
| 10265 | } |
| 10266 | #endif |
| 10267 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10268 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10269 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10270 | /*[clinic input] |
| 10271 | os.fpathconf -> long |
| 10272 | |
| 10273 | fd: int |
| 10274 | name: path_confname |
| 10275 | / |
| 10276 | |
| 10277 | Return the configuration limit name for the file descriptor fd. |
| 10278 | |
| 10279 | If there is no limit, return -1. |
| 10280 | [clinic start generated code]*/ |
| 10281 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10282 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10283 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 10284 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10285 | { |
| 10286 | long limit; |
| 10287 | |
| 10288 | errno = 0; |
| 10289 | limit = fpathconf(fd, name); |
| 10290 | if (limit == -1 && errno != 0) |
| 10291 | posix_error(); |
| 10292 | |
| 10293 | return limit; |
| 10294 | } |
| 10295 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10296 | |
| 10297 | |
| 10298 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10299 | /*[clinic input] |
| 10300 | os.pathconf -> long |
| 10301 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 10302 | name: path_confname |
| 10303 | |
| 10304 | Return the configuration limit name for the file or directory path. |
| 10305 | |
| 10306 | If there is no limit, return -1. |
| 10307 | On some platforms, path may also be specified as an open file descriptor. |
| 10308 | If this functionality is unavailable, using it raises an exception. |
| 10309 | [clinic start generated code]*/ |
| 10310 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10311 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10312 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 10313 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10314 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10315 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10316 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10317 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10318 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10319 | if (path->fd != -1) |
| 10320 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10321 | else |
| 10322 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10323 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10324 | if (limit == -1 && errno != 0) { |
| 10325 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 10326 | /* could be a path or name problem */ |
| 10327 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10328 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10329 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10330 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10331 | |
| 10332 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10333 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10334 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10335 | |
| 10336 | #ifdef HAVE_CONFSTR |
| 10337 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10338 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10339 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10340 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10341 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10342 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10343 | #endif |
| 10344 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10345 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10346 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10347 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10348 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10349 | #endif |
| 10350 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10351 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10352 | #endif |
| 10353 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10354 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10355 | #endif |
| 10356 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10357 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10358 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10359 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10360 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10361 | #endif |
| 10362 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10363 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10364 | #endif |
| 10365 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10366 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10367 | #endif |
| 10368 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10369 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10370 | #endif |
| 10371 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10372 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10373 | #endif |
| 10374 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10375 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10376 | #endif |
| 10377 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10378 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10379 | #endif |
| 10380 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10381 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10382 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10383 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10384 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10385 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10386 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10387 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10388 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10389 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10390 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10391 | #endif |
| 10392 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10393 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10394 | #endif |
| 10395 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10396 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10397 | #endif |
| 10398 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10399 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10400 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10401 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10402 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10403 | #endif |
| 10404 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10405 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10406 | #endif |
| 10407 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10408 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10409 | #endif |
| 10410 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10411 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10412 | #endif |
| 10413 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10414 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10415 | #endif |
| 10416 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10417 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10418 | #endif |
| 10419 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10420 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10421 | #endif |
| 10422 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10423 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10424 | #endif |
| 10425 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10426 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10427 | #endif |
| 10428 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10429 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10430 | #endif |
| 10431 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10432 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10433 | #endif |
| 10434 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10435 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10436 | #endif |
| 10437 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10438 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10439 | #endif |
| 10440 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10441 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10442 | #endif |
| 10443 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10444 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10445 | #endif |
| 10446 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10447 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10448 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10449 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10450 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10451 | #endif |
| 10452 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10453 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10454 | #endif |
| 10455 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10456 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10457 | #endif |
| 10458 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10459 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10460 | #endif |
| 10461 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10462 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10463 | #endif |
| 10464 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10465 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10466 | #endif |
| 10467 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10468 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10469 | #endif |
| 10470 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10471 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10472 | #endif |
| 10473 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10474 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10475 | #endif |
| 10476 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10477 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10478 | #endif |
| 10479 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10480 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10481 | #endif |
| 10482 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10483 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10484 | #endif |
| 10485 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10486 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
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 | }; |
| 10489 | |
| 10490 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10491 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10492 | { |
| 10493 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 10494 | sizeof(posix_constants_confstr) |
| 10495 | / sizeof(struct constdef)); |
| 10496 | } |
| 10497 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10498 | |
| 10499 | /*[clinic input] |
| 10500 | os.confstr |
| 10501 | |
| 10502 | name: confstr_confname |
| 10503 | / |
| 10504 | |
| 10505 | Return a string-valued system configuration variable. |
| 10506 | [clinic start generated code]*/ |
| 10507 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10508 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10509 | os_confstr_impl(PyObject *module, int name) |
| 10510 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10511 | { |
| 10512 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10513 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10514 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10515 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10516 | errno = 0; |
| 10517 | len = confstr(name, buffer, sizeof(buffer)); |
| 10518 | if (len == 0) { |
| 10519 | if (errno) { |
| 10520 | posix_error(); |
| 10521 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10522 | } |
| 10523 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10524 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10525 | } |
| 10526 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10527 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10528 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 10529 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10530 | char *buf = PyMem_Malloc(len); |
| 10531 | if (buf == NULL) |
| 10532 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 10533 | len2 = confstr(name, buf, len); |
| 10534 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 10535 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10536 | PyMem_Free(buf); |
| 10537 | } |
| 10538 | else |
| 10539 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10540 | return result; |
| 10541 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10542 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10543 | |
| 10544 | |
| 10545 | #ifdef HAVE_SYSCONF |
| 10546 | static struct constdef posix_constants_sysconf[] = { |
| 10547 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10548 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10549 | #endif |
| 10550 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10551 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10552 | #endif |
| 10553 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10554 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10555 | #endif |
| 10556 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10557 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10558 | #endif |
| 10559 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10560 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10561 | #endif |
| 10562 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10563 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10564 | #endif |
| 10565 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10566 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10567 | #endif |
| 10568 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10569 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10570 | #endif |
| 10571 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10572 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10573 | #endif |
| 10574 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10575 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10576 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10577 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10578 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10579 | #endif |
| 10580 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10581 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10582 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10583 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10584 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10585 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10586 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10587 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10588 | #endif |
| 10589 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10590 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10591 | #endif |
| 10592 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10593 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10594 | #endif |
| 10595 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10596 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10597 | #endif |
| 10598 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10599 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10600 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10601 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10602 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10603 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10604 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10605 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10606 | #endif |
| 10607 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10608 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10609 | #endif |
| 10610 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10611 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10612 | #endif |
| 10613 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10614 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10615 | #endif |
| 10616 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10617 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10618 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10619 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10620 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10621 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10622 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10623 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10624 | #endif |
| 10625 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10626 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10627 | #endif |
| 10628 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10629 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10630 | #endif |
| 10631 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10632 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10633 | #endif |
| 10634 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10635 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10636 | #endif |
| 10637 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10638 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10639 | #endif |
| 10640 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10641 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10642 | #endif |
| 10643 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10644 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10645 | #endif |
| 10646 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10647 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10648 | #endif |
| 10649 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10650 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10651 | #endif |
| 10652 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10653 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10654 | #endif |
| 10655 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10656 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10657 | #endif |
| 10658 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10659 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10660 | #endif |
| 10661 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10662 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10663 | #endif |
| 10664 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10665 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10666 | #endif |
| 10667 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10668 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10669 | #endif |
| 10670 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10671 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10672 | #endif |
| 10673 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10674 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10675 | #endif |
| 10676 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10677 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10678 | #endif |
| 10679 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10680 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10681 | #endif |
| 10682 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10683 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10684 | #endif |
| 10685 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10686 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10687 | #endif |
| 10688 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10689 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10690 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10691 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10692 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10693 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10694 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10695 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10696 | #endif |
| 10697 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10698 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10699 | #endif |
| 10700 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10701 | {"SC_IOV_MAX", _SC_IOV_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_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10704 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
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_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10707 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10708 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10709 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10710 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10711 | #endif |
| 10712 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10713 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10714 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10715 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10716 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10717 | #endif |
| 10718 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10719 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10720 | #endif |
| 10721 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10722 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10723 | #endif |
| 10724 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10725 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10726 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10727 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10728 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10729 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10730 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10731 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10732 | #endif |
| 10733 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10734 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10735 | #endif |
| 10736 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10737 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10738 | #endif |
| 10739 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10740 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10741 | #endif |
| 10742 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10743 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10744 | #endif |
| 10745 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10746 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10747 | #endif |
| 10748 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10749 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10750 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10751 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10752 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10753 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10754 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10755 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10756 | #endif |
| 10757 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10758 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10759 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10760 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10761 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10762 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10763 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10764 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10765 | #endif |
| 10766 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10767 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10768 | #endif |
| 10769 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10770 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10771 | #endif |
| 10772 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10773 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10774 | #endif |
| 10775 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10776 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10777 | #endif |
| 10778 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10779 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10780 | #endif |
| 10781 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10782 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10783 | #endif |
| 10784 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10785 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10786 | #endif |
| 10787 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10788 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10789 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10790 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10791 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10792 | #endif |
| 10793 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10794 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
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_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10797 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10798 | #endif |
| 10799 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10800 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10801 | #endif |
| 10802 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10803 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10804 | #endif |
| 10805 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10806 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10807 | #endif |
| 10808 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10809 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10810 | #endif |
| 10811 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10812 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10813 | #endif |
| 10814 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10815 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10816 | #endif |
| 10817 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10818 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10819 | #endif |
| 10820 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10821 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10822 | #endif |
| 10823 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10824 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10825 | #endif |
| 10826 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10827 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10828 | #endif |
| 10829 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10830 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10831 | #endif |
| 10832 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10833 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10834 | #endif |
| 10835 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10836 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10837 | #endif |
| 10838 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10839 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10840 | #endif |
| 10841 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10842 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10843 | #endif |
| 10844 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10845 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10846 | #endif |
| 10847 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10848 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10849 | #endif |
| 10850 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10851 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10852 | #endif |
| 10853 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10854 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10855 | #endif |
| 10856 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10857 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10858 | #endif |
| 10859 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10860 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10861 | #endif |
| 10862 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10863 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10864 | #endif |
| 10865 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10866 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10867 | #endif |
| 10868 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10869 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10870 | #endif |
| 10871 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10872 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10873 | #endif |
| 10874 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10875 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10876 | #endif |
| 10877 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10878 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10879 | #endif |
| 10880 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10881 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10882 | #endif |
| 10883 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10884 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10885 | #endif |
| 10886 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10887 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10888 | #endif |
| 10889 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10890 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10891 | #endif |
| 10892 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10893 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10894 | #endif |
| 10895 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10896 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10897 | #endif |
| 10898 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10899 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10900 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10901 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10902 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10903 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10904 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10905 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10906 | #endif |
| 10907 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10908 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10909 | #endif |
| 10910 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10911 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10912 | #endif |
| 10913 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10914 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10915 | #endif |
| 10916 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10917 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10918 | #endif |
| 10919 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10920 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10921 | #endif |
| 10922 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10923 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10924 | #endif |
| 10925 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10926 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10927 | #endif |
| 10928 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10929 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10930 | #endif |
| 10931 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10932 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10933 | #endif |
| 10934 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10935 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10936 | #endif |
| 10937 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10938 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10939 | #endif |
| 10940 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10941 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10942 | #endif |
| 10943 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10944 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10945 | #endif |
| 10946 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10947 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10948 | #endif |
| 10949 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10950 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10951 | #endif |
| 10952 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10953 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10954 | #endif |
| 10955 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10956 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10957 | #endif |
| 10958 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10959 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10960 | #endif |
| 10961 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10962 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10963 | #endif |
| 10964 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10965 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10966 | #endif |
| 10967 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10968 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10969 | #endif |
| 10970 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10971 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10972 | #endif |
| 10973 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10974 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10975 | #endif |
| 10976 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10977 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10978 | #endif |
| 10979 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10980 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10981 | #endif |
| 10982 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10983 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10984 | #endif |
| 10985 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10986 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10987 | #endif |
| 10988 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10989 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10990 | #endif |
| 10991 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10992 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10993 | #endif |
| 10994 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10995 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10996 | #endif |
| 10997 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10998 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10999 | #endif |
| 11000 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11001 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11002 | #endif |
| 11003 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11004 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11005 | #endif |
| 11006 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11007 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11008 | #endif |
| 11009 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11010 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11011 | #endif |
| 11012 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11013 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11014 | #endif |
| 11015 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11016 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11017 | #endif |
| 11018 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11019 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11020 | #endif |
| 11021 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11022 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11023 | #endif |
| 11024 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11025 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11026 | #endif |
| 11027 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11028 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11029 | #endif |
| 11030 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11031 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11032 | #endif |
| 11033 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11034 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11035 | #endif |
| 11036 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11037 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11038 | #endif |
| 11039 | }; |
| 11040 | |
| 11041 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11042 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11043 | { |
| 11044 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 11045 | sizeof(posix_constants_sysconf) |
| 11046 | / sizeof(struct constdef)); |
| 11047 | } |
| 11048 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11049 | |
| 11050 | /*[clinic input] |
| 11051 | os.sysconf -> long |
| 11052 | name: sysconf_confname |
| 11053 | / |
| 11054 | |
| 11055 | Return an integer-valued system configuration variable. |
| 11056 | [clinic start generated code]*/ |
| 11057 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11058 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11059 | os_sysconf_impl(PyObject *module, int name) |
| 11060 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11061 | { |
| 11062 | long value; |
| 11063 | |
| 11064 | errno = 0; |
| 11065 | value = sysconf(name); |
| 11066 | if (value == -1 && errno != 0) |
| 11067 | posix_error(); |
| 11068 | return value; |
| 11069 | } |
| 11070 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11071 | |
| 11072 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11073 | /* 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] | 11074 | * 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] | 11075 | * the exported dictionaries that are used to publish information about the |
| 11076 | * names available on the host platform. |
| 11077 | * |
| 11078 | * Sorting the table at runtime ensures that the table is properly ordered |
| 11079 | * when used, even for platforms we're not able to test on. It also makes |
| 11080 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11081 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11082 | |
| 11083 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11084 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11085 | { |
| 11086 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11087 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11088 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11089 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11090 | |
| 11091 | return strcmp(c1->name, c2->name); |
| 11092 | } |
| 11093 | |
| 11094 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11095 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11096 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11097 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11098 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11099 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11100 | |
| 11101 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 11102 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11103 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11104 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11105 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11106 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11107 | PyObject *o = PyLong_FromLong(table[i].value); |
| 11108 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 11109 | Py_XDECREF(o); |
| 11110 | Py_DECREF(d); |
| 11111 | return -1; |
| 11112 | } |
| 11113 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11114 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11115 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11116 | } |
| 11117 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11118 | /* Return -1 on failure, 0 on success. */ |
| 11119 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11120 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11121 | { |
| 11122 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11123 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11124 | sizeof(posix_constants_pathconf) |
| 11125 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11126 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11127 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11128 | #endif |
| 11129 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11130 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11131 | sizeof(posix_constants_confstr) |
| 11132 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11133 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11134 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11135 | #endif |
| 11136 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11137 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11138 | sizeof(posix_constants_sysconf) |
| 11139 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11140 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11141 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11142 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11143 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11144 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11145 | |
| 11146 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11147 | /*[clinic input] |
| 11148 | os.abort |
| 11149 | |
| 11150 | Abort the interpreter immediately. |
| 11151 | |
| 11152 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 11153 | on the hosting operating system. This function never returns. |
| 11154 | [clinic start generated code]*/ |
| 11155 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11156 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11157 | os_abort_impl(PyObject *module) |
| 11158 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11159 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11160 | abort(); |
| 11161 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11162 | #ifndef __clang__ |
| 11163 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 11164 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 11165 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11166 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 11167 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11168 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11169 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11170 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11171 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11172 | /* Grab ShellExecute dynamically from shell32 */ |
| 11173 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11174 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 11175 | LPCWSTR, INT); |
| 11176 | static int |
| 11177 | check_ShellExecute() |
| 11178 | { |
| 11179 | HINSTANCE hShell32; |
| 11180 | |
| 11181 | /* only recheck */ |
| 11182 | if (-1 == has_ShellExecute) { |
| 11183 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11184 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 11185 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 11186 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 11187 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11188 | hShell32 = LoadLibraryW(L"SHELL32"); |
| 11189 | Py_END_ALLOW_THREADS |
| 11190 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11191 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 11192 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11193 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11194 | } else { |
| 11195 | has_ShellExecute = 0; |
| 11196 | } |
| 11197 | } |
| 11198 | return has_ShellExecute; |
| 11199 | } |
| 11200 | |
| 11201 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11202 | /*[clinic input] |
| 11203 | os.startfile |
| 11204 | filepath: path_t |
| 11205 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11206 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11207 | startfile(filepath [, operation]) |
| 11208 | |
| 11209 | Start a file with its associated application. |
| 11210 | |
| 11211 | When "operation" is not specified or "open", this acts like |
| 11212 | double-clicking the file in Explorer, or giving the file name as an |
| 11213 | argument to the DOS "start" command: the file is opened with whatever |
| 11214 | application (if any) its extension is associated. |
| 11215 | When another "operation" is given, it specifies what should be done with |
| 11216 | the file. A typical operation is "print". |
| 11217 | |
| 11218 | startfile returns as soon as the associated application is launched. |
| 11219 | There is no option to wait for the application to close, and no way |
| 11220 | to retrieve the application's exit status. |
| 11221 | |
| 11222 | The filepath is relative to the current directory. If you want to use |
| 11223 | an absolute path, make sure the first character is not a slash ("/"); |
| 11224 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 11225 | [clinic start generated code]*/ |
| 11226 | |
| 11227 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11228 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 11229 | const Py_UNICODE *operation) |
| 11230 | /*[clinic end generated code: output=66dc311c94d50797 input=63950bf2986380d0]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11231 | { |
| 11232 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11233 | |
| 11234 | if(!check_ShellExecute()) { |
| 11235 | /* If the OS doesn't have ShellExecute, return a |
| 11236 | NotImplementedError. */ |
| 11237 | return PyErr_Format(PyExc_NotImplementedError, |
| 11238 | "startfile not available on this platform"); |
| 11239 | } |
| 11240 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11241 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11242 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11243 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11244 | Py_END_ALLOW_THREADS |
| 11245 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11246 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11247 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11248 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11249 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11250 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11251 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11252 | #endif /* MS_WINDOWS */ |
| 11253 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11254 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11255 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11256 | /*[clinic input] |
| 11257 | os.getloadavg |
| 11258 | |
| 11259 | Return average recent system load information. |
| 11260 | |
| 11261 | Return the number of processes in the system run queue averaged over |
| 11262 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 11263 | Raises OSError if the load average was unobtainable. |
| 11264 | [clinic start generated code]*/ |
| 11265 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11266 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11267 | os_getloadavg_impl(PyObject *module) |
| 11268 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11269 | { |
| 11270 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11271 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11272 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 11273 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11274 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11275 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11276 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11277 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11278 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11279 | |
| 11280 | /*[clinic input] |
| 11281 | os.device_encoding |
| 11282 | fd: int |
| 11283 | |
| 11284 | Return a string describing the encoding of a terminal's file descriptor. |
| 11285 | |
| 11286 | The file descriptor must be attached to a terminal. |
| 11287 | If the device is not a terminal, return None. |
| 11288 | [clinic start generated code]*/ |
| 11289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11290 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11291 | os_device_encoding_impl(PyObject *module, int fd) |
| 11292 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11293 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 11294 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 11295 | } |
| 11296 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11297 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11298 | #ifdef HAVE_SETRESUID |
| 11299 | /*[clinic input] |
| 11300 | os.setresuid |
| 11301 | |
| 11302 | ruid: uid_t |
| 11303 | euid: uid_t |
| 11304 | suid: uid_t |
| 11305 | / |
| 11306 | |
| 11307 | Set the current process's real, effective, and saved user ids. |
| 11308 | [clinic start generated code]*/ |
| 11309 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11310 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11311 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 11312 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11313 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11314 | if (setresuid(ruid, euid, suid) < 0) |
| 11315 | return posix_error(); |
| 11316 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11317 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11318 | #endif /* HAVE_SETRESUID */ |
| 11319 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11320 | |
| 11321 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11322 | /*[clinic input] |
| 11323 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11324 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11325 | rgid: gid_t |
| 11326 | egid: gid_t |
| 11327 | sgid: gid_t |
| 11328 | / |
| 11329 | |
| 11330 | Set the current process's real, effective, and saved group ids. |
| 11331 | [clinic start generated code]*/ |
| 11332 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11333 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11334 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 11335 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11336 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11337 | if (setresgid(rgid, egid, sgid) < 0) |
| 11338 | return posix_error(); |
| 11339 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11340 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11341 | #endif /* HAVE_SETRESGID */ |
| 11342 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11343 | |
| 11344 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11345 | /*[clinic input] |
| 11346 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11347 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11348 | Return a tuple of the current process's real, effective, and saved user ids. |
| 11349 | [clinic start generated code]*/ |
| 11350 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11351 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11352 | os_getresuid_impl(PyObject *module) |
| 11353 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11354 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11355 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11356 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 11357 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11358 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 11359 | _PyLong_FromUid(euid), |
| 11360 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11361 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11362 | #endif /* HAVE_GETRESUID */ |
| 11363 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11364 | |
| 11365 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11366 | /*[clinic input] |
| 11367 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11368 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11369 | Return a tuple of the current process's real, effective, and saved group ids. |
| 11370 | [clinic start generated code]*/ |
| 11371 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11372 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11373 | os_getresgid_impl(PyObject *module) |
| 11374 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11375 | { |
| 11376 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11377 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 11378 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11379 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 11380 | _PyLong_FromGid(egid), |
| 11381 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11382 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11383 | #endif /* HAVE_GETRESGID */ |
| 11384 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11385 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11386 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11387 | /*[clinic input] |
| 11388 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11389 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11390 | path: path_t(allow_fd=True) |
| 11391 | attribute: path_t |
| 11392 | * |
| 11393 | follow_symlinks: bool = True |
| 11394 | |
| 11395 | Return the value of extended attribute attribute on path. |
| 11396 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11397 | 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] | 11398 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11399 | link, getxattr will examine the symbolic link itself instead of the file |
| 11400 | the link points to. |
| 11401 | |
| 11402 | [clinic start generated code]*/ |
| 11403 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11404 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11405 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11406 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11407 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11408 | { |
| 11409 | Py_ssize_t i; |
| 11410 | PyObject *buffer = NULL; |
| 11411 | |
| 11412 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 11413 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11414 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11415 | for (i = 0; ; i++) { |
| 11416 | void *ptr; |
| 11417 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11418 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11419 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11420 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11421 | path_error(path); |
| 11422 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11423 | } |
| 11424 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 11425 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11426 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11427 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11428 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11429 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11430 | if (path->fd >= 0) |
| 11431 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11432 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11433 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11434 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11435 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11436 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11437 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11438 | if (result < 0) { |
| 11439 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11440 | if (errno == ERANGE) |
| 11441 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11442 | path_error(path); |
| 11443 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11444 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11445 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11446 | if (result != buffer_size) { |
| 11447 | /* Can only shrink. */ |
| 11448 | _PyBytes_Resize(&buffer, result); |
| 11449 | } |
| 11450 | break; |
| 11451 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11452 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11453 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11454 | } |
| 11455 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11456 | |
| 11457 | /*[clinic input] |
| 11458 | os.setxattr |
| 11459 | |
| 11460 | path: path_t(allow_fd=True) |
| 11461 | attribute: path_t |
| 11462 | value: Py_buffer |
| 11463 | flags: int = 0 |
| 11464 | * |
| 11465 | follow_symlinks: bool = True |
| 11466 | |
| 11467 | Set extended attribute attribute on path to value. |
| 11468 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11469 | 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] | 11470 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11471 | link, setxattr will modify the symbolic link itself instead of the file |
| 11472 | the link points to. |
| 11473 | |
| 11474 | [clinic start generated code]*/ |
| 11475 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11476 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11477 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11478 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11479 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11480 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11481 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11482 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11483 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11484 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11485 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11486 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11487 | if (path->fd > -1) |
| 11488 | result = fsetxattr(path->fd, attribute->narrow, |
| 11489 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11490 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11491 | result = setxattr(path->narrow, attribute->narrow, |
| 11492 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11493 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11494 | result = lsetxattr(path->narrow, attribute->narrow, |
| 11495 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11496 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11497 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11498 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11499 | path_error(path); |
| 11500 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11501 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11502 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11503 | Py_RETURN_NONE; |
| 11504 | } |
| 11505 | |
| 11506 | |
| 11507 | /*[clinic input] |
| 11508 | os.removexattr |
| 11509 | |
| 11510 | path: path_t(allow_fd=True) |
| 11511 | attribute: path_t |
| 11512 | * |
| 11513 | follow_symlinks: bool = True |
| 11514 | |
| 11515 | Remove extended attribute attribute on path. |
| 11516 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11517 | 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] | 11518 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11519 | link, removexattr will modify the symbolic link itself instead of the file |
| 11520 | the link points to. |
| 11521 | |
| 11522 | [clinic start generated code]*/ |
| 11523 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11524 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11525 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11526 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11527 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11528 | { |
| 11529 | ssize_t result; |
| 11530 | |
| 11531 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 11532 | return NULL; |
| 11533 | |
| 11534 | Py_BEGIN_ALLOW_THREADS; |
| 11535 | if (path->fd > -1) |
| 11536 | result = fremovexattr(path->fd, attribute->narrow); |
| 11537 | else if (follow_symlinks) |
| 11538 | result = removexattr(path->narrow, attribute->narrow); |
| 11539 | else |
| 11540 | result = lremovexattr(path->narrow, attribute->narrow); |
| 11541 | Py_END_ALLOW_THREADS; |
| 11542 | |
| 11543 | if (result) { |
| 11544 | return path_error(path); |
| 11545 | } |
| 11546 | |
| 11547 | Py_RETURN_NONE; |
| 11548 | } |
| 11549 | |
| 11550 | |
| 11551 | /*[clinic input] |
| 11552 | os.listxattr |
| 11553 | |
| 11554 | path: path_t(allow_fd=True, nullable=True) = None |
| 11555 | * |
| 11556 | follow_symlinks: bool = True |
| 11557 | |
| 11558 | Return a list of extended attributes on path. |
| 11559 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11560 | 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] | 11561 | if path is None, listxattr will examine the current directory. |
| 11562 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11563 | link, listxattr will examine the symbolic link itself instead of the file |
| 11564 | the link points to. |
| 11565 | [clinic start generated code]*/ |
| 11566 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11567 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11568 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11569 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11570 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11571 | Py_ssize_t i; |
| 11572 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11573 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11574 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11575 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11576 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11577 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11578 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11579 | name = path->narrow ? path->narrow : "."; |
| 11580 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11581 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11582 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11583 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11584 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11585 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11586 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 11587 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11588 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11589 | break; |
| 11590 | } |
| 11591 | buffer = PyMem_MALLOC(buffer_size); |
| 11592 | if (!buffer) { |
| 11593 | PyErr_NoMemory(); |
| 11594 | break; |
| 11595 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11596 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11597 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11598 | if (path->fd > -1) |
| 11599 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11600 | else if (follow_symlinks) |
| 11601 | length = listxattr(name, buffer, buffer_size); |
| 11602 | else |
| 11603 | length = llistxattr(name, buffer, buffer_size); |
| 11604 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11605 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11606 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11607 | if (errno == ERANGE) { |
| 11608 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 11609 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11610 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11611 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11612 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11613 | break; |
| 11614 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11615 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11616 | result = PyList_New(0); |
| 11617 | if (!result) { |
| 11618 | goto exit; |
| 11619 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11620 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11621 | end = buffer + length; |
| 11622 | for (trace = start = buffer; trace != end; trace++) { |
| 11623 | if (!*trace) { |
| 11624 | int error; |
| 11625 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 11626 | trace - start); |
| 11627 | if (!attribute) { |
| 11628 | Py_DECREF(result); |
| 11629 | result = NULL; |
| 11630 | goto exit; |
| 11631 | } |
| 11632 | error = PyList_Append(result, attribute); |
| 11633 | Py_DECREF(attribute); |
| 11634 | if (error) { |
| 11635 | Py_DECREF(result); |
| 11636 | result = NULL; |
| 11637 | goto exit; |
| 11638 | } |
| 11639 | start = trace + 1; |
| 11640 | } |
| 11641 | } |
| 11642 | break; |
| 11643 | } |
| 11644 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11645 | if (buffer) |
| 11646 | PyMem_FREE(buffer); |
| 11647 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11648 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11649 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11650 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11651 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11652 | /*[clinic input] |
| 11653 | os.urandom |
| 11654 | |
| 11655 | size: Py_ssize_t |
| 11656 | / |
| 11657 | |
| 11658 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 11659 | [clinic start generated code]*/ |
| 11660 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11661 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11662 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 11663 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11664 | { |
| 11665 | PyObject *bytes; |
| 11666 | int result; |
| 11667 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11668 | if (size < 0) |
| 11669 | return PyErr_Format(PyExc_ValueError, |
| 11670 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11671 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 11672 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11673 | return NULL; |
| 11674 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 11675 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11676 | if (result == -1) { |
| 11677 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11678 | return NULL; |
| 11679 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11680 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11681 | } |
| 11682 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11683 | /* Terminal size querying */ |
| 11684 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 11685 | static PyTypeObject* TerminalSizeType; |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11686 | |
| 11687 | PyDoc_STRVAR(TerminalSize_docstring, |
| 11688 | "A tuple of (columns, lines) for holding terminal window size"); |
| 11689 | |
| 11690 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 11691 | {"columns", "width of the terminal window in characters"}, |
| 11692 | {"lines", "height of the terminal window in characters"}, |
| 11693 | {NULL, NULL} |
| 11694 | }; |
| 11695 | |
| 11696 | static PyStructSequence_Desc TerminalSize_desc = { |
| 11697 | "os.terminal_size", |
| 11698 | TerminalSize_docstring, |
| 11699 | TerminalSize_fields, |
| 11700 | 2, |
| 11701 | }; |
| 11702 | |
| 11703 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11704 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11705 | PyDoc_STRVAR(termsize__doc__, |
| 11706 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 11707 | "\n" \ |
| 11708 | "The optional argument fd (default standard output) specifies\n" \ |
| 11709 | "which file descriptor should be queried.\n" \ |
| 11710 | "\n" \ |
| 11711 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 11712 | "is thrown.\n" \ |
| 11713 | "\n" \ |
| 11714 | "This function will only be defined if an implementation is\n" \ |
| 11715 | "available for this system.\n" \ |
| 11716 | "\n" \ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 11717 | "shutil.get_terminal_size is the high-level function which should\n" \ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11718 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 11719 | |
| 11720 | static PyObject* |
| 11721 | get_terminal_size(PyObject *self, PyObject *args) |
| 11722 | { |
| 11723 | int columns, lines; |
| 11724 | PyObject *termsize; |
| 11725 | |
| 11726 | int fd = fileno(stdout); |
| 11727 | /* Under some conditions stdout may not be connected and |
| 11728 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 11729 | * GUI apps don't have valid standard streams by default. |
| 11730 | * |
| 11731 | * If this happens, and the optional fd argument is not present, |
| 11732 | * the ioctl below will fail returning EBADF. This is what we want. |
| 11733 | */ |
| 11734 | |
| 11735 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 11736 | return NULL; |
| 11737 | |
| 11738 | #ifdef TERMSIZE_USE_IOCTL |
| 11739 | { |
| 11740 | struct winsize w; |
| 11741 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 11742 | return PyErr_SetFromErrno(PyExc_OSError); |
| 11743 | columns = w.ws_col; |
| 11744 | lines = w.ws_row; |
| 11745 | } |
| 11746 | #endif /* TERMSIZE_USE_IOCTL */ |
| 11747 | |
| 11748 | #ifdef TERMSIZE_USE_CONIO |
| 11749 | { |
| 11750 | DWORD nhandle; |
| 11751 | HANDLE handle; |
| 11752 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 11753 | switch (fd) { |
| 11754 | case 0: nhandle = STD_INPUT_HANDLE; |
| 11755 | break; |
| 11756 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 11757 | break; |
| 11758 | case 2: nhandle = STD_ERROR_HANDLE; |
| 11759 | break; |
| 11760 | default: |
| 11761 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 11762 | } |
| 11763 | handle = GetStdHandle(nhandle); |
| 11764 | if (handle == NULL) |
| 11765 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 11766 | if (handle == INVALID_HANDLE_VALUE) |
| 11767 | return PyErr_SetFromWindowsErr(0); |
| 11768 | |
| 11769 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 11770 | return PyErr_SetFromWindowsErr(0); |
| 11771 | |
| 11772 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 11773 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 11774 | } |
| 11775 | #endif /* TERMSIZE_USE_CONIO */ |
| 11776 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 11777 | termsize = PyStructSequence_New(TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11778 | if (termsize == NULL) |
| 11779 | return NULL; |
| 11780 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 11781 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 11782 | if (PyErr_Occurred()) { |
| 11783 | Py_DECREF(termsize); |
| 11784 | return NULL; |
| 11785 | } |
| 11786 | return termsize; |
| 11787 | } |
| 11788 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 11789 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11790 | |
| 11791 | /*[clinic input] |
| 11792 | os.cpu_count |
| 11793 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 11794 | Return the number of CPUs in the system; return None if indeterminable. |
| 11795 | |
| 11796 | This number is not equivalent to the number of CPUs the current process can |
| 11797 | use. The number of usable CPUs can be obtained with |
| 11798 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11799 | [clinic start generated code]*/ |
| 11800 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11801 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11802 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 11803 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11804 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11805 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11806 | #ifdef MS_WINDOWS |
Christopher Wilcox | c67bae0 | 2017-08-30 05:01:08 -0400 | [diff] [blame] | 11807 | /* Vista is supported and the GetMaximumProcessorCount API is Win7+ |
| 11808 | Need to fallback to Vista behavior if this call isn't present */ |
| 11809 | HINSTANCE hKernel32; |
| 11810 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 11811 | |
| 11812 | static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL; |
| 11813 | *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32, |
| 11814 | "GetMaximumProcessorCount"); |
| 11815 | if (_GetMaximumProcessorCount != NULL) { |
| 11816 | ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS); |
| 11817 | } |
| 11818 | else { |
| 11819 | SYSTEM_INFO sysinfo; |
| 11820 | GetSystemInfo(&sysinfo); |
| 11821 | ncpu = sysinfo.dwNumberOfProcessors; |
| 11822 | } |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11823 | #elif defined(__hpux) |
| 11824 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 11825 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 11826 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11827 | #elif defined(__DragonFly__) || \ |
| 11828 | defined(__OpenBSD__) || \ |
| 11829 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11830 | defined(__NetBSD__) || \ |
| 11831 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 11832 | int mib[2]; |
| 11833 | size_t len = sizeof(ncpu); |
| 11834 | mib[0] = CTL_HW; |
| 11835 | mib[1] = HW_NCPU; |
| 11836 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 11837 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11838 | #endif |
| 11839 | if (ncpu >= 1) |
| 11840 | return PyLong_FromLong(ncpu); |
| 11841 | else |
| 11842 | Py_RETURN_NONE; |
| 11843 | } |
| 11844 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11845 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11846 | /*[clinic input] |
| 11847 | os.get_inheritable -> bool |
| 11848 | |
| 11849 | fd: int |
| 11850 | / |
| 11851 | |
| 11852 | Get the close-on-exe flag of the specified file descriptor. |
| 11853 | [clinic start generated code]*/ |
| 11854 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11855 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11856 | os_get_inheritable_impl(PyObject *module, int fd) |
| 11857 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11858 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11859 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11860 | _Py_BEGIN_SUPPRESS_IPH |
| 11861 | return_value = _Py_get_inheritable(fd); |
| 11862 | _Py_END_SUPPRESS_IPH |
| 11863 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11864 | } |
| 11865 | |
| 11866 | |
| 11867 | /*[clinic input] |
| 11868 | os.set_inheritable |
| 11869 | fd: int |
| 11870 | inheritable: int |
| 11871 | / |
| 11872 | |
| 11873 | Set the inheritable flag of the specified file descriptor. |
| 11874 | [clinic start generated code]*/ |
| 11875 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11876 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11877 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 11878 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11879 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11880 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11881 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11882 | _Py_BEGIN_SUPPRESS_IPH |
| 11883 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 11884 | _Py_END_SUPPRESS_IPH |
| 11885 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11886 | return NULL; |
| 11887 | Py_RETURN_NONE; |
| 11888 | } |
| 11889 | |
| 11890 | |
| 11891 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11892 | /*[clinic input] |
| 11893 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11894 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11895 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11896 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11897 | Get the close-on-exe flag of the specified file descriptor. |
| 11898 | [clinic start generated code]*/ |
| 11899 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11900 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11901 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 11902 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11903 | { |
| 11904 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11905 | |
| 11906 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 11907 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11908 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11909 | } |
| 11910 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11911 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11912 | } |
| 11913 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11914 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11915 | /*[clinic input] |
| 11916 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11917 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11918 | inheritable: bool |
| 11919 | / |
| 11920 | |
| 11921 | Set the inheritable flag of the specified handle. |
| 11922 | [clinic start generated code]*/ |
| 11923 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11924 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11925 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11926 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 11927 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11928 | { |
| 11929 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11930 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 11931 | PyErr_SetFromWindowsErr(0); |
| 11932 | return NULL; |
| 11933 | } |
| 11934 | Py_RETURN_NONE; |
| 11935 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11936 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11937 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11938 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 11939 | /*[clinic input] |
| 11940 | os.get_blocking -> bool |
| 11941 | fd: int |
| 11942 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11943 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 11944 | Get the blocking mode of the file descriptor. |
| 11945 | |
| 11946 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 11947 | [clinic start generated code]*/ |
| 11948 | |
| 11949 | static int |
| 11950 | os_get_blocking_impl(PyObject *module, int fd) |
| 11951 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11952 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11953 | int blocking; |
| 11954 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11955 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11956 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11957 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 11958 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11959 | } |
| 11960 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 11961 | /*[clinic input] |
| 11962 | os.set_blocking |
| 11963 | fd: int |
| 11964 | blocking: bool(accept={int}) |
| 11965 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11966 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 11967 | Set the blocking mode of the specified file descriptor. |
| 11968 | |
| 11969 | Set the O_NONBLOCK flag if blocking is False, |
| 11970 | clear the O_NONBLOCK flag otherwise. |
| 11971 | [clinic start generated code]*/ |
| 11972 | |
| 11973 | static PyObject * |
| 11974 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 11975 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11976 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 11977 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11978 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11979 | _Py_BEGIN_SUPPRESS_IPH |
| 11980 | result = _Py_set_blocking(fd, blocking); |
| 11981 | _Py_END_SUPPRESS_IPH |
| 11982 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11983 | return NULL; |
| 11984 | Py_RETURN_NONE; |
| 11985 | } |
| 11986 | #endif /* !MS_WINDOWS */ |
| 11987 | |
| 11988 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 11989 | /*[clinic input] |
| 11990 | class os.DirEntry "DirEntry *" "&DirEntryType" |
| 11991 | [clinic start generated code]*/ |
| 11992 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11993 | |
| 11994 | typedef struct { |
| 11995 | PyObject_HEAD |
| 11996 | PyObject *name; |
| 11997 | PyObject *path; |
| 11998 | PyObject *stat; |
| 11999 | PyObject *lstat; |
| 12000 | #ifdef MS_WINDOWS |
| 12001 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12002 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12003 | int got_file_index; |
| 12004 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12005 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12006 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12007 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12008 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12009 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12010 | #endif |
| 12011 | } DirEntry; |
| 12012 | |
| 12013 | static void |
| 12014 | DirEntry_dealloc(DirEntry *entry) |
| 12015 | { |
| 12016 | Py_XDECREF(entry->name); |
| 12017 | Py_XDECREF(entry->path); |
| 12018 | Py_XDECREF(entry->stat); |
| 12019 | Py_XDECREF(entry->lstat); |
| 12020 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 12021 | } |
| 12022 | |
| 12023 | /* Forward reference */ |
| 12024 | static int |
| 12025 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 12026 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12027 | /*[clinic input] |
| 12028 | os.DirEntry.is_symlink -> bool |
| 12029 | |
| 12030 | Return True if the entry is a symbolic link; cached per entry. |
| 12031 | [clinic start generated code]*/ |
| 12032 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12033 | static int |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12034 | os_DirEntry_is_symlink_impl(DirEntry *self) |
| 12035 | /*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12036 | { |
| 12037 | #ifdef MS_WINDOWS |
| 12038 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12039 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 12040 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12041 | if (self->d_type != DT_UNKNOWN) |
| 12042 | return self->d_type == DT_LNK; |
| 12043 | else |
| 12044 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12045 | #else |
| 12046 | /* POSIX without d_type */ |
| 12047 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12048 | #endif |
| 12049 | } |
| 12050 | |
| 12051 | static PyObject * |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12052 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 12053 | { |
| 12054 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12055 | STRUCT_STAT st; |
| 12056 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12057 | |
| 12058 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12059 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 12060 | return NULL; |
| 12061 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12062 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12063 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 12064 | return NULL; |
| 12065 | const char *path = PyBytes_AS_STRING(ub); |
| 12066 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 12067 | #ifdef HAVE_FSTATAT |
| 12068 | result = fstatat(self->dir_fd, path, &st, |
| 12069 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 12070 | #else |
| 12071 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 12072 | return NULL; |
| 12073 | #endif /* HAVE_FSTATAT */ |
| 12074 | } |
| 12075 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12076 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12077 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12078 | if (follow_symlinks) |
| 12079 | result = STAT(path, &st); |
| 12080 | else |
| 12081 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12082 | } |
| 12083 | Py_DECREF(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12084 | |
| 12085 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12086 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12087 | |
| 12088 | return _pystat_fromstructstat(&st); |
| 12089 | } |
| 12090 | |
| 12091 | static PyObject * |
| 12092 | DirEntry_get_lstat(DirEntry *self) |
| 12093 | { |
| 12094 | if (!self->lstat) { |
| 12095 | #ifdef MS_WINDOWS |
| 12096 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 12097 | #else /* POSIX */ |
| 12098 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 12099 | #endif |
| 12100 | } |
| 12101 | Py_XINCREF(self->lstat); |
| 12102 | return self->lstat; |
| 12103 | } |
| 12104 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12105 | /*[clinic input] |
| 12106 | os.DirEntry.stat |
| 12107 | * |
| 12108 | follow_symlinks: bool = True |
| 12109 | |
| 12110 | Return stat_result object for the entry; cached per entry. |
| 12111 | [clinic start generated code]*/ |
| 12112 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12113 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12114 | os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks) |
| 12115 | /*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12116 | { |
| 12117 | if (!follow_symlinks) |
| 12118 | return DirEntry_get_lstat(self); |
| 12119 | |
| 12120 | if (!self->stat) { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12121 | int result = os_DirEntry_is_symlink_impl(self); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12122 | if (result == -1) |
| 12123 | return NULL; |
| 12124 | else if (result) |
| 12125 | self->stat = DirEntry_fetch_stat(self, 1); |
| 12126 | else |
| 12127 | self->stat = DirEntry_get_lstat(self); |
| 12128 | } |
| 12129 | |
| 12130 | Py_XINCREF(self->stat); |
| 12131 | return self->stat; |
| 12132 | } |
| 12133 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12134 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 12135 | static int |
| 12136 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 12137 | { |
| 12138 | PyObject *stat = NULL; |
| 12139 | PyObject *st_mode = NULL; |
| 12140 | long mode; |
| 12141 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12142 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12143 | int is_symlink; |
| 12144 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12145 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12146 | #ifdef MS_WINDOWS |
| 12147 | unsigned long dir_bits; |
| 12148 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12149 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12150 | |
| 12151 | #ifdef MS_WINDOWS |
| 12152 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 12153 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12154 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12155 | is_symlink = self->d_type == DT_LNK; |
| 12156 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 12157 | #endif |
| 12158 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12159 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12160 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12161 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12162 | stat = os_DirEntry_stat_impl(self, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12163 | if (!stat) { |
| 12164 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 12165 | /* If file doesn't exist (anymore), then return False |
| 12166 | (i.e., say it's not a file/directory) */ |
| 12167 | PyErr_Clear(); |
| 12168 | return 0; |
| 12169 | } |
| 12170 | goto error; |
| 12171 | } |
| 12172 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 12173 | if (!st_mode) |
| 12174 | goto error; |
| 12175 | |
| 12176 | mode = PyLong_AsLong(st_mode); |
| 12177 | if (mode == -1 && PyErr_Occurred()) |
| 12178 | goto error; |
| 12179 | Py_CLEAR(st_mode); |
| 12180 | Py_CLEAR(stat); |
| 12181 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12182 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12183 | } |
| 12184 | else if (is_symlink) { |
| 12185 | assert(mode_bits != S_IFLNK); |
| 12186 | result = 0; |
| 12187 | } |
| 12188 | else { |
| 12189 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 12190 | #ifdef MS_WINDOWS |
| 12191 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 12192 | if (mode_bits == S_IFDIR) |
| 12193 | result = dir_bits != 0; |
| 12194 | else |
| 12195 | result = dir_bits == 0; |
| 12196 | #else /* POSIX */ |
| 12197 | if (mode_bits == S_IFDIR) |
| 12198 | result = self->d_type == DT_DIR; |
| 12199 | else |
| 12200 | result = self->d_type == DT_REG; |
| 12201 | #endif |
| 12202 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12203 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12204 | |
| 12205 | return result; |
| 12206 | |
| 12207 | error: |
| 12208 | Py_XDECREF(st_mode); |
| 12209 | Py_XDECREF(stat); |
| 12210 | return -1; |
| 12211 | } |
| 12212 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12213 | /*[clinic input] |
| 12214 | os.DirEntry.is_dir -> bool |
| 12215 | * |
| 12216 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12217 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12218 | Return True if the entry is a directory; cached per entry. |
| 12219 | [clinic start generated code]*/ |
| 12220 | |
| 12221 | static int |
| 12222 | os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks) |
| 12223 | /*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/ |
| 12224 | { |
| 12225 | return DirEntry_test_mode(self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12226 | } |
| 12227 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12228 | /*[clinic input] |
| 12229 | os.DirEntry.is_file -> bool |
| 12230 | * |
| 12231 | follow_symlinks: bool = True |
| 12232 | |
| 12233 | Return True if the entry is a file; cached per entry. |
| 12234 | [clinic start generated code]*/ |
| 12235 | |
| 12236 | static int |
| 12237 | os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks) |
| 12238 | /*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12239 | { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12240 | return DirEntry_test_mode(self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12241 | } |
| 12242 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12243 | /*[clinic input] |
| 12244 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12245 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12246 | Return inode of the entry; cached per entry. |
| 12247 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12248 | |
| 12249 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12250 | os_DirEntry_inode_impl(DirEntry *self) |
| 12251 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12252 | { |
| 12253 | #ifdef MS_WINDOWS |
| 12254 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12255 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12256 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12257 | STRUCT_STAT stat; |
| 12258 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12259 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12260 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12261 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12262 | path = PyUnicode_AsUnicode(unicode); |
| 12263 | result = LSTAT(path, &stat); |
| 12264 | Py_DECREF(unicode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12265 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12266 | if (result != 0) |
| 12267 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12268 | |
| 12269 | self->win32_file_index = stat.st_ino; |
| 12270 | self->got_file_index = 1; |
| 12271 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12272 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 12273 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12274 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 12275 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 12276 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12277 | #endif |
| 12278 | } |
| 12279 | |
| 12280 | static PyObject * |
| 12281 | DirEntry_repr(DirEntry *self) |
| 12282 | { |
| 12283 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 12284 | } |
| 12285 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12286 | /*[clinic input] |
| 12287 | os.DirEntry.__fspath__ |
| 12288 | |
| 12289 | Returns the path for the entry. |
| 12290 | [clinic start generated code]*/ |
| 12291 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12292 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12293 | os_DirEntry___fspath___impl(DirEntry *self) |
| 12294 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12295 | { |
| 12296 | Py_INCREF(self->path); |
| 12297 | return self->path; |
| 12298 | } |
| 12299 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12300 | static PyMemberDef DirEntry_members[] = { |
| 12301 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 12302 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 12303 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 12304 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 12305 | {NULL} |
| 12306 | }; |
| 12307 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12308 | #include "clinic/posixmodule.c.h" |
| 12309 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12310 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12311 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 12312 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 12313 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 12314 | OS_DIRENTRY_STAT_METHODDEF |
| 12315 | OS_DIRENTRY_INODE_METHODDEF |
| 12316 | OS_DIRENTRY___FSPATH___METHODDEF |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12317 | {NULL} |
| 12318 | }; |
| 12319 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12320 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12321 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12322 | MODNAME ".DirEntry", /* tp_name */ |
| 12323 | sizeof(DirEntry), /* tp_basicsize */ |
| 12324 | 0, /* tp_itemsize */ |
| 12325 | /* methods */ |
| 12326 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
| 12327 | 0, /* tp_print */ |
| 12328 | 0, /* tp_getattr */ |
| 12329 | 0, /* tp_setattr */ |
| 12330 | 0, /* tp_compare */ |
| 12331 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 12332 | 0, /* tp_as_number */ |
| 12333 | 0, /* tp_as_sequence */ |
| 12334 | 0, /* tp_as_mapping */ |
| 12335 | 0, /* tp_hash */ |
| 12336 | 0, /* tp_call */ |
| 12337 | 0, /* tp_str */ |
| 12338 | 0, /* tp_getattro */ |
| 12339 | 0, /* tp_setattro */ |
| 12340 | 0, /* tp_as_buffer */ |
| 12341 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 12342 | 0, /* tp_doc */ |
| 12343 | 0, /* tp_traverse */ |
| 12344 | 0, /* tp_clear */ |
| 12345 | 0, /* tp_richcompare */ |
| 12346 | 0, /* tp_weaklistoffset */ |
| 12347 | 0, /* tp_iter */ |
| 12348 | 0, /* tp_iternext */ |
| 12349 | DirEntry_methods, /* tp_methods */ |
| 12350 | DirEntry_members, /* tp_members */ |
| 12351 | }; |
| 12352 | |
| 12353 | #ifdef MS_WINDOWS |
| 12354 | |
| 12355 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12356 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12357 | { |
| 12358 | Py_ssize_t path_len; |
| 12359 | Py_ssize_t size; |
| 12360 | wchar_t *result; |
| 12361 | wchar_t ch; |
| 12362 | |
| 12363 | if (!path_wide) { /* Default arg: "." */ |
| 12364 | path_wide = L"."; |
| 12365 | path_len = 1; |
| 12366 | } |
| 12367 | else { |
| 12368 | path_len = wcslen(path_wide); |
| 12369 | } |
| 12370 | |
| 12371 | /* The +1's are for the path separator and the NUL */ |
| 12372 | size = path_len + 1 + wcslen(filename) + 1; |
| 12373 | result = PyMem_New(wchar_t, size); |
| 12374 | if (!result) { |
| 12375 | PyErr_NoMemory(); |
| 12376 | return NULL; |
| 12377 | } |
| 12378 | wcscpy(result, path_wide); |
| 12379 | if (path_len > 0) { |
| 12380 | ch = result[path_len - 1]; |
| 12381 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 12382 | result[path_len++] = SEP; |
| 12383 | wcscpy(result + path_len, filename); |
| 12384 | } |
| 12385 | return result; |
| 12386 | } |
| 12387 | |
| 12388 | static PyObject * |
| 12389 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 12390 | { |
| 12391 | DirEntry *entry; |
| 12392 | BY_HANDLE_FILE_INFORMATION file_info; |
| 12393 | ULONG reparse_tag; |
| 12394 | wchar_t *joined_path; |
| 12395 | |
| 12396 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 12397 | if (!entry) |
| 12398 | return NULL; |
| 12399 | entry->name = NULL; |
| 12400 | entry->path = NULL; |
| 12401 | entry->stat = NULL; |
| 12402 | entry->lstat = NULL; |
| 12403 | entry->got_file_index = 0; |
| 12404 | |
| 12405 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 12406 | if (!entry->name) |
| 12407 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12408 | if (path->narrow) { |
| 12409 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 12410 | if (!entry->name) |
| 12411 | goto error; |
| 12412 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12413 | |
| 12414 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 12415 | if (!joined_path) |
| 12416 | goto error; |
| 12417 | |
| 12418 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 12419 | PyMem_Free(joined_path); |
| 12420 | if (!entry->path) |
| 12421 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12422 | if (path->narrow) { |
| 12423 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 12424 | if (!entry->path) |
| 12425 | goto error; |
| 12426 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12427 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12428 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12429 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 12430 | |
| 12431 | return (PyObject *)entry; |
| 12432 | |
| 12433 | error: |
| 12434 | Py_DECREF(entry); |
| 12435 | return NULL; |
| 12436 | } |
| 12437 | |
| 12438 | #else /* POSIX */ |
| 12439 | |
| 12440 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12441 | 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] | 12442 | { |
| 12443 | Py_ssize_t path_len; |
| 12444 | Py_ssize_t size; |
| 12445 | char *result; |
| 12446 | |
| 12447 | if (!path_narrow) { /* Default arg: "." */ |
| 12448 | path_narrow = "."; |
| 12449 | path_len = 1; |
| 12450 | } |
| 12451 | else { |
| 12452 | path_len = strlen(path_narrow); |
| 12453 | } |
| 12454 | |
| 12455 | if (filename_len == -1) |
| 12456 | filename_len = strlen(filename); |
| 12457 | |
| 12458 | /* The +1's are for the path separator and the NUL */ |
| 12459 | size = path_len + 1 + filename_len + 1; |
| 12460 | result = PyMem_New(char, size); |
| 12461 | if (!result) { |
| 12462 | PyErr_NoMemory(); |
| 12463 | return NULL; |
| 12464 | } |
| 12465 | strcpy(result, path_narrow); |
| 12466 | if (path_len > 0 && result[path_len - 1] != '/') |
| 12467 | result[path_len++] = '/'; |
| 12468 | strcpy(result + path_len, filename); |
| 12469 | return result; |
| 12470 | } |
| 12471 | |
| 12472 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12473 | 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] | 12474 | ino_t d_ino |
| 12475 | #ifdef HAVE_DIRENT_D_TYPE |
| 12476 | , unsigned char d_type |
| 12477 | #endif |
| 12478 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12479 | { |
| 12480 | DirEntry *entry; |
| 12481 | char *joined_path; |
| 12482 | |
| 12483 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 12484 | if (!entry) |
| 12485 | return NULL; |
| 12486 | entry->name = NULL; |
| 12487 | entry->path = NULL; |
| 12488 | entry->stat = NULL; |
| 12489 | entry->lstat = NULL; |
| 12490 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12491 | if (path->fd != -1) { |
| 12492 | entry->dir_fd = path->fd; |
| 12493 | joined_path = NULL; |
| 12494 | } |
| 12495 | else { |
| 12496 | entry->dir_fd = DEFAULT_DIR_FD; |
| 12497 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 12498 | if (!joined_path) |
| 12499 | goto error; |
| 12500 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12501 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 12502 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12503 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12504 | if (joined_path) |
| 12505 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12506 | } |
| 12507 | else { |
| 12508 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12509 | if (joined_path) |
| 12510 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12511 | } |
| 12512 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12513 | if (!entry->name) |
| 12514 | goto error; |
| 12515 | |
| 12516 | if (path->fd != -1) { |
| 12517 | entry->path = entry->name; |
| 12518 | Py_INCREF(entry->path); |
| 12519 | } |
| 12520 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12521 | goto error; |
| 12522 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12523 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12524 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12525 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12526 | entry->d_ino = d_ino; |
| 12527 | |
| 12528 | return (PyObject *)entry; |
| 12529 | |
| 12530 | error: |
| 12531 | Py_XDECREF(entry); |
| 12532 | return NULL; |
| 12533 | } |
| 12534 | |
| 12535 | #endif |
| 12536 | |
| 12537 | |
| 12538 | typedef struct { |
| 12539 | PyObject_HEAD |
| 12540 | path_t path; |
| 12541 | #ifdef MS_WINDOWS |
| 12542 | HANDLE handle; |
| 12543 | WIN32_FIND_DATAW file_data; |
| 12544 | int first_time; |
| 12545 | #else /* POSIX */ |
| 12546 | DIR *dirp; |
| 12547 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12548 | #ifdef HAVE_FDOPENDIR |
| 12549 | int fd; |
| 12550 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12551 | } ScandirIterator; |
| 12552 | |
| 12553 | #ifdef MS_WINDOWS |
| 12554 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12555 | static int |
| 12556 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 12557 | { |
| 12558 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 12559 | } |
| 12560 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12561 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12562 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12563 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12564 | HANDLE handle = iterator->handle; |
| 12565 | |
| 12566 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12567 | return; |
| 12568 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12569 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12570 | Py_BEGIN_ALLOW_THREADS |
| 12571 | FindClose(handle); |
| 12572 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12573 | } |
| 12574 | |
| 12575 | static PyObject * |
| 12576 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12577 | { |
| 12578 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 12579 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12580 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12581 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12582 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12583 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12584 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12585 | |
| 12586 | while (1) { |
| 12587 | if (!iterator->first_time) { |
| 12588 | Py_BEGIN_ALLOW_THREADS |
| 12589 | success = FindNextFileW(iterator->handle, file_data); |
| 12590 | Py_END_ALLOW_THREADS |
| 12591 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12592 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12593 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12594 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12595 | break; |
| 12596 | } |
| 12597 | } |
| 12598 | iterator->first_time = 0; |
| 12599 | |
| 12600 | /* Skip over . and .. */ |
| 12601 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12602 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 12603 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 12604 | if (!entry) |
| 12605 | break; |
| 12606 | return entry; |
| 12607 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12608 | |
| 12609 | /* Loop till we get a non-dot directory or finish iterating */ |
| 12610 | } |
| 12611 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12612 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12613 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12614 | return NULL; |
| 12615 | } |
| 12616 | |
| 12617 | #else /* POSIX */ |
| 12618 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12619 | static int |
| 12620 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 12621 | { |
| 12622 | return !iterator->dirp; |
| 12623 | } |
| 12624 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12625 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12626 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12627 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12628 | DIR *dirp = iterator->dirp; |
| 12629 | |
| 12630 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12631 | return; |
| 12632 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12633 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12634 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12635 | #ifdef HAVE_FDOPENDIR |
| 12636 | if (iterator->path.fd != -1) |
| 12637 | rewinddir(dirp); |
| 12638 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12639 | closedir(dirp); |
| 12640 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12641 | return; |
| 12642 | } |
| 12643 | |
| 12644 | static PyObject * |
| 12645 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12646 | { |
| 12647 | struct dirent *direntp; |
| 12648 | Py_ssize_t name_len; |
| 12649 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12650 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12651 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12652 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12653 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12654 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12655 | |
| 12656 | while (1) { |
| 12657 | errno = 0; |
| 12658 | Py_BEGIN_ALLOW_THREADS |
| 12659 | direntp = readdir(iterator->dirp); |
| 12660 | Py_END_ALLOW_THREADS |
| 12661 | |
| 12662 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12663 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12664 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12665 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12666 | break; |
| 12667 | } |
| 12668 | |
| 12669 | /* Skip over . and .. */ |
| 12670 | name_len = NAMLEN(direntp); |
| 12671 | is_dot = direntp->d_name[0] == '.' && |
| 12672 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 12673 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12674 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12675 | name_len, direntp->d_ino |
| 12676 | #ifdef HAVE_DIRENT_D_TYPE |
| 12677 | , direntp->d_type |
| 12678 | #endif |
| 12679 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12680 | if (!entry) |
| 12681 | break; |
| 12682 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12683 | } |
| 12684 | |
| 12685 | /* Loop till we get a non-dot directory or finish iterating */ |
| 12686 | } |
| 12687 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12688 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12689 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12690 | return NULL; |
| 12691 | } |
| 12692 | |
| 12693 | #endif |
| 12694 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12695 | static PyObject * |
| 12696 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 12697 | { |
| 12698 | ScandirIterator_closedir(self); |
| 12699 | Py_RETURN_NONE; |
| 12700 | } |
| 12701 | |
| 12702 | static PyObject * |
| 12703 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 12704 | { |
| 12705 | Py_INCREF(self); |
| 12706 | return self; |
| 12707 | } |
| 12708 | |
| 12709 | static PyObject * |
| 12710 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 12711 | { |
| 12712 | ScandirIterator_closedir(self); |
| 12713 | Py_RETURN_NONE; |
| 12714 | } |
| 12715 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12716 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12717 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 12718 | { |
| 12719 | PyObject *error_type, *error_value, *error_traceback; |
| 12720 | |
| 12721 | /* Save the current exception, if any. */ |
| 12722 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 12723 | |
| 12724 | if (!ScandirIterator_is_closed(iterator)) { |
| 12725 | ScandirIterator_closedir(iterator); |
| 12726 | |
| 12727 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 12728 | "unclosed scandir iterator %R", iterator)) { |
| 12729 | /* Spurious errors can appear at shutdown */ |
| 12730 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 12731 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 12732 | } |
| 12733 | } |
| 12734 | } |
| 12735 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12736 | path_cleanup(&iterator->path); |
| 12737 | |
| 12738 | /* Restore the saved exception. */ |
| 12739 | PyErr_Restore(error_type, error_value, error_traceback); |
| 12740 | } |
| 12741 | |
| 12742 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12743 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 12744 | { |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12745 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 12746 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12747 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12748 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 12749 | } |
| 12750 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12751 | static PyMethodDef ScandirIterator_methods[] = { |
| 12752 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 12753 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 12754 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 12755 | {NULL} |
| 12756 | }; |
| 12757 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12758 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12759 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12760 | MODNAME ".ScandirIterator", /* tp_name */ |
| 12761 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 12762 | 0, /* tp_itemsize */ |
| 12763 | /* methods */ |
| 12764 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
| 12765 | 0, /* tp_print */ |
| 12766 | 0, /* tp_getattr */ |
| 12767 | 0, /* tp_setattr */ |
| 12768 | 0, /* tp_compare */ |
| 12769 | 0, /* tp_repr */ |
| 12770 | 0, /* tp_as_number */ |
| 12771 | 0, /* tp_as_sequence */ |
| 12772 | 0, /* tp_as_mapping */ |
| 12773 | 0, /* tp_hash */ |
| 12774 | 0, /* tp_call */ |
| 12775 | 0, /* tp_str */ |
| 12776 | 0, /* tp_getattro */ |
| 12777 | 0, /* tp_setattro */ |
| 12778 | 0, /* tp_as_buffer */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12779 | Py_TPFLAGS_DEFAULT |
| 12780 | | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12781 | 0, /* tp_doc */ |
| 12782 | 0, /* tp_traverse */ |
| 12783 | 0, /* tp_clear */ |
| 12784 | 0, /* tp_richcompare */ |
| 12785 | 0, /* tp_weaklistoffset */ |
| 12786 | PyObject_SelfIter, /* tp_iter */ |
| 12787 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12788 | ScandirIterator_methods, /* tp_methods */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12789 | 0, /* tp_members */ |
| 12790 | 0, /* tp_getset */ |
| 12791 | 0, /* tp_base */ |
| 12792 | 0, /* tp_dict */ |
| 12793 | 0, /* tp_descr_get */ |
| 12794 | 0, /* tp_descr_set */ |
| 12795 | 0, /* tp_dictoffset */ |
| 12796 | 0, /* tp_init */ |
| 12797 | 0, /* tp_alloc */ |
| 12798 | 0, /* tp_new */ |
| 12799 | 0, /* tp_free */ |
| 12800 | 0, /* tp_is_gc */ |
| 12801 | 0, /* tp_bases */ |
| 12802 | 0, /* tp_mro */ |
| 12803 | 0, /* tp_cache */ |
| 12804 | 0, /* tp_subclasses */ |
| 12805 | 0, /* tp_weaklist */ |
| 12806 | 0, /* tp_del */ |
| 12807 | 0, /* tp_version_tag */ |
| 12808 | (destructor)ScandirIterator_finalize, /* tp_finalize */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12809 | }; |
| 12810 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12811 | /*[clinic input] |
| 12812 | os.scandir |
| 12813 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12814 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12815 | |
| 12816 | Return an iterator of DirEntry objects for given path. |
| 12817 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12818 | 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] | 12819 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 12820 | all other circumstances they will be str. |
| 12821 | |
| 12822 | If path is None, uses the path='.'. |
| 12823 | [clinic start generated code]*/ |
| 12824 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12825 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12826 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12827 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12828 | { |
| 12829 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12830 | #ifdef MS_WINDOWS |
| 12831 | wchar_t *path_strW; |
| 12832 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12833 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12834 | #ifdef HAVE_FDOPENDIR |
| 12835 | int fd = -1; |
| 12836 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12837 | #endif |
| 12838 | |
| 12839 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 12840 | if (!iterator) |
| 12841 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12842 | |
| 12843 | #ifdef MS_WINDOWS |
| 12844 | iterator->handle = INVALID_HANDLE_VALUE; |
| 12845 | #else |
| 12846 | iterator->dirp = NULL; |
| 12847 | #endif |
| 12848 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12849 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 12850 | /* Move the ownership to iterator->path */ |
| 12851 | path->object = NULL; |
| 12852 | path->cleanup = NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12853 | |
| 12854 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12855 | iterator->first_time = 1; |
| 12856 | |
| 12857 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 12858 | if (!path_strW) |
| 12859 | goto error; |
| 12860 | |
| 12861 | Py_BEGIN_ALLOW_THREADS |
| 12862 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 12863 | Py_END_ALLOW_THREADS |
| 12864 | |
| 12865 | PyMem_Free(path_strW); |
| 12866 | |
| 12867 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 12868 | path_error(&iterator->path); |
| 12869 | goto error; |
| 12870 | } |
| 12871 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12872 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12873 | #ifdef HAVE_FDOPENDIR |
| 12874 | if (path->fd != -1) { |
| 12875 | /* closedir() closes the FD, so we duplicate it */ |
| 12876 | fd = _Py_dup(path->fd); |
| 12877 | if (fd == -1) |
| 12878 | goto error; |
| 12879 | |
| 12880 | Py_BEGIN_ALLOW_THREADS |
| 12881 | iterator->dirp = fdopendir(fd); |
| 12882 | Py_END_ALLOW_THREADS |
| 12883 | } |
| 12884 | else |
| 12885 | #endif |
| 12886 | { |
| 12887 | if (iterator->path.narrow) |
| 12888 | path_str = iterator->path.narrow; |
| 12889 | else |
| 12890 | path_str = "."; |
| 12891 | |
| 12892 | Py_BEGIN_ALLOW_THREADS |
| 12893 | iterator->dirp = opendir(path_str); |
| 12894 | Py_END_ALLOW_THREADS |
| 12895 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12896 | |
| 12897 | if (!iterator->dirp) { |
| 12898 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12899 | #ifdef HAVE_FDOPENDIR |
| 12900 | if (fd != -1) { |
| 12901 | Py_BEGIN_ALLOW_THREADS |
| 12902 | close(fd); |
| 12903 | Py_END_ALLOW_THREADS |
| 12904 | } |
| 12905 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12906 | goto error; |
| 12907 | } |
| 12908 | #endif |
| 12909 | |
| 12910 | return (PyObject *)iterator; |
| 12911 | |
| 12912 | error: |
| 12913 | Py_DECREF(iterator); |
| 12914 | return NULL; |
| 12915 | } |
| 12916 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12917 | /* |
| 12918 | Return the file system path representation of the object. |
| 12919 | |
| 12920 | If the object is str or bytes, then allow it to pass through with |
| 12921 | an incremented refcount. If the object defines __fspath__(), then |
| 12922 | return the result of that method. All other types raise a TypeError. |
| 12923 | */ |
| 12924 | PyObject * |
| 12925 | PyOS_FSPath(PyObject *path) |
| 12926 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 12927 | /* For error message reasons, this function is manually inlined in |
| 12928 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12929 | _Py_IDENTIFIER(__fspath__); |
| 12930 | PyObject *func = NULL; |
| 12931 | PyObject *path_repr = NULL; |
| 12932 | |
| 12933 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 12934 | Py_INCREF(path); |
| 12935 | return path; |
| 12936 | } |
| 12937 | |
| 12938 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 12939 | if (NULL == func) { |
| 12940 | return PyErr_Format(PyExc_TypeError, |
| 12941 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 12942 | "not %.200s", |
| 12943 | Py_TYPE(path)->tp_name); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12944 | } |
| 12945 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 12946 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12947 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 12948 | if (NULL == path_repr) { |
| 12949 | return NULL; |
| 12950 | } |
| 12951 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 12952 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 12953 | PyErr_Format(PyExc_TypeError, |
| 12954 | "expected %.200s.__fspath__() to return str or bytes, " |
| 12955 | "not %.200s", Py_TYPE(path)->tp_name, |
| 12956 | Py_TYPE(path_repr)->tp_name); |
| 12957 | Py_DECREF(path_repr); |
| 12958 | return NULL; |
| 12959 | } |
| 12960 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12961 | return path_repr; |
| 12962 | } |
| 12963 | |
| 12964 | /*[clinic input] |
| 12965 | os.fspath |
| 12966 | |
| 12967 | path: object |
| 12968 | |
| 12969 | Return the file system path representation of the object. |
| 12970 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 12971 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 12972 | object defines __fspath__(), then return the result of that method. All other |
| 12973 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12974 | [clinic start generated code]*/ |
| 12975 | |
| 12976 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12977 | os_fspath_impl(PyObject *module, PyObject *path) |
| 12978 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12979 | { |
| 12980 | return PyOS_FSPath(path); |
| 12981 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12982 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12983 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 12984 | /*[clinic input] |
| 12985 | os.getrandom |
| 12986 | |
| 12987 | size: Py_ssize_t |
| 12988 | flags: int=0 |
| 12989 | |
| 12990 | Obtain a series of random bytes. |
| 12991 | [clinic start generated code]*/ |
| 12992 | |
| 12993 | static PyObject * |
| 12994 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 12995 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 12996 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12997 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 12998 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12999 | |
| 13000 | if (size < 0) { |
| 13001 | errno = EINVAL; |
| 13002 | return posix_error(); |
| 13003 | } |
| 13004 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13005 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 13006 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13007 | PyErr_NoMemory(); |
| 13008 | return NULL; |
| 13009 | } |
| 13010 | |
| 13011 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13012 | n = syscall(SYS_getrandom, |
| 13013 | PyBytes_AS_STRING(bytes), |
| 13014 | PyBytes_GET_SIZE(bytes), |
| 13015 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13016 | if (n < 0 && errno == EINTR) { |
| 13017 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13018 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13019 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13020 | |
| 13021 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13022 | continue; |
| 13023 | } |
| 13024 | break; |
| 13025 | } |
| 13026 | |
| 13027 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13028 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13029 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13030 | } |
| 13031 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13032 | if (n != size) { |
| 13033 | _PyBytes_Resize(&bytes, n); |
| 13034 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13035 | |
| 13036 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13037 | |
| 13038 | error: |
| 13039 | Py_DECREF(bytes); |
| 13040 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13041 | } |
| 13042 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 13043 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13044 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13045 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13046 | |
| 13047 | OS_STAT_METHODDEF |
| 13048 | OS_ACCESS_METHODDEF |
| 13049 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13050 | OS_CHDIR_METHODDEF |
| 13051 | OS_CHFLAGS_METHODDEF |
| 13052 | OS_CHMOD_METHODDEF |
| 13053 | OS_FCHMOD_METHODDEF |
| 13054 | OS_LCHMOD_METHODDEF |
| 13055 | OS_CHOWN_METHODDEF |
| 13056 | OS_FCHOWN_METHODDEF |
| 13057 | OS_LCHOWN_METHODDEF |
| 13058 | OS_LCHFLAGS_METHODDEF |
| 13059 | OS_CHROOT_METHODDEF |
| 13060 | OS_CTERMID_METHODDEF |
| 13061 | OS_GETCWD_METHODDEF |
| 13062 | OS_GETCWDB_METHODDEF |
| 13063 | OS_LINK_METHODDEF |
| 13064 | OS_LISTDIR_METHODDEF |
| 13065 | OS_LSTAT_METHODDEF |
| 13066 | OS_MKDIR_METHODDEF |
| 13067 | OS_NICE_METHODDEF |
| 13068 | OS_GETPRIORITY_METHODDEF |
| 13069 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13070 | OS_POSIX_SPAWN_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13071 | OS_READLINK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13072 | OS_RENAME_METHODDEF |
| 13073 | OS_REPLACE_METHODDEF |
| 13074 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13075 | OS_SYMLINK_METHODDEF |
| 13076 | OS_SYSTEM_METHODDEF |
| 13077 | OS_UMASK_METHODDEF |
| 13078 | OS_UNAME_METHODDEF |
| 13079 | OS_UNLINK_METHODDEF |
| 13080 | OS_REMOVE_METHODDEF |
| 13081 | OS_UTIME_METHODDEF |
| 13082 | OS_TIMES_METHODDEF |
| 13083 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 13084 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13085 | OS_EXECV_METHODDEF |
| 13086 | OS_EXECVE_METHODDEF |
| 13087 | OS_SPAWNV_METHODDEF |
| 13088 | OS_SPAWNVE_METHODDEF |
| 13089 | OS_FORK1_METHODDEF |
| 13090 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 13091 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13092 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 13093 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 13094 | OS_SCHED_GETPARAM_METHODDEF |
| 13095 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 13096 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 13097 | OS_SCHED_SETPARAM_METHODDEF |
| 13098 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 13099 | OS_SCHED_YIELD_METHODDEF |
| 13100 | OS_SCHED_SETAFFINITY_METHODDEF |
| 13101 | OS_SCHED_GETAFFINITY_METHODDEF |
| 13102 | OS_OPENPTY_METHODDEF |
| 13103 | OS_FORKPTY_METHODDEF |
| 13104 | OS_GETEGID_METHODDEF |
| 13105 | OS_GETEUID_METHODDEF |
| 13106 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 13107 | #ifdef HAVE_GETGROUPLIST |
| 13108 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 13109 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13110 | OS_GETGROUPS_METHODDEF |
| 13111 | OS_GETPID_METHODDEF |
| 13112 | OS_GETPGRP_METHODDEF |
| 13113 | OS_GETPPID_METHODDEF |
| 13114 | OS_GETUID_METHODDEF |
| 13115 | OS_GETLOGIN_METHODDEF |
| 13116 | OS_KILL_METHODDEF |
| 13117 | OS_KILLPG_METHODDEF |
| 13118 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13119 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13120 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13121 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13122 | OS_SETUID_METHODDEF |
| 13123 | OS_SETEUID_METHODDEF |
| 13124 | OS_SETREUID_METHODDEF |
| 13125 | OS_SETGID_METHODDEF |
| 13126 | OS_SETEGID_METHODDEF |
| 13127 | OS_SETREGID_METHODDEF |
| 13128 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13129 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13130 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13131 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13132 | OS_GETPGID_METHODDEF |
| 13133 | OS_SETPGRP_METHODDEF |
| 13134 | OS_WAIT_METHODDEF |
| 13135 | OS_WAIT3_METHODDEF |
| 13136 | OS_WAIT4_METHODDEF |
| 13137 | OS_WAITID_METHODDEF |
| 13138 | OS_WAITPID_METHODDEF |
| 13139 | OS_GETSID_METHODDEF |
| 13140 | OS_SETSID_METHODDEF |
| 13141 | OS_SETPGID_METHODDEF |
| 13142 | OS_TCGETPGRP_METHODDEF |
| 13143 | OS_TCSETPGRP_METHODDEF |
| 13144 | OS_OPEN_METHODDEF |
| 13145 | OS_CLOSE_METHODDEF |
| 13146 | OS_CLOSERANGE_METHODDEF |
| 13147 | OS_DEVICE_ENCODING_METHODDEF |
| 13148 | OS_DUP_METHODDEF |
| 13149 | OS_DUP2_METHODDEF |
| 13150 | OS_LOCKF_METHODDEF |
| 13151 | OS_LSEEK_METHODDEF |
| 13152 | OS_READ_METHODDEF |
| 13153 | OS_READV_METHODDEF |
| 13154 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13155 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13156 | OS_WRITE_METHODDEF |
| 13157 | OS_WRITEV_METHODDEF |
| 13158 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13159 | OS_PWRITEV_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13160 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 13161 | {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13162 | posix_sendfile__doc__}, |
| 13163 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13164 | OS_FSTAT_METHODDEF |
| 13165 | OS_ISATTY_METHODDEF |
| 13166 | OS_PIPE_METHODDEF |
| 13167 | OS_PIPE2_METHODDEF |
| 13168 | OS_MKFIFO_METHODDEF |
| 13169 | OS_MKNOD_METHODDEF |
| 13170 | OS_MAJOR_METHODDEF |
| 13171 | OS_MINOR_METHODDEF |
| 13172 | OS_MAKEDEV_METHODDEF |
| 13173 | OS_FTRUNCATE_METHODDEF |
| 13174 | OS_TRUNCATE_METHODDEF |
| 13175 | OS_POSIX_FALLOCATE_METHODDEF |
| 13176 | OS_POSIX_FADVISE_METHODDEF |
| 13177 | OS_PUTENV_METHODDEF |
| 13178 | OS_UNSETENV_METHODDEF |
| 13179 | OS_STRERROR_METHODDEF |
| 13180 | OS_FCHDIR_METHODDEF |
| 13181 | OS_FSYNC_METHODDEF |
| 13182 | OS_SYNC_METHODDEF |
| 13183 | OS_FDATASYNC_METHODDEF |
| 13184 | OS_WCOREDUMP_METHODDEF |
| 13185 | OS_WIFCONTINUED_METHODDEF |
| 13186 | OS_WIFSTOPPED_METHODDEF |
| 13187 | OS_WIFSIGNALED_METHODDEF |
| 13188 | OS_WIFEXITED_METHODDEF |
| 13189 | OS_WEXITSTATUS_METHODDEF |
| 13190 | OS_WTERMSIG_METHODDEF |
| 13191 | OS_WSTOPSIG_METHODDEF |
| 13192 | OS_FSTATVFS_METHODDEF |
| 13193 | OS_STATVFS_METHODDEF |
| 13194 | OS_CONFSTR_METHODDEF |
| 13195 | OS_SYSCONF_METHODDEF |
| 13196 | OS_FPATHCONF_METHODDEF |
| 13197 | OS_PATHCONF_METHODDEF |
| 13198 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 13199 | OS__GETFULLPATHNAME_METHODDEF |
| 13200 | OS__ISDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13201 | OS__GETDISKUSAGE_METHODDEF |
| 13202 | OS__GETFINALPATHNAME_METHODDEF |
| 13203 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 13204 | OS_GETLOADAVG_METHODDEF |
| 13205 | OS_URANDOM_METHODDEF |
| 13206 | OS_SETRESUID_METHODDEF |
| 13207 | OS_SETRESGID_METHODDEF |
| 13208 | OS_GETRESUID_METHODDEF |
| 13209 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 13210 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13211 | OS_GETXATTR_METHODDEF |
| 13212 | OS_SETXATTR_METHODDEF |
| 13213 | OS_REMOVEXATTR_METHODDEF |
| 13214 | OS_LISTXATTR_METHODDEF |
| 13215 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13216 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 13217 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 13218 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13219 | OS_CPU_COUNT_METHODDEF |
| 13220 | OS_GET_INHERITABLE_METHODDEF |
| 13221 | OS_SET_INHERITABLE_METHODDEF |
| 13222 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 13223 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13224 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13225 | OS_GET_BLOCKING_METHODDEF |
| 13226 | OS_SET_BLOCKING_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13227 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13228 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13229 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13230 | OS_GETRANDOM_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13231 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13232 | }; |
| 13233 | |
| 13234 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13235 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13236 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13237 | enable_symlink() |
| 13238 | { |
| 13239 | HANDLE tok; |
| 13240 | TOKEN_PRIVILEGES tok_priv; |
| 13241 | LUID luid; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13242 | |
| 13243 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13244 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13245 | |
| 13246 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13247 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13248 | |
| 13249 | tok_priv.PrivilegeCount = 1; |
| 13250 | tok_priv.Privileges[0].Luid = luid; |
| 13251 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 13252 | |
| 13253 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 13254 | sizeof(TOKEN_PRIVILEGES), |
| 13255 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13256 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13257 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13258 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 13259 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13260 | } |
| 13261 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 13262 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13263 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13264 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13265 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13266 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13267 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13268 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13269 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13270 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13271 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13272 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13273 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13274 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13275 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13276 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13277 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13278 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13279 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13280 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13281 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13282 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13283 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13284 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13285 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13286 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13287 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13288 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13289 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13290 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13291 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13292 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13293 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13294 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13295 | #endif |
| 13296 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13297 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13298 | #endif |
| 13299 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13300 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13301 | #endif |
| 13302 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13303 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13304 | #endif |
| 13305 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13306 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13307 | #endif |
| 13308 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13309 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13310 | #endif |
| 13311 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13312 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13313 | #endif |
| 13314 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13315 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13316 | #endif |
| 13317 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13318 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13319 | #endif |
| 13320 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13321 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13322 | #endif |
| 13323 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13324 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13325 | #endif |
| 13326 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13327 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13328 | #endif |
| 13329 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13330 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13331 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13332 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13333 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13334 | #endif |
| 13335 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13336 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13337 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13338 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13339 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13340 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13341 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13342 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13343 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13344 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13345 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13346 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13347 | #endif |
| 13348 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13349 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13350 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13351 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13352 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13353 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13354 | #endif |
| 13355 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13356 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13357 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13358 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13359 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13360 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13361 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13362 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13363 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 13364 | #ifdef O_TMPFILE |
| 13365 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 13366 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13367 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13368 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13369 | #endif |
| 13370 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13371 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13372 | #endif |
| 13373 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13374 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13375 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13376 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13377 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13378 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13379 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13380 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13381 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13382 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13383 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13384 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13385 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13386 | #endif |
| 13387 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13388 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13389 | #endif |
| 13390 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13391 | /* MS Windows */ |
| 13392 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13393 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13394 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13395 | #endif |
| 13396 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13397 | /* Optimize for short life (keep in memory). */ |
| 13398 | /* 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] | 13399 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13400 | #endif |
| 13401 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13402 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13403 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13404 | #endif |
| 13405 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13406 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13407 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13408 | #endif |
| 13409 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13410 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13411 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13412 | #endif |
| 13413 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13414 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13415 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13416 | /* Send a SIGIO signal whenever input or output |
| 13417 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13418 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13419 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13420 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13421 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13422 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13423 | #endif |
| 13424 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13425 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13426 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13427 | #endif |
| 13428 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13429 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13430 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13431 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13432 | #ifdef O_NOLINKS |
| 13433 | /* 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] | 13434 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13435 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13436 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13437 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13438 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13439 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 13440 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13441 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13442 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13443 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13444 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13445 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13446 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13447 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13448 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13449 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13450 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13451 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13452 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13453 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13454 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13455 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13456 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13457 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13458 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13459 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13460 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13461 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13462 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13463 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13464 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13465 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13466 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13467 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13468 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13469 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13470 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13471 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13472 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13473 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13474 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13475 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13476 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13477 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13478 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13479 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13480 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13481 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13482 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13483 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13484 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13485 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13486 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13487 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13488 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13489 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13490 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13491 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13492 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13493 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 13494 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13495 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13496 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13497 | #endif /* ST_RDONLY */ |
| 13498 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13499 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13500 | #endif /* ST_NOSUID */ |
| 13501 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 13502 | /* GNU extensions */ |
| 13503 | #ifdef ST_NODEV |
| 13504 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 13505 | #endif /* ST_NODEV */ |
| 13506 | #ifdef ST_NOEXEC |
| 13507 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 13508 | #endif /* ST_NOEXEC */ |
| 13509 | #ifdef ST_SYNCHRONOUS |
| 13510 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 13511 | #endif /* ST_SYNCHRONOUS */ |
| 13512 | #ifdef ST_MANDLOCK |
| 13513 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 13514 | #endif /* ST_MANDLOCK */ |
| 13515 | #ifdef ST_WRITE |
| 13516 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 13517 | #endif /* ST_WRITE */ |
| 13518 | #ifdef ST_APPEND |
| 13519 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 13520 | #endif /* ST_APPEND */ |
| 13521 | #ifdef ST_NOATIME |
| 13522 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 13523 | #endif /* ST_NOATIME */ |
| 13524 | #ifdef ST_NODIRATIME |
| 13525 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 13526 | #endif /* ST_NODIRATIME */ |
| 13527 | #ifdef ST_RELATIME |
| 13528 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 13529 | #endif /* ST_RELATIME */ |
| 13530 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13531 | /* FreeBSD sendfile() constants */ |
| 13532 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13533 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13534 | #endif |
| 13535 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13536 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13537 | #endif |
| 13538 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13539 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13540 | #endif |
| 13541 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13542 | /* constants for posix_fadvise */ |
| 13543 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13544 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13545 | #endif |
| 13546 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13547 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13548 | #endif |
| 13549 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13550 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13551 | #endif |
| 13552 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13553 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13554 | #endif |
| 13555 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13556 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13557 | #endif |
| 13558 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13559 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13560 | #endif |
| 13561 | |
| 13562 | /* constants for waitid */ |
| 13563 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13564 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 13565 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 13566 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13567 | #endif |
| 13568 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13569 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13570 | #endif |
| 13571 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13572 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13573 | #endif |
| 13574 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13575 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13576 | #endif |
| 13577 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13578 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13579 | #endif |
| 13580 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13581 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13582 | #endif |
| 13583 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13584 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13585 | #endif |
| 13586 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13587 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13588 | #endif |
| 13589 | |
| 13590 | /* constants for lockf */ |
| 13591 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13592 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13593 | #endif |
| 13594 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13595 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13596 | #endif |
| 13597 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13598 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13599 | #endif |
| 13600 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13601 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13602 | #endif |
| 13603 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13604 | #ifdef RWF_DSYNC |
| 13605 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 13606 | #endif |
| 13607 | #ifdef RWF_HIPRI |
| 13608 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 13609 | #endif |
| 13610 | #ifdef RWF_SYNC |
| 13611 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 13612 | #endif |
| 13613 | #ifdef RWF_NOWAIT |
| 13614 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 13615 | #endif |
| 13616 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13617 | /* constants for posix_spawn */ |
| 13618 | #ifdef HAVE_POSIX_SPAWN |
| 13619 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 13620 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 13621 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 13622 | #endif |
| 13623 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 13624 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13625 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 13626 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 13627 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 13628 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 13629 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 13630 | #endif |
| 13631 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13632 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13633 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13634 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13635 | #endif |
| 13636 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13637 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13638 | #endif |
| 13639 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13640 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13641 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13642 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 13643 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13644 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13645 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13646 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13647 | #endif |
| 13648 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13649 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13650 | #endif |
| 13651 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13652 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13653 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13654 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13655 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13656 | #endif |
| 13657 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13658 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13659 | #endif |
| 13660 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13661 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13662 | #endif |
| 13663 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13664 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13665 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13666 | #endif |
| 13667 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 13668 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13669 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 13670 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 13671 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 13672 | #endif |
| 13673 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13674 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13675 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13676 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13677 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13678 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13679 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13680 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13681 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13682 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13683 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13684 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13685 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13686 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13687 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13688 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13689 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13690 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13691 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13692 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13693 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13694 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 13695 | #if HAVE_DECL_RTLD_MEMBER |
| 13696 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 13697 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13698 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13699 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13700 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 13701 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 13702 | #endif |
| 13703 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 13704 | #if defined(__APPLE__) |
| 13705 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 13706 | #endif |
| 13707 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13708 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13709 | } |
| 13710 | |
| 13711 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 13712 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13713 | PyModuleDef_HEAD_INIT, |
| 13714 | MODNAME, |
| 13715 | posix__doc__, |
| 13716 | -1, |
| 13717 | posix_methods, |
| 13718 | NULL, |
| 13719 | NULL, |
| 13720 | NULL, |
| 13721 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 13722 | }; |
| 13723 | |
| 13724 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 13725 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13726 | |
| 13727 | #ifdef HAVE_FACCESSAT |
| 13728 | "HAVE_FACCESSAT", |
| 13729 | #endif |
| 13730 | |
| 13731 | #ifdef HAVE_FCHDIR |
| 13732 | "HAVE_FCHDIR", |
| 13733 | #endif |
| 13734 | |
| 13735 | #ifdef HAVE_FCHMOD |
| 13736 | "HAVE_FCHMOD", |
| 13737 | #endif |
| 13738 | |
| 13739 | #ifdef HAVE_FCHMODAT |
| 13740 | "HAVE_FCHMODAT", |
| 13741 | #endif |
| 13742 | |
| 13743 | #ifdef HAVE_FCHOWN |
| 13744 | "HAVE_FCHOWN", |
| 13745 | #endif |
| 13746 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 13747 | #ifdef HAVE_FCHOWNAT |
| 13748 | "HAVE_FCHOWNAT", |
| 13749 | #endif |
| 13750 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13751 | #ifdef HAVE_FEXECVE |
| 13752 | "HAVE_FEXECVE", |
| 13753 | #endif |
| 13754 | |
| 13755 | #ifdef HAVE_FDOPENDIR |
| 13756 | "HAVE_FDOPENDIR", |
| 13757 | #endif |
| 13758 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 13759 | #ifdef HAVE_FPATHCONF |
| 13760 | "HAVE_FPATHCONF", |
| 13761 | #endif |
| 13762 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13763 | #ifdef HAVE_FSTATAT |
| 13764 | "HAVE_FSTATAT", |
| 13765 | #endif |
| 13766 | |
| 13767 | #ifdef HAVE_FSTATVFS |
| 13768 | "HAVE_FSTATVFS", |
| 13769 | #endif |
| 13770 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 13771 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 13772 | "HAVE_FTRUNCATE", |
| 13773 | #endif |
| 13774 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13775 | #ifdef HAVE_FUTIMENS |
| 13776 | "HAVE_FUTIMENS", |
| 13777 | #endif |
| 13778 | |
| 13779 | #ifdef HAVE_FUTIMES |
| 13780 | "HAVE_FUTIMES", |
| 13781 | #endif |
| 13782 | |
| 13783 | #ifdef HAVE_FUTIMESAT |
| 13784 | "HAVE_FUTIMESAT", |
| 13785 | #endif |
| 13786 | |
| 13787 | #ifdef HAVE_LINKAT |
| 13788 | "HAVE_LINKAT", |
| 13789 | #endif |
| 13790 | |
| 13791 | #ifdef HAVE_LCHFLAGS |
| 13792 | "HAVE_LCHFLAGS", |
| 13793 | #endif |
| 13794 | |
| 13795 | #ifdef HAVE_LCHMOD |
| 13796 | "HAVE_LCHMOD", |
| 13797 | #endif |
| 13798 | |
| 13799 | #ifdef HAVE_LCHOWN |
| 13800 | "HAVE_LCHOWN", |
| 13801 | #endif |
| 13802 | |
| 13803 | #ifdef HAVE_LSTAT |
| 13804 | "HAVE_LSTAT", |
| 13805 | #endif |
| 13806 | |
| 13807 | #ifdef HAVE_LUTIMES |
| 13808 | "HAVE_LUTIMES", |
| 13809 | #endif |
| 13810 | |
| 13811 | #ifdef HAVE_MKDIRAT |
| 13812 | "HAVE_MKDIRAT", |
| 13813 | #endif |
| 13814 | |
| 13815 | #ifdef HAVE_MKFIFOAT |
| 13816 | "HAVE_MKFIFOAT", |
| 13817 | #endif |
| 13818 | |
| 13819 | #ifdef HAVE_MKNODAT |
| 13820 | "HAVE_MKNODAT", |
| 13821 | #endif |
| 13822 | |
| 13823 | #ifdef HAVE_OPENAT |
| 13824 | "HAVE_OPENAT", |
| 13825 | #endif |
| 13826 | |
| 13827 | #ifdef HAVE_READLINKAT |
| 13828 | "HAVE_READLINKAT", |
| 13829 | #endif |
| 13830 | |
| 13831 | #ifdef HAVE_RENAMEAT |
| 13832 | "HAVE_RENAMEAT", |
| 13833 | #endif |
| 13834 | |
| 13835 | #ifdef HAVE_SYMLINKAT |
| 13836 | "HAVE_SYMLINKAT", |
| 13837 | #endif |
| 13838 | |
| 13839 | #ifdef HAVE_UNLINKAT |
| 13840 | "HAVE_UNLINKAT", |
| 13841 | #endif |
| 13842 | |
| 13843 | #ifdef HAVE_UTIMENSAT |
| 13844 | "HAVE_UTIMENSAT", |
| 13845 | #endif |
| 13846 | |
| 13847 | #ifdef MS_WINDOWS |
| 13848 | "MS_WINDOWS", |
| 13849 | #endif |
| 13850 | |
| 13851 | NULL |
| 13852 | }; |
| 13853 | |
| 13854 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 13855 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 13856 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13857 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13858 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13859 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 13860 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13861 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13862 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13863 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13864 | #endif |
| 13865 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13866 | m = PyModule_Create(&posixmodule); |
| 13867 | if (m == NULL) |
| 13868 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13869 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13870 | /* Initialize environ dictionary */ |
| 13871 | v = convertenviron(); |
| 13872 | Py_XINCREF(v); |
| 13873 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 13874 | return NULL; |
| 13875 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13876 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13877 | if (all_ins(m)) |
| 13878 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13879 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13880 | if (setup_confname_tables(m)) |
| 13881 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 13882 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13883 | Py_INCREF(PyExc_OSError); |
| 13884 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 13885 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 13886 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13887 | if (posix_putenv_garbage == NULL) |
| 13888 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 13889 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 13890 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13891 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13892 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 13893 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13894 | WaitidResultType = PyStructSequence_NewType(&waitid_result_desc); |
| 13895 | if (WaitidResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13896 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13897 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13898 | #endif |
| 13899 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 13900 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13901 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 13902 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 13903 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13904 | StatResultType = PyStructSequence_NewType(&stat_result_desc); |
| 13905 | if (StatResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13906 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13907 | } |
| 13908 | structseq_new = StatResultType->tp_new; |
| 13909 | StatResultType->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13910 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 13911 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13912 | StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc); |
| 13913 | if (StatVFSResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13914 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13915 | } |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13916 | #ifdef NEED_TICKS_PER_SECOND |
| 13917 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13918 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13919 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13920 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13921 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13922 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13923 | # endif |
| 13924 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13925 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 13926 | #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] | 13927 | sched_param_desc.name = MODNAME ".sched_param"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13928 | SchedParamType = PyStructSequence_NewType(&sched_param_desc); |
| 13929 | if (SchedParamType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13930 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13931 | } |
| 13932 | SchedParamType->tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13933 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13934 | |
| 13935 | /* initialize TerminalSize_info */ |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13936 | TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc); |
| 13937 | if (TerminalSizeType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13938 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13939 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13940 | |
| 13941 | /* initialize scandir types */ |
| 13942 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 13943 | return NULL; |
| 13944 | if (PyType_Ready(&DirEntryType) < 0) |
| 13945 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13946 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13947 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13948 | Py_INCREF((PyObject*) WaitidResultType); |
| 13949 | PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13950 | #endif |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13951 | Py_INCREF((PyObject*) StatResultType); |
| 13952 | PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType); |
| 13953 | Py_INCREF((PyObject*) StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13954 | PyModule_AddObject(m, "statvfs_result", |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13955 | (PyObject*) StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 13956 | |
| 13957 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13958 | Py_INCREF(SchedParamType); |
| 13959 | PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 13960 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13961 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13962 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13963 | TimesResultType = PyStructSequence_NewType(×_result_desc); |
| 13964 | if (TimesResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13965 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13966 | } |
| 13967 | PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13968 | |
| 13969 | uname_result_desc.name = MODNAME ".uname_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13970 | UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
| 13971 | if (UnameResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13972 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 13973 | } |
| 13974 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13975 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13976 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13977 | /* |
| 13978 | * Step 2 of weak-linking support on Mac OS X. |
| 13979 | * |
| 13980 | * The code below removes functions that are not available on the |
| 13981 | * currently active platform. |
| 13982 | * |
| 13983 | * 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] | 13984 | * 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] | 13985 | * OSX 10.4. |
| 13986 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13987 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13988 | if (fstatvfs == NULL) { |
| 13989 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 13990 | return NULL; |
| 13991 | } |
| 13992 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13993 | #endif /* HAVE_FSTATVFS */ |
| 13994 | |
| 13995 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13996 | if (statvfs == NULL) { |
| 13997 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 13998 | return NULL; |
| 13999 | } |
| 14000 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14001 | #endif /* HAVE_STATVFS */ |
| 14002 | |
| 14003 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14004 | if (lchown == NULL) { |
| 14005 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 14006 | return NULL; |
| 14007 | } |
| 14008 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14009 | #endif /* HAVE_LCHOWN */ |
| 14010 | |
| 14011 | |
| 14012 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14013 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14014 | Py_INCREF(TerminalSizeType); |
| 14015 | PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14016 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 14017 | billion = PyLong_FromLong(1000000000); |
| 14018 | if (!billion) |
| 14019 | return NULL; |
| 14020 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14021 | /* suppress "function not used" warnings */ |
| 14022 | { |
| 14023 | int ignored; |
| 14024 | fd_specified("", -1); |
| 14025 | follow_symlinks_specified("", 1); |
| 14026 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 14027 | dir_fd_converter(Py_None, &ignored); |
| 14028 | dir_fd_unavailable(Py_None, &ignored); |
| 14029 | } |
| 14030 | |
| 14031 | /* |
| 14032 | * provide list of locally available functions |
| 14033 | * so os.py can populate support_* lists |
| 14034 | */ |
| 14035 | list = PyList_New(0); |
| 14036 | if (!list) |
| 14037 | return NULL; |
| 14038 | for (trace = have_functions; *trace; trace++) { |
| 14039 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 14040 | if (!unicode) |
| 14041 | return NULL; |
| 14042 | if (PyList_Append(list, unicode)) |
| 14043 | return NULL; |
| 14044 | Py_DECREF(unicode); |
| 14045 | } |
| 14046 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 14047 | |
| 14048 | Py_INCREF((PyObject *) &DirEntryType); |
Brett Cannon | a32c4d0 | 2016-06-24 14:14:44 -0700 | [diff] [blame] | 14049 | PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14050 | |
| 14051 | initialized = 1; |
| 14052 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14053 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14054 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14055 | |
| 14056 | #ifdef __cplusplus |
| 14057 | } |
| 14058 | #endif |