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 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 35 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 36 | /* On android API level 21, 'AT_EACCESS' is not declared although |
| 37 | * HAVE_FACCESSAT is defined. */ |
| 38 | #ifdef __ANDROID__ |
| 39 | #undef HAVE_FACCESSAT |
| 40 | #endif |
| 41 | |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | fa76eee | 2016-05-28 21:03:48 +0000 | [diff] [blame] | 42 | #include <stdio.h> /* needed for ctermid() */ |
| 43 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 48 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 49 | "This module provides access to operating system functionality that is\n\ |
| 50 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 51 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 52 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 53 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 54 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 55 | #ifdef HAVE_SYS_UIO_H |
| 56 | #include <sys/uio.h> |
| 57 | #endif |
| 58 | |
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 59 | #ifdef HAVE_SYS_SYSMACROS_H |
| 60 | /* GNU C Library: major(), minor(), makedev() */ |
| 61 | #include <sys/sysmacros.h> |
| 62 | #endif |
| 63 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 65 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | #endif /* HAVE_SYS_TYPES_H */ |
| 67 | |
| 68 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 69 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 70 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 72 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 73 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 74 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 75 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 77 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 80 | #ifdef HAVE_FCNTL_H |
| 81 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 82 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 84 | #ifdef HAVE_GRP_H |
| 85 | #include <grp.h> |
| 86 | #endif |
| 87 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 88 | #ifdef HAVE_SYSEXITS_H |
| 89 | #include <sysexits.h> |
| 90 | #endif /* HAVE_SYSEXITS_H */ |
| 91 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 92 | #ifdef HAVE_SYS_LOADAVG_H |
| 93 | #include <sys/loadavg.h> |
| 94 | #endif |
| 95 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 96 | #ifdef HAVE_SYS_SENDFILE_H |
| 97 | #include <sys/sendfile.h> |
| 98 | #endif |
| 99 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 100 | #ifdef HAVE_SCHED_H |
| 101 | #include <sched.h> |
| 102 | #endif |
| 103 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 104 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 105 | #undef HAVE_SCHED_SETAFFINITY |
| 106 | #endif |
| 107 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 108 | #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] | 109 | #define USE_XATTRS |
| 110 | #endif |
| 111 | |
| 112 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 113 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 114 | #endif |
| 115 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 116 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 117 | #ifdef HAVE_SYS_SOCKET_H |
| 118 | #include <sys/socket.h> |
| 119 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 120 | #endif |
| 121 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 122 | #ifdef HAVE_DLFCN_H |
| 123 | #include <dlfcn.h> |
| 124 | #endif |
| 125 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 126 | #ifdef __hpux |
| 127 | #include <sys/mpctl.h> |
| 128 | #endif |
| 129 | |
| 130 | #if defined(__DragonFly__) || \ |
| 131 | defined(__OpenBSD__) || \ |
| 132 | defined(__FreeBSD__) || \ |
| 133 | defined(__NetBSD__) || \ |
| 134 | defined(__APPLE__) |
| 135 | #include <sys/sysctl.h> |
| 136 | #endif |
| 137 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 138 | #ifdef HAVE_LINUX_RANDOM_H |
| 139 | # include <linux/random.h> |
| 140 | #endif |
| 141 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 142 | # include <sys/syscall.h> |
| 143 | #endif |
| 144 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 145 | #if defined(MS_WINDOWS) |
| 146 | # define TERMSIZE_USE_CONIO |
| 147 | #elif defined(HAVE_SYS_IOCTL_H) |
| 148 | # include <sys/ioctl.h> |
| 149 | # if defined(HAVE_TERMIOS_H) |
| 150 | # include <termios.h> |
| 151 | # endif |
| 152 | # if defined(TIOCGWINSZ) |
| 153 | # define TERMSIZE_USE_IOCTL |
| 154 | # endif |
| 155 | #endif /* MS_WINDOWS */ |
| 156 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 157 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 158 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 159 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 160 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 161 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 162 | #include <process.h> |
| 163 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 164 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 165 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 166 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 167 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 168 | #define HAVE_EXECV 1 |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 169 | #define HAVE_WSPAWNV 1 |
| 170 | #define HAVE_WEXECV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 171 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 172 | #define HAVE_SYSTEM 1 |
| 173 | #define HAVE_CWAIT 1 |
| 174 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 175 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 176 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 177 | /* Unix functions that the configure script doesn't check for */ |
| 178 | #define HAVE_EXECV 1 |
| 179 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 180 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 181 | #define HAVE_FORK1 1 |
| 182 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 183 | #define HAVE_GETEGID 1 |
| 184 | #define HAVE_GETEUID 1 |
| 185 | #define HAVE_GETGID 1 |
| 186 | #define HAVE_GETPPID 1 |
| 187 | #define HAVE_GETUID 1 |
| 188 | #define HAVE_KILL 1 |
| 189 | #define HAVE_OPENDIR 1 |
| 190 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 191 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 192 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 193 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 194 | #endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 195 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 196 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 197 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 198 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 199 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 200 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 201 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 202 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 203 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 204 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 205 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 206 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 207 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 208 | (default) */ |
| 209 | extern char *ctermid_r(char *); |
| 210 | #endif |
| 211 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 212 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 213 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 214 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 215 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 216 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 217 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 218 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 219 | extern int chdir(char *); |
| 220 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 221 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 222 | extern int chdir(const char *); |
| 223 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 224 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 225 | extern int chmod(const char *, mode_t); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 226 | /*#ifdef HAVE_FCHMOD |
| 227 | extern int fchmod(int, mode_t); |
| 228 | #endif*/ |
| 229 | /*#ifdef HAVE_LCHMOD |
| 230 | extern int lchmod(const char *, mode_t); |
| 231 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 232 | extern int chown(const char *, uid_t, gid_t); |
| 233 | extern char *getcwd(char *, int); |
| 234 | extern char *strerror(int); |
| 235 | extern int link(const char *, const char *); |
| 236 | extern int rename(const char *, const char *); |
| 237 | extern int stat(const char *, struct stat *); |
| 238 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 240 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 241 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 243 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 244 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 246 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 247 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 248 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 249 | #ifdef HAVE_POSIX_SPAWN |
| 250 | #include <spawn.h> |
| 251 | #endif |
| 252 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 253 | #ifdef HAVE_UTIME_H |
| 254 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 255 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 257 | #ifdef HAVE_SYS_UTIME_H |
| 258 | #include <sys/utime.h> |
| 259 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 260 | #endif /* HAVE_SYS_UTIME_H */ |
| 261 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 262 | #ifdef HAVE_SYS_TIMES_H |
| 263 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 264 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | |
| 266 | #ifdef HAVE_SYS_PARAM_H |
| 267 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 268 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 269 | |
| 270 | #ifdef HAVE_SYS_UTSNAME_H |
| 271 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 272 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 274 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 275 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 276 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 277 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 278 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 279 | #include <direct.h> |
| 280 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 281 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 282 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 283 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 284 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 285 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 286 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 287 | #endif |
| 288 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 289 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 290 | #endif |
| 291 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 292 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 293 | #endif |
| 294 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 296 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 297 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 298 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 299 | #endif |
| 300 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 301 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 302 | #endif |
| 303 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 304 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 305 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 306 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 307 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 308 | #endif |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 309 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 310 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 311 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 312 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 313 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 314 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 315 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 316 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 317 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 318 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 319 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 320 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 321 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 322 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 323 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 324 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 325 | #define MAXPATHLEN PATH_MAX |
| 326 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 327 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 328 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 329 | #endif /* MAXPATHLEN */ |
| 330 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 331 | #ifdef UNION_WAIT |
| 332 | /* Emulate some macros on systems that have a union instead of macros */ |
| 333 | |
| 334 | #ifndef WIFEXITED |
| 335 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 336 | #endif |
| 337 | |
| 338 | #ifndef WEXITSTATUS |
| 339 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 340 | #endif |
| 341 | |
| 342 | #ifndef WTERMSIG |
| 343 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 344 | #endif |
| 345 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 346 | #define WAIT_TYPE union wait |
| 347 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 348 | |
| 349 | #else /* !UNION_WAIT */ |
| 350 | #define WAIT_TYPE int |
| 351 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 352 | #endif /* UNION_WAIT */ |
| 353 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 354 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 355 | prototype for it, at least on Solaris -- maybe others as well?). */ |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 356 | #if defined(HAVE_CTERMID_R) |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 357 | #define USE_CTERMID_R |
| 358 | #endif |
| 359 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 360 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 361 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 362 | #undef FSTAT |
| 363 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 364 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 365 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 366 | # define LSTAT win32_lstat |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 367 | # define FSTAT _Py_fstat_noraise |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 368 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 369 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 370 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 371 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 372 | # define FSTAT fstat |
| 373 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 374 | #endif |
| 375 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 376 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 377 | #include <sys/mkdev.h> |
| 378 | #else |
| 379 | #if defined(MAJOR_IN_SYSMACROS) |
| 380 | #include <sys/sysmacros.h> |
| 381 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 382 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 383 | #include <sys/mkdev.h> |
| 384 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 385 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 386 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 387 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 388 | #define INITFUNC PyInit_nt |
| 389 | #define MODNAME "nt" |
| 390 | #else |
| 391 | #define INITFUNC PyInit_posix |
| 392 | #define MODNAME "posix" |
| 393 | #endif |
| 394 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 395 | #if defined(__sun) |
| 396 | /* Something to implement in autoconf, not present in autoconf 2.69 */ |
| 397 | #define HAVE_STRUCT_STAT_ST_FSTYPE 1 |
| 398 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 399 | |
| 400 | #ifdef HAVE_FORK |
| 401 | static void |
| 402 | run_at_forkers(PyObject *lst, int reverse) |
| 403 | { |
| 404 | Py_ssize_t i; |
| 405 | PyObject *cpy; |
| 406 | |
| 407 | if (lst != NULL) { |
| 408 | assert(PyList_CheckExact(lst)); |
| 409 | |
| 410 | /* Use a list copy in case register_at_fork() is called from |
| 411 | * one of the callbacks. |
| 412 | */ |
| 413 | cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst)); |
| 414 | if (cpy == NULL) |
| 415 | PyErr_WriteUnraisable(lst); |
| 416 | else { |
| 417 | if (reverse) |
| 418 | PyList_Reverse(cpy); |
| 419 | for (i = 0; i < PyList_GET_SIZE(cpy); i++) { |
| 420 | PyObject *func, *res; |
| 421 | func = PyList_GET_ITEM(cpy, i); |
| 422 | res = PyObject_CallObject(func, NULL); |
| 423 | if (res == NULL) |
| 424 | PyErr_WriteUnraisable(func); |
| 425 | else |
| 426 | Py_DECREF(res); |
| 427 | } |
| 428 | Py_DECREF(cpy); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void |
| 434 | PyOS_BeforeFork(void) |
| 435 | { |
| 436 | run_at_forkers(PyThreadState_Get()->interp->before_forkers, 1); |
| 437 | |
| 438 | _PyImport_AcquireLock(); |
| 439 | } |
| 440 | |
| 441 | void |
| 442 | PyOS_AfterFork_Parent(void) |
| 443 | { |
| 444 | if (_PyImport_ReleaseLock() <= 0) |
| 445 | Py_FatalError("failed releasing import lock after fork"); |
| 446 | |
| 447 | run_at_forkers(PyThreadState_Get()->interp->after_forkers_parent, 0); |
| 448 | } |
| 449 | |
| 450 | void |
| 451 | PyOS_AfterFork_Child(void) |
| 452 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 453 | _PyGILState_Reinit(); |
| 454 | PyEval_ReInitThreads(); |
| 455 | _PyImport_ReInitLock(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 456 | _PySignal_AfterFork(); |
| 457 | |
| 458 | run_at_forkers(PyThreadState_Get()->interp->after_forkers_child, 0); |
| 459 | } |
| 460 | |
| 461 | static int |
| 462 | register_at_forker(PyObject **lst, PyObject *func) |
| 463 | { |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 464 | if (func == NULL) /* nothing to register? do nothing. */ |
| 465 | return 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 466 | if (*lst == NULL) { |
| 467 | *lst = PyList_New(0); |
| 468 | if (*lst == NULL) |
| 469 | return -1; |
| 470 | } |
| 471 | return PyList_Append(*lst, func); |
| 472 | } |
| 473 | #endif |
| 474 | |
| 475 | /* Legacy wrapper */ |
| 476 | void |
| 477 | PyOS_AfterFork(void) |
| 478 | { |
| 479 | #ifdef HAVE_FORK |
| 480 | PyOS_AfterFork_Child(); |
| 481 | #endif |
| 482 | } |
| 483 | |
| 484 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 485 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 486 | /* defined in fileutils.c */ |
| 487 | PyAPI_FUNC(void) _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 488 | PyAPI_FUNC(void) _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
| 489 | ULONG, struct _Py_stat_struct *); |
| 490 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 491 | |
| 492 | #ifdef MS_WINDOWS |
| 493 | static int |
| 494 | win32_warn_bytes_api() |
| 495 | { |
| 496 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 497 | "The Windows bytes API has been deprecated, " |
| 498 | "use Unicode filenames instead", |
| 499 | 1); |
| 500 | } |
| 501 | #endif |
| 502 | |
| 503 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 504 | #ifndef MS_WINDOWS |
| 505 | PyObject * |
| 506 | _PyLong_FromUid(uid_t uid) |
| 507 | { |
| 508 | if (uid == (uid_t)-1) |
| 509 | return PyLong_FromLong(-1); |
| 510 | return PyLong_FromUnsignedLong(uid); |
| 511 | } |
| 512 | |
| 513 | PyObject * |
| 514 | _PyLong_FromGid(gid_t gid) |
| 515 | { |
| 516 | if (gid == (gid_t)-1) |
| 517 | return PyLong_FromLong(-1); |
| 518 | return PyLong_FromUnsignedLong(gid); |
| 519 | } |
| 520 | |
| 521 | int |
| 522 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 523 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 524 | uid_t uid; |
| 525 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 526 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 527 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 528 | unsigned long uresult; |
| 529 | |
| 530 | index = PyNumber_Index(obj); |
| 531 | if (index == NULL) { |
| 532 | PyErr_Format(PyExc_TypeError, |
| 533 | "uid should be integer, not %.200s", |
| 534 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 535 | return 0; |
| 536 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 537 | |
| 538 | /* |
| 539 | * Handling uid_t is complicated for two reasons: |
| 540 | * * Although uid_t is (always?) unsigned, it still |
| 541 | * accepts -1. |
| 542 | * * We don't know its size in advance--it may be |
| 543 | * bigger than an int, or it may be smaller than |
| 544 | * a long. |
| 545 | * |
| 546 | * So a bit of defensive programming is in order. |
| 547 | * Start with interpreting the value passed |
| 548 | * in as a signed long and see if it works. |
| 549 | */ |
| 550 | |
| 551 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 552 | |
| 553 | if (!overflow) { |
| 554 | uid = (uid_t)result; |
| 555 | |
| 556 | if (result == -1) { |
| 557 | if (PyErr_Occurred()) |
| 558 | goto fail; |
| 559 | /* It's a legitimate -1, we're done. */ |
| 560 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 561 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 562 | |
| 563 | /* Any other negative number is disallowed. */ |
| 564 | if (result < 0) |
| 565 | goto underflow; |
| 566 | |
| 567 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 568 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 569 | (long)uid != result) |
| 570 | goto underflow; |
| 571 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 572 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 573 | |
| 574 | if (overflow < 0) |
| 575 | goto underflow; |
| 576 | |
| 577 | /* |
| 578 | * Okay, the value overflowed a signed long. If it |
| 579 | * fits in an *unsigned* long, it may still be okay, |
| 580 | * as uid_t may be unsigned long on this platform. |
| 581 | */ |
| 582 | uresult = PyLong_AsUnsignedLong(index); |
| 583 | if (PyErr_Occurred()) { |
| 584 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 585 | goto overflow; |
| 586 | goto fail; |
| 587 | } |
| 588 | |
| 589 | uid = (uid_t)uresult; |
| 590 | |
| 591 | /* |
| 592 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 593 | * but this value would get interpreted as (uid_t)-1 by chown |
| 594 | * and its siblings. That's not what the user meant! So we |
| 595 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 596 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 597 | */ |
| 598 | if (uid == (uid_t)-1) |
| 599 | goto overflow; |
| 600 | |
| 601 | /* Ensure the value wasn't truncated. */ |
| 602 | if (sizeof(uid_t) < sizeof(long) && |
| 603 | (unsigned long)uid != uresult) |
| 604 | goto overflow; |
| 605 | /* fallthrough */ |
| 606 | |
| 607 | success: |
| 608 | Py_DECREF(index); |
| 609 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 610 | return 1; |
| 611 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 612 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 613 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 614 | "uid is less than minimum"); |
| 615 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 616 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 617 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 618 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 619 | "uid is greater than maximum"); |
| 620 | /* fallthrough */ |
| 621 | |
| 622 | fail: |
| 623 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | int |
| 628 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 629 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 630 | gid_t gid; |
| 631 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 632 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 633 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 634 | unsigned long uresult; |
| 635 | |
| 636 | index = PyNumber_Index(obj); |
| 637 | if (index == NULL) { |
| 638 | PyErr_Format(PyExc_TypeError, |
| 639 | "gid should be integer, not %.200s", |
| 640 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 641 | return 0; |
| 642 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * Handling gid_t is complicated for two reasons: |
| 646 | * * Although gid_t is (always?) unsigned, it still |
| 647 | * accepts -1. |
| 648 | * * We don't know its size in advance--it may be |
| 649 | * bigger than an int, or it may be smaller than |
| 650 | * a long. |
| 651 | * |
| 652 | * So a bit of defensive programming is in order. |
| 653 | * Start with interpreting the value passed |
| 654 | * in as a signed long and see if it works. |
| 655 | */ |
| 656 | |
| 657 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 658 | |
| 659 | if (!overflow) { |
| 660 | gid = (gid_t)result; |
| 661 | |
| 662 | if (result == -1) { |
| 663 | if (PyErr_Occurred()) |
| 664 | goto fail; |
| 665 | /* It's a legitimate -1, we're done. */ |
| 666 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 667 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 668 | |
| 669 | /* Any other negative number is disallowed. */ |
| 670 | if (result < 0) { |
| 671 | goto underflow; |
| 672 | } |
| 673 | |
| 674 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 675 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 676 | (long)gid != result) |
| 677 | goto underflow; |
| 678 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 679 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 680 | |
| 681 | if (overflow < 0) |
| 682 | goto underflow; |
| 683 | |
| 684 | /* |
| 685 | * Okay, the value overflowed a signed long. If it |
| 686 | * fits in an *unsigned* long, it may still be okay, |
| 687 | * as gid_t may be unsigned long on this platform. |
| 688 | */ |
| 689 | uresult = PyLong_AsUnsignedLong(index); |
| 690 | if (PyErr_Occurred()) { |
| 691 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 692 | goto overflow; |
| 693 | goto fail; |
| 694 | } |
| 695 | |
| 696 | gid = (gid_t)uresult; |
| 697 | |
| 698 | /* |
| 699 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 700 | * but this value would get interpreted as (gid_t)-1 by chown |
| 701 | * and its siblings. That's not what the user meant! So we |
| 702 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 703 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 704 | */ |
| 705 | if (gid == (gid_t)-1) |
| 706 | goto overflow; |
| 707 | |
| 708 | /* Ensure the value wasn't truncated. */ |
| 709 | if (sizeof(gid_t) < sizeof(long) && |
| 710 | (unsigned long)gid != uresult) |
| 711 | goto overflow; |
| 712 | /* fallthrough */ |
| 713 | |
| 714 | success: |
| 715 | Py_DECREF(index); |
| 716 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 717 | return 1; |
| 718 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 719 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 720 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 721 | "gid is less than minimum"); |
| 722 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 723 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 724 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 725 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 726 | "gid is greater than maximum"); |
| 727 | /* fallthrough */ |
| 728 | |
| 729 | fail: |
| 730 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 731 | return 0; |
| 732 | } |
| 733 | #endif /* MS_WINDOWS */ |
| 734 | |
| 735 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 736 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 737 | |
| 738 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 739 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 740 | static int |
| 741 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 742 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 743 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 744 | if (PyErr_Occurred()) |
| 745 | return 0; |
| 746 | return 1; |
| 747 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 748 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 749 | |
| 750 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 751 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 752 | /* |
| 753 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 754 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 755 | * which doesn't play nicely with all the initializer lines in this file that |
| 756 | * look like this: |
| 757 | * int dir_fd = DEFAULT_DIR_FD; |
| 758 | */ |
| 759 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 760 | #else |
| 761 | #define DEFAULT_DIR_FD (-100) |
| 762 | #endif |
| 763 | |
| 764 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 765 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 766 | { |
| 767 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 768 | long long_value; |
| 769 | |
| 770 | PyObject *index = PyNumber_Index(o); |
| 771 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 772 | return 0; |
| 773 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 774 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 775 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 776 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 777 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 778 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 779 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 780 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 781 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 782 | return 0; |
| 783 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 784 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 785 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 786 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 787 | return 0; |
| 788 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 789 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 790 | *p = (int)long_value; |
| 791 | return 1; |
| 792 | } |
| 793 | |
| 794 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 795 | dir_fd_converter(PyObject *o, void *p) |
| 796 | { |
| 797 | if (o == Py_None) { |
| 798 | *(int *)p = DEFAULT_DIR_FD; |
| 799 | return 1; |
| 800 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 801 | else if (PyIndex_Check(o)) { |
| 802 | return _fd_converter(o, (int *)p); |
| 803 | } |
| 804 | else { |
| 805 | PyErr_Format(PyExc_TypeError, |
| 806 | "argument should be integer or None, not %.200s", |
| 807 | Py_TYPE(o)->tp_name); |
| 808 | return 0; |
| 809 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 813 | /* |
| 814 | * A PyArg_ParseTuple "converter" function |
| 815 | * that handles filesystem paths in the manner |
| 816 | * preferred by the os module. |
| 817 | * |
| 818 | * path_converter accepts (Unicode) strings and their |
| 819 | * subclasses, and bytes and their subclasses. What |
| 820 | * it does with the argument depends on the platform: |
| 821 | * |
| 822 | * * On Windows, if we get a (Unicode) string we |
| 823 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 824 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 825 | * |
| 826 | * * On all other platforms, strings are encoded |
| 827 | * to bytes using PyUnicode_FSConverter, then we |
| 828 | * extract the char * from the bytes object and |
| 829 | * return that. |
| 830 | * |
| 831 | * path_converter also optionally accepts signed |
| 832 | * integers (representing open file descriptors) instead |
| 833 | * of path strings. |
| 834 | * |
| 835 | * Input fields: |
| 836 | * path.nullable |
| 837 | * If nonzero, the path is permitted to be None. |
| 838 | * path.allow_fd |
| 839 | * If nonzero, the path is permitted to be a file handle |
| 840 | * (a signed int) instead of a string. |
| 841 | * path.function_name |
| 842 | * If non-NULL, path_converter will use that as the name |
| 843 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 844 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 845 | * path.argument_name |
| 846 | * If non-NULL, path_converter will use that as the name |
| 847 | * of the parameter in error messages. |
| 848 | * (If path.argument_name is NULL it uses "path".) |
| 849 | * |
| 850 | * Output fields: |
| 851 | * path.wide |
| 852 | * Points to the path if it was expressed as Unicode |
| 853 | * and was not encoded. (Only used on Windows.) |
| 854 | * path.narrow |
| 855 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 856 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 857 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 858 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 859 | * path.fd |
| 860 | * Contains a file descriptor if path.accept_fd was true |
| 861 | * and the caller provided a signed integer instead of any |
| 862 | * sort of string. |
| 863 | * |
| 864 | * WARNING: if your "path" parameter is optional, and is |
| 865 | * unspecified, path_converter will never get called. |
| 866 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 867 | * yourself! |
| 868 | * path.length |
| 869 | * The length of the path in characters, if specified as |
| 870 | * a string. |
| 871 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 872 | * The original object passed in (if get a PathLike object, |
| 873 | * the result of PyOS_FSPath() is treated as the original object). |
| 874 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 875 | * path.cleanup |
| 876 | * For internal use only. May point to a temporary object. |
| 877 | * (Pay no attention to the man behind the curtain.) |
| 878 | * |
| 879 | * At most one of path.wide or path.narrow will be non-NULL. |
| 880 | * If path was None and path.nullable was set, |
| 881 | * or if path was an integer and path.allow_fd was set, |
| 882 | * both path.wide and path.narrow will be NULL |
| 883 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 884 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 885 | * path_converter takes care to not write to the path_t |
| 886 | * unless it's successful. However it must reset the |
| 887 | * "cleanup" field each time it's called. |
| 888 | * |
| 889 | * Use as follows: |
| 890 | * path_t path; |
| 891 | * memset(&path, 0, sizeof(path)); |
| 892 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 893 | * // ... use values from path ... |
| 894 | * path_cleanup(&path); |
| 895 | * |
| 896 | * (Note that if PyArg_Parse fails you don't need to call |
| 897 | * path_cleanup(). However it is safe to do so.) |
| 898 | */ |
| 899 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 900 | const char *function_name; |
| 901 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 902 | int nullable; |
| 903 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 904 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 905 | #ifdef MS_WINDOWS |
| 906 | BOOL narrow; |
| 907 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 908 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 909 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 910 | int fd; |
| 911 | Py_ssize_t length; |
| 912 | PyObject *object; |
| 913 | PyObject *cleanup; |
| 914 | } path_t; |
| 915 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 916 | #ifdef MS_WINDOWS |
| 917 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 918 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 919 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 920 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 921 | {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] | 922 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 923 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 924 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 925 | path_cleanup(path_t *path) |
| 926 | { |
| 927 | Py_CLEAR(path->object); |
| 928 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 932 | path_converter(PyObject *o, void *p) |
| 933 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 934 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 935 | PyObject *bytes = NULL; |
| 936 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 937 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 938 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 939 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 940 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 941 | const wchar_t *wide; |
| 942 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 943 | |
| 944 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 945 | PyErr_Format(exc, "%s%s" fmt, \ |
| 946 | path->function_name ? path->function_name : "", \ |
| 947 | path->function_name ? ": " : "", \ |
| 948 | path->argument_name ? path->argument_name : "path") |
| 949 | |
| 950 | /* Py_CLEANUP_SUPPORTED support */ |
| 951 | if (o == NULL) { |
| 952 | path_cleanup(path); |
| 953 | return 1; |
| 954 | } |
| 955 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 956 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 957 | path->object = path->cleanup = NULL; |
| 958 | /* path->object owns a reference to the original object */ |
| 959 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 960 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 961 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 962 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 963 | #ifdef MS_WINDOWS |
| 964 | path->narrow = FALSE; |
| 965 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 966 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 967 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 968 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 969 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 972 | /* Only call this here so that we don't treat the return value of |
| 973 | os.fspath() as an fd or buffer. */ |
| 974 | is_index = path->allow_fd && PyIndex_Check(o); |
| 975 | is_buffer = PyObject_CheckBuffer(o); |
| 976 | is_bytes = PyBytes_Check(o); |
| 977 | is_unicode = PyUnicode_Check(o); |
| 978 | |
| 979 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 980 | /* Inline PyOS_FSPath() for better error messages. */ |
| 981 | _Py_IDENTIFIER(__fspath__); |
| 982 | PyObject *func = NULL; |
| 983 | |
| 984 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 985 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 986 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 987 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 988 | /* still owns a reference to the original object */ |
| 989 | Py_DECREF(o); |
| 990 | o = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 991 | Py_DECREF(func); |
| 992 | if (NULL == o) { |
| 993 | goto error_exit; |
| 994 | } |
| 995 | else if (PyUnicode_Check(o)) { |
| 996 | is_unicode = 1; |
| 997 | } |
| 998 | else if (PyBytes_Check(o)) { |
| 999 | is_bytes = 1; |
| 1000 | } |
| 1001 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1002 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1007 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1008 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1009 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1010 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1011 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1012 | if (length > 32767) { |
| 1013 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1014 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1015 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1016 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1017 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1018 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1019 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1020 | |
| 1021 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1022 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1023 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1024 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1025 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1026 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1027 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1028 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1029 | #endif |
| 1030 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1031 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1032 | bytes = o; |
| 1033 | Py_INCREF(bytes); |
| 1034 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1035 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1036 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1037 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1038 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1039 | "%s%s%s should be %s, not %.200s", |
| 1040 | path->function_name ? path->function_name : "", |
| 1041 | path->function_name ? ": " : "", |
| 1042 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1043 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1044 | "integer or None" : |
| 1045 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1046 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1047 | "string, bytes or os.PathLike", |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1048 | Py_TYPE(o)->tp_name)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1049 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1050 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1051 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1052 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1053 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1054 | } |
| 1055 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1056 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1057 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1058 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1059 | } |
| 1060 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1061 | #ifdef MS_WINDOWS |
| 1062 | path->narrow = FALSE; |
| 1063 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1064 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1065 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1066 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1067 | } |
| 1068 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1069 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1070 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1071 | path->function_name ? path->function_name : "", |
| 1072 | path->function_name ? ": " : "", |
| 1073 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1074 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1075 | "integer or None" : |
| 1076 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1077 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1078 | "string, bytes or os.PathLike", |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1079 | Py_TYPE(o)->tp_name); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1080 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1083 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1084 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1085 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1086 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1087 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1090 | #ifdef MS_WINDOWS |
| 1091 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1092 | narrow, |
| 1093 | length |
| 1094 | ); |
| 1095 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1096 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1099 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1100 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1101 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1102 | } |
| 1103 | if (length > 32767) { |
| 1104 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1105 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1106 | } |
| 1107 | if (wcslen(wide) != length) { |
| 1108 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1109 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1110 | } |
| 1111 | path->wide = wide; |
| 1112 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1113 | path->cleanup = wo; |
| 1114 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1115 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1116 | path->wide = NULL; |
| 1117 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1118 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1119 | /* Still a reference owned by path->object, don't have to |
| 1120 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1121 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1122 | } |
| 1123 | else { |
| 1124 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1125 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1126 | #endif |
| 1127 | path->fd = -1; |
| 1128 | |
| 1129 | success_exit: |
| 1130 | path->length = length; |
| 1131 | path->object = o; |
| 1132 | return Py_CLEANUP_SUPPORTED; |
| 1133 | |
| 1134 | error_exit: |
| 1135 | Py_XDECREF(o); |
| 1136 | Py_XDECREF(bytes); |
| 1137 | #ifdef MS_WINDOWS |
| 1138 | Py_XDECREF(wo); |
| 1139 | #endif |
| 1140 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1144 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1145 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1146 | PyErr_Format(PyExc_NotImplementedError, |
| 1147 | "%s%s%s unavailable on this platform", |
| 1148 | (function_name != NULL) ? function_name : "", |
| 1149 | (function_name != NULL) ? ": ": "", |
| 1150 | argument_name); |
| 1151 | } |
| 1152 | |
| 1153 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1154 | dir_fd_unavailable(PyObject *o, void *p) |
| 1155 | { |
| 1156 | int dir_fd; |
| 1157 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1158 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1159 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1160 | argument_unavailable_error(NULL, "dir_fd"); |
| 1161 | return 0; |
| 1162 | } |
| 1163 | *(int *)p = dir_fd; |
| 1164 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1168 | fd_specified(const char *function_name, int fd) |
| 1169 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1170 | if (fd == -1) |
| 1171 | return 0; |
| 1172 | |
| 1173 | argument_unavailable_error(function_name, "fd"); |
| 1174 | return 1; |
| 1175 | } |
| 1176 | |
| 1177 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1178 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1179 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1180 | if (follow_symlinks) |
| 1181 | return 0; |
| 1182 | |
| 1183 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1184 | return 1; |
| 1185 | } |
| 1186 | |
| 1187 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1188 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1189 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1190 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1191 | #ifndef MS_WINDOWS |
| 1192 | && !path->narrow |
| 1193 | #endif |
| 1194 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1195 | PyErr_Format(PyExc_ValueError, |
| 1196 | "%s: can't specify dir_fd without matching path", |
| 1197 | function_name); |
| 1198 | return 1; |
| 1199 | } |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1204 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1205 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1206 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1207 | PyErr_Format(PyExc_ValueError, |
| 1208 | "%s: can't specify both dir_fd and fd", |
| 1209 | function_name); |
| 1210 | return 1; |
| 1211 | } |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1216 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1217 | int follow_symlinks) |
| 1218 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1219 | if ((fd > 0) && (!follow_symlinks)) { |
| 1220 | PyErr_Format(PyExc_ValueError, |
| 1221 | "%s: cannot use fd and follow_symlinks together", |
| 1222 | function_name); |
| 1223 | return 1; |
| 1224 | } |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
| 1228 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1229 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1230 | int follow_symlinks) |
| 1231 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1232 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1233 | PyErr_Format(PyExc_ValueError, |
| 1234 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1235 | function_name); |
| 1236 | return 1; |
| 1237 | } |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1241 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1242 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1243 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1244 | typedef off_t Py_off_t; |
| 1245 | #endif |
| 1246 | |
| 1247 | static int |
| 1248 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1249 | { |
| 1250 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1251 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1252 | #else |
| 1253 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1254 | #endif |
| 1255 | if (PyErr_Occurred()) |
| 1256 | return 0; |
| 1257 | return 1; |
| 1258 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1259 | |
| 1260 | static PyObject * |
| 1261 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1262 | { |
| 1263 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1264 | return PyLong_FromLongLong(offset); |
| 1265 | #else |
| 1266 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1267 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1268 | } |
| 1269 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1270 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1271 | |
| 1272 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1273 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1274 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1275 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1276 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1277 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1278 | |
| 1279 | if (0 == DeviceIoControl( |
| 1280 | reparse_point_handle, |
| 1281 | FSCTL_GET_REPARSE_POINT, |
| 1282 | NULL, 0, /* in buffer */ |
| 1283 | target_buffer, sizeof(target_buffer), |
| 1284 | &n_bytes_returned, |
| 1285 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1286 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1287 | |
| 1288 | if (reparse_tag) |
| 1289 | *reparse_tag = rdb->ReparseTag; |
| 1290 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1291 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1292 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1293 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1294 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1295 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1296 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1297 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1298 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1299 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1300 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1301 | */ |
| 1302 | #include <crt_externs.h> |
| 1303 | static char **environ; |
| 1304 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1305 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1306 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1307 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1308 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1309 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1310 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1311 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1312 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1313 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1314 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1315 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1316 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1317 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1318 | d = PyDict_New(); |
| 1319 | if (d == NULL) |
| 1320 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1321 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1322 | if (environ == NULL) |
| 1323 | environ = *_NSGetEnviron(); |
| 1324 | #endif |
| 1325 | #ifdef MS_WINDOWS |
| 1326 | /* _wenviron must be initialized in this way if the program is started |
| 1327 | through main() instead of wmain(). */ |
| 1328 | _wgetenv(L""); |
| 1329 | if (_wenviron == NULL) |
| 1330 | return d; |
| 1331 | /* This part ignores errors */ |
| 1332 | for (e = _wenviron; *e != NULL; e++) { |
| 1333 | PyObject *k; |
| 1334 | PyObject *v; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1335 | const wchar_t *p = wcschr(*e, L'='); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1336 | if (p == NULL) |
| 1337 | continue; |
| 1338 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1339 | if (k == NULL) { |
| 1340 | PyErr_Clear(); |
| 1341 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1342 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1343 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1344 | if (v == NULL) { |
| 1345 | PyErr_Clear(); |
| 1346 | Py_DECREF(k); |
| 1347 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1348 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1349 | if (PyDict_GetItem(d, k) == NULL) { |
| 1350 | if (PyDict_SetItem(d, k, v) != 0) |
| 1351 | PyErr_Clear(); |
| 1352 | } |
| 1353 | Py_DECREF(k); |
| 1354 | Py_DECREF(v); |
| 1355 | } |
| 1356 | #else |
| 1357 | if (environ == NULL) |
| 1358 | return d; |
| 1359 | /* This part ignores errors */ |
| 1360 | for (e = environ; *e != NULL; e++) { |
| 1361 | PyObject *k; |
| 1362 | PyObject *v; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1363 | const char *p = strchr(*e, '='); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1364 | if (p == NULL) |
| 1365 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1366 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1367 | if (k == NULL) { |
| 1368 | PyErr_Clear(); |
| 1369 | continue; |
| 1370 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1371 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1372 | if (v == NULL) { |
| 1373 | PyErr_Clear(); |
| 1374 | Py_DECREF(k); |
| 1375 | continue; |
| 1376 | } |
| 1377 | if (PyDict_GetItem(d, k) == NULL) { |
| 1378 | if (PyDict_SetItem(d, k, v) != 0) |
| 1379 | PyErr_Clear(); |
| 1380 | } |
| 1381 | Py_DECREF(k); |
| 1382 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1383 | } |
| 1384 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1385 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1388 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1389 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1390 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1391 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1392 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1393 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1394 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1395 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1396 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1397 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1398 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1399 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1400 | /* XXX We should pass the function name along in the future. |
| 1401 | (winreg.c also wants to pass the function name.) |
| 1402 | This would however require an additional param to the |
| 1403 | Windows error object, which is non-trivial. |
| 1404 | */ |
| 1405 | errno = GetLastError(); |
| 1406 | if (filename) |
| 1407 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1408 | else |
| 1409 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1410 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1411 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1412 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1413 | win32_error_object(const char* function, PyObject* filename) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1414 | { |
| 1415 | /* XXX - see win32_error for comments on 'function' */ |
| 1416 | errno = GetLastError(); |
| 1417 | if (filename) |
| 1418 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1419 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1420 | errno, |
| 1421 | filename); |
| 1422 | else |
| 1423 | return PyErr_SetFromWindowsErr(errno); |
| 1424 | } |
| 1425 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1426 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1427 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1428 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1429 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1430 | { |
| 1431 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1432 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1433 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1434 | #else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1435 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1436 | #endif |
| 1437 | } |
| 1438 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1439 | static PyObject * |
| 1440 | path_object_error2(PyObject *path, PyObject *path2) |
| 1441 | { |
| 1442 | #ifdef MS_WINDOWS |
| 1443 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1444 | PyExc_OSError, 0, path, path2); |
| 1445 | #else |
| 1446 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1447 | #endif |
| 1448 | } |
| 1449 | |
| 1450 | static PyObject * |
| 1451 | path_error(path_t *path) |
| 1452 | { |
| 1453 | return path_object_error(path->object); |
| 1454 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1455 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1456 | static PyObject * |
| 1457 | path_error2(path_t *path, path_t *path2) |
| 1458 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1459 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1463 | /* POSIX generic methods */ |
| 1464 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1465 | static int |
| 1466 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1467 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1468 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1469 | int *pointer = (int *)p; |
| 1470 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1471 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1472 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1473 | *pointer = fd; |
| 1474 | return 1; |
| 1475 | } |
| 1476 | |
| 1477 | static PyObject * |
| 1478 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1479 | { |
| 1480 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1481 | int async_err = 0; |
| 1482 | |
| 1483 | do { |
| 1484 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1485 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1486 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1487 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1488 | Py_END_ALLOW_THREADS |
| 1489 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1490 | if (res != 0) |
| 1491 | return (!async_err) ? posix_error() : NULL; |
| 1492 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1493 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1494 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1495 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1496 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1497 | /* This is a reimplementation of the C library's chdir function, |
| 1498 | but one that produces Win32 errors instead of DOS error codes. |
| 1499 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1500 | it also needs to set "magic" environment variables indicating |
| 1501 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1502 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1503 | win32_wchdir(LPCWSTR path) |
| 1504 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1505 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1506 | int result; |
| 1507 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1508 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1509 | if(!SetCurrentDirectoryW(path)) |
| 1510 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1511 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1512 | if (!result) |
| 1513 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1514 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1515 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1516 | if (!new_path) { |
| 1517 | SetLastError(ERROR_OUTOFMEMORY); |
| 1518 | return FALSE; |
| 1519 | } |
| 1520 | result = GetCurrentDirectoryW(result, new_path); |
| 1521 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1522 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1523 | return FALSE; |
| 1524 | } |
| 1525 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1526 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1527 | wcsncmp(new_path, L"//", 2) == 0); |
| 1528 | if (!is_unc_like_path) { |
| 1529 | env[1] = new_path[0]; |
| 1530 | result = SetEnvironmentVariableW(env, new_path); |
| 1531 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1532 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1533 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1534 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1535 | } |
| 1536 | #endif |
| 1537 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1538 | #ifdef MS_WINDOWS |
| 1539 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1540 | - time stamps are restricted to second resolution |
| 1541 | - file modification times suffer from forth-and-back conversions between |
| 1542 | UTC and local time |
| 1543 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1544 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1545 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1546 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1547 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1548 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1549 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1550 | BY_HANDLE_FILE_INFORMATION *info, |
| 1551 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1552 | { |
| 1553 | memset(info, 0, sizeof(*info)); |
| 1554 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1555 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1556 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1557 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1558 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1559 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1560 | /* info->nNumberOfLinks = 1; */ |
| 1561 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1562 | *reparse_tag = pFileData->dwReserved0; |
| 1563 | else |
| 1564 | *reparse_tag = 0; |
| 1565 | } |
| 1566 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1567 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1568 | 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] | 1569 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1570 | HANDLE hFindFile; |
| 1571 | WIN32_FIND_DATAW FileData; |
| 1572 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1573 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1574 | return FALSE; |
| 1575 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1576 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1577 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1580 | static BOOL |
| 1581 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1582 | { |
| 1583 | int buf_size, result_length; |
| 1584 | wchar_t *buf; |
| 1585 | |
| 1586 | /* We have a good handle to the target, use it to determine |
| 1587 | the target path name (then we'll call lstat on it). */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1588 | buf_size = GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1589 | VOLUME_NAME_DOS); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1590 | if(!buf_size) |
| 1591 | return FALSE; |
| 1592 | |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1593 | buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1594 | if (!buf) { |
| 1595 | SetLastError(ERROR_OUTOFMEMORY); |
| 1596 | return FALSE; |
| 1597 | } |
| 1598 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1599 | result_length = GetFinalPathNameByHandleW(hdl, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1600 | buf, buf_size, VOLUME_NAME_DOS); |
| 1601 | |
| 1602 | if(!result_length) { |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1603 | PyMem_RawFree(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1604 | return FALSE; |
| 1605 | } |
| 1606 | |
| 1607 | if(!CloseHandle(hdl)) { |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1608 | PyMem_RawFree(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1609 | return FALSE; |
| 1610 | } |
| 1611 | |
| 1612 | buf[result_length] = 0; |
| 1613 | |
| 1614 | *target_path = buf; |
| 1615 | return TRUE; |
| 1616 | } |
| 1617 | |
| 1618 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1619 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1620 | BOOL traverse) |
| 1621 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1622 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1623 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1624 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1625 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1626 | wchar_t *target_path; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1627 | const wchar_t *dot; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1628 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1629 | hFile = CreateFileW( |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1630 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1631 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1632 | 0, /* share mode */ |
| 1633 | NULL, /* security attributes */ |
| 1634 | OPEN_EXISTING, |
| 1635 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1636 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1637 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1638 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1639 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1640 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1641 | NULL); |
| 1642 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1643 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1644 | /* Either the target doesn't exist, or we don't have access to |
| 1645 | get a handle to it. If the former, we need to return an error. |
| 1646 | If the latter, we can use attributes_from_dir. */ |
Berker Peksag | 0b4dc48 | 2016-09-17 15:49:59 +0300 | [diff] [blame] | 1647 | DWORD lastError = GetLastError(); |
| 1648 | if (lastError != ERROR_ACCESS_DENIED && |
| 1649 | lastError != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1650 | return -1; |
| 1651 | /* Could not get attributes on open file. Fall back to |
| 1652 | reading the directory. */ |
| 1653 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1654 | /* Very strange. This should not fail now */ |
| 1655 | return -1; |
| 1656 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1657 | if (traverse) { |
| 1658 | /* Should traverse, but could not open reparse point handle */ |
Berker Peksag | 0b4dc48 | 2016-09-17 15:49:59 +0300 | [diff] [blame] | 1659 | SetLastError(lastError); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1660 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1661 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1662 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1663 | } else { |
| 1664 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1665 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1666 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1667 | } |
| 1668 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1669 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1670 | return -1; |
| 1671 | |
| 1672 | /* Close the outer open file handle now that we're about to |
| 1673 | reopen it with different flags. */ |
| 1674 | if (!CloseHandle(hFile)) |
| 1675 | return -1; |
| 1676 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1677 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1678 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1679 | the file without the reparse handling flag set. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1680 | hFile2 = CreateFileW( |
| 1681 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1682 | NULL, OPEN_EXISTING, |
| 1683 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1684 | NULL); |
| 1685 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1686 | return -1; |
| 1687 | |
| 1688 | if (!get_target_path(hFile2, &target_path)) |
| 1689 | return -1; |
| 1690 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1691 | code = win32_xstat_impl(target_path, result, FALSE); |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1692 | PyMem_RawFree(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1693 | return code; |
| 1694 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1695 | } else |
| 1696 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1697 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1698 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1699 | |
| 1700 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1701 | dot = wcsrchr(path, '.'); |
| 1702 | if (dot) { |
| 1703 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1704 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1705 | result->st_mode |= 0111; |
| 1706 | } |
| 1707 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1711 | 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] | 1712 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1713 | /* Protocol violation: we explicitly clear errno, instead of |
| 1714 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1715 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1716 | errno = 0; |
| 1717 | return code; |
| 1718 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1719 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1720 | |
| 1721 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1722 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1723 | default does not traverse symlinks and instead returns attributes for |
| 1724 | the symlink. |
| 1725 | |
| 1726 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1727 | win32_stat will first explicitly resolve the symlink target and then will |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1728 | call win32_lstat on that result. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1729 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1730 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1731 | 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] | 1732 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1733 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1736 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1737 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1738 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1739 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1742 | #endif /* MS_WINDOWS */ |
| 1743 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1744 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1745 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1746 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1747 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1748 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1749 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1750 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1751 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1752 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1753 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1754 | |
| 1755 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1756 | {"st_mode", "protection bits"}, |
| 1757 | {"st_ino", "inode"}, |
| 1758 | {"st_dev", "device"}, |
| 1759 | {"st_nlink", "number of hard links"}, |
| 1760 | {"st_uid", "user ID of owner"}, |
| 1761 | {"st_gid", "group ID of owner"}, |
| 1762 | {"st_size", "total size, in bytes"}, |
| 1763 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1764 | {NULL, "integer time of last access"}, |
| 1765 | {NULL, "integer time of last modification"}, |
| 1766 | {NULL, "integer time of last change"}, |
| 1767 | {"st_atime", "time of last access"}, |
| 1768 | {"st_mtime", "time of last modification"}, |
| 1769 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1770 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1771 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1772 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1773 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1774 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1775 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1776 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1777 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1778 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1779 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1780 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1781 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1782 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1783 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1784 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1785 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1786 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1787 | #endif |
| 1788 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1789 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1790 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1791 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1792 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1793 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1794 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1795 | {"st_fstype", "Type of filesystem"}, |
| 1796 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1797 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1798 | }; |
| 1799 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1800 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1801 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1802 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1803 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1804 | #endif |
| 1805 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1806 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1807 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1808 | #else |
| 1809 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1810 | #endif |
| 1811 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1812 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1813 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1814 | #else |
| 1815 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1816 | #endif |
| 1817 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1818 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1819 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1820 | #else |
| 1821 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1822 | #endif |
| 1823 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1824 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1825 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1826 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1827 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1828 | #endif |
| 1829 | |
| 1830 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1831 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1832 | #else |
| 1833 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1834 | #endif |
| 1835 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1836 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1837 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1838 | #else |
| 1839 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 1840 | #endif |
| 1841 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1842 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1843 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 1844 | #else |
| 1845 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 1846 | #endif |
| 1847 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1848 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1849 | "stat_result", /* name */ |
| 1850 | stat_result__doc__, /* doc */ |
| 1851 | stat_result_fields, |
| 1852 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1853 | }; |
| 1854 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1855 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1856 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1857 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1858 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1859 | 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] | 1860 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1861 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1862 | |
| 1863 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1864 | {"f_bsize", }, |
| 1865 | {"f_frsize", }, |
| 1866 | {"f_blocks", }, |
| 1867 | {"f_bfree", }, |
| 1868 | {"f_bavail", }, |
| 1869 | {"f_files", }, |
| 1870 | {"f_ffree", }, |
| 1871 | {"f_favail", }, |
| 1872 | {"f_flag", }, |
| 1873 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 1874 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1875 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1876 | }; |
| 1877 | |
| 1878 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1879 | "statvfs_result", /* name */ |
| 1880 | statvfs_result__doc__, /* doc */ |
| 1881 | statvfs_result_fields, |
| 1882 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1883 | }; |
| 1884 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1885 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1886 | PyDoc_STRVAR(waitid_result__doc__, |
| 1887 | "waitid_result: Result from waitid.\n\n\ |
| 1888 | This object may be accessed either as a tuple of\n\ |
| 1889 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1890 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1891 | \n\ |
| 1892 | See os.waitid for more information."); |
| 1893 | |
| 1894 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1895 | {"si_pid", }, |
| 1896 | {"si_uid", }, |
| 1897 | {"si_signo", }, |
| 1898 | {"si_status", }, |
| 1899 | {"si_code", }, |
| 1900 | {0} |
| 1901 | }; |
| 1902 | |
| 1903 | static PyStructSequence_Desc waitid_result_desc = { |
| 1904 | "waitid_result", /* name */ |
| 1905 | waitid_result__doc__, /* doc */ |
| 1906 | waitid_result_fields, |
| 1907 | 5 |
| 1908 | }; |
| 1909 | static PyTypeObject WaitidResultType; |
| 1910 | #endif |
| 1911 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1912 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1913 | static PyTypeObject StatResultType; |
| 1914 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1915 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1916 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1917 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1918 | static newfunc structseq_new; |
| 1919 | |
| 1920 | static PyObject * |
| 1921 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1922 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1923 | PyStructSequence *result; |
| 1924 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1925 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1926 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1927 | if (!result) |
| 1928 | return NULL; |
| 1929 | /* If we have been initialized from a tuple, |
| 1930 | st_?time might be set to None. Initialize it |
| 1931 | from the int slots. */ |
| 1932 | for (i = 7; i <= 9; i++) { |
| 1933 | if (result->ob_item[i+3] == Py_None) { |
| 1934 | Py_DECREF(Py_None); |
| 1935 | Py_INCREF(result->ob_item[i]); |
| 1936 | result->ob_item[i+3] = result->ob_item[i]; |
| 1937 | } |
| 1938 | } |
| 1939 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1943 | static PyObject *billion = NULL; |
| 1944 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1945 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1946 | 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] | 1947 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1948 | PyObject *s = _PyLong_FromTime_t(sec); |
| 1949 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 1950 | PyObject *s_in_ns = NULL; |
| 1951 | PyObject *ns_total = NULL; |
| 1952 | PyObject *float_s = NULL; |
| 1953 | |
| 1954 | if (!(s && ns_fractional)) |
| 1955 | goto exit; |
| 1956 | |
| 1957 | s_in_ns = PyNumber_Multiply(s, billion); |
| 1958 | if (!s_in_ns) |
| 1959 | goto exit; |
| 1960 | |
| 1961 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 1962 | if (!ns_total) |
| 1963 | goto exit; |
| 1964 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 1965 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1966 | if (!float_s) { |
| 1967 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | PyStructSequence_SET_ITEM(v, index, s); |
| 1971 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 1972 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 1973 | s = NULL; |
| 1974 | float_s = NULL; |
| 1975 | ns_total = NULL; |
| 1976 | exit: |
| 1977 | Py_XDECREF(s); |
| 1978 | Py_XDECREF(ns_fractional); |
| 1979 | Py_XDECREF(s_in_ns); |
| 1980 | Py_XDECREF(ns_total); |
| 1981 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1984 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1985 | (used by posix_stat() and posix_fstat()) */ |
| 1986 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1987 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1988 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1989 | unsigned long ansec, mnsec, cnsec; |
| 1990 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1991 | if (v == NULL) |
| 1992 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1993 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1994 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 1995 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 1996 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 1997 | #ifdef MS_WINDOWS |
| 1998 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1999 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2000 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2001 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2002 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2003 | #if defined(MS_WINDOWS) |
| 2004 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2005 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2006 | #else |
| 2007 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2008 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2009 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2010 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2011 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2012 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2013 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2014 | ansec = st->st_atim.tv_nsec; |
| 2015 | mnsec = st->st_mtim.tv_nsec; |
| 2016 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2017 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2018 | ansec = st->st_atimespec.tv_nsec; |
| 2019 | mnsec = st->st_mtimespec.tv_nsec; |
| 2020 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2021 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2022 | ansec = st->st_atime_nsec; |
| 2023 | mnsec = st->st_mtime_nsec; |
| 2024 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2025 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2026 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2027 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2028 | fill_time(v, 7, st->st_atime, ansec); |
| 2029 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2030 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2031 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2032 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2033 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2034 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2035 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2036 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2037 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2038 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2039 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2040 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2041 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2042 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2043 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2044 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2045 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2046 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2047 | #endif |
| 2048 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2049 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2050 | PyObject *val; |
| 2051 | unsigned long bsec,bnsec; |
| 2052 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2053 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2054 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2055 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2056 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2057 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2058 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2059 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2060 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2061 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2062 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2063 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2064 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2065 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2066 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2067 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2068 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2069 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2070 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2071 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2072 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2073 | PyUnicode_FromString(st->st_fstype)); |
| 2074 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2075 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2076 | if (PyErr_Occurred()) { |
| 2077 | Py_DECREF(v); |
| 2078 | return NULL; |
| 2079 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2080 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2081 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2084 | /* POSIX methods */ |
| 2085 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2086 | |
| 2087 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2088 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2089 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2090 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2091 | STRUCT_STAT st; |
| 2092 | int result; |
| 2093 | |
| 2094 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2095 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2096 | return NULL; |
| 2097 | #endif |
| 2098 | |
| 2099 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2100 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2101 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2102 | return NULL; |
| 2103 | |
| 2104 | Py_BEGIN_ALLOW_THREADS |
| 2105 | if (path->fd != -1) |
| 2106 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2107 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2108 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2109 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2110 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2111 | result = win32_lstat(path->wide, &st); |
| 2112 | #else |
| 2113 | else |
| 2114 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2115 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2116 | result = LSTAT(path->narrow, &st); |
| 2117 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2118 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2119 | #ifdef HAVE_FSTATAT |
| 2120 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2121 | result = fstatat(dir_fd, path->narrow, &st, |
| 2122 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2123 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2124 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2125 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2126 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2127 | Py_END_ALLOW_THREADS |
| 2128 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2129 | if (result != 0) { |
| 2130 | return path_error(path); |
| 2131 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2132 | |
| 2133 | return _pystat_fromstructstat(&st); |
| 2134 | } |
| 2135 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2136 | /*[python input] |
| 2137 | |
| 2138 | for s in """ |
| 2139 | |
| 2140 | FACCESSAT |
| 2141 | FCHMODAT |
| 2142 | FCHOWNAT |
| 2143 | FSTATAT |
| 2144 | LINKAT |
| 2145 | MKDIRAT |
| 2146 | MKFIFOAT |
| 2147 | MKNODAT |
| 2148 | OPENAT |
| 2149 | READLINKAT |
| 2150 | SYMLINKAT |
| 2151 | UNLINKAT |
| 2152 | |
| 2153 | """.strip().split(): |
| 2154 | s = s.strip() |
| 2155 | print(""" |
| 2156 | #ifdef HAVE_{s} |
| 2157 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2158 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2159 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2160 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2161 | """.rstrip().format(s=s)) |
| 2162 | |
| 2163 | for s in """ |
| 2164 | |
| 2165 | FCHDIR |
| 2166 | FCHMOD |
| 2167 | FCHOWN |
| 2168 | FDOPENDIR |
| 2169 | FEXECVE |
| 2170 | FPATHCONF |
| 2171 | FSTATVFS |
| 2172 | FTRUNCATE |
| 2173 | |
| 2174 | """.strip().split(): |
| 2175 | s = s.strip() |
| 2176 | print(""" |
| 2177 | #ifdef HAVE_{s} |
| 2178 | #define PATH_HAVE_{s} 1 |
| 2179 | #else |
| 2180 | #define PATH_HAVE_{s} 0 |
| 2181 | #endif |
| 2182 | |
| 2183 | """.rstrip().format(s=s)) |
| 2184 | [python start generated code]*/ |
| 2185 | |
| 2186 | #ifdef HAVE_FACCESSAT |
| 2187 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2188 | #else |
| 2189 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2190 | #endif |
| 2191 | |
| 2192 | #ifdef HAVE_FCHMODAT |
| 2193 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2194 | #else |
| 2195 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2196 | #endif |
| 2197 | |
| 2198 | #ifdef HAVE_FCHOWNAT |
| 2199 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2200 | #else |
| 2201 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2202 | #endif |
| 2203 | |
| 2204 | #ifdef HAVE_FSTATAT |
| 2205 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2206 | #else |
| 2207 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2208 | #endif |
| 2209 | |
| 2210 | #ifdef HAVE_LINKAT |
| 2211 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2212 | #else |
| 2213 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2214 | #endif |
| 2215 | |
| 2216 | #ifdef HAVE_MKDIRAT |
| 2217 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2218 | #else |
| 2219 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2220 | #endif |
| 2221 | |
| 2222 | #ifdef HAVE_MKFIFOAT |
| 2223 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2224 | #else |
| 2225 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2226 | #endif |
| 2227 | |
| 2228 | #ifdef HAVE_MKNODAT |
| 2229 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2230 | #else |
| 2231 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2232 | #endif |
| 2233 | |
| 2234 | #ifdef HAVE_OPENAT |
| 2235 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2236 | #else |
| 2237 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2238 | #endif |
| 2239 | |
| 2240 | #ifdef HAVE_READLINKAT |
| 2241 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2242 | #else |
| 2243 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2244 | #endif |
| 2245 | |
| 2246 | #ifdef HAVE_SYMLINKAT |
| 2247 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2248 | #else |
| 2249 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2250 | #endif |
| 2251 | |
| 2252 | #ifdef HAVE_UNLINKAT |
| 2253 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2254 | #else |
| 2255 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2256 | #endif |
| 2257 | |
| 2258 | #ifdef HAVE_FCHDIR |
| 2259 | #define PATH_HAVE_FCHDIR 1 |
| 2260 | #else |
| 2261 | #define PATH_HAVE_FCHDIR 0 |
| 2262 | #endif |
| 2263 | |
| 2264 | #ifdef HAVE_FCHMOD |
| 2265 | #define PATH_HAVE_FCHMOD 1 |
| 2266 | #else |
| 2267 | #define PATH_HAVE_FCHMOD 0 |
| 2268 | #endif |
| 2269 | |
| 2270 | #ifdef HAVE_FCHOWN |
| 2271 | #define PATH_HAVE_FCHOWN 1 |
| 2272 | #else |
| 2273 | #define PATH_HAVE_FCHOWN 0 |
| 2274 | #endif |
| 2275 | |
| 2276 | #ifdef HAVE_FDOPENDIR |
| 2277 | #define PATH_HAVE_FDOPENDIR 1 |
| 2278 | #else |
| 2279 | #define PATH_HAVE_FDOPENDIR 0 |
| 2280 | #endif |
| 2281 | |
| 2282 | #ifdef HAVE_FEXECVE |
| 2283 | #define PATH_HAVE_FEXECVE 1 |
| 2284 | #else |
| 2285 | #define PATH_HAVE_FEXECVE 0 |
| 2286 | #endif |
| 2287 | |
| 2288 | #ifdef HAVE_FPATHCONF |
| 2289 | #define PATH_HAVE_FPATHCONF 1 |
| 2290 | #else |
| 2291 | #define PATH_HAVE_FPATHCONF 0 |
| 2292 | #endif |
| 2293 | |
| 2294 | #ifdef HAVE_FSTATVFS |
| 2295 | #define PATH_HAVE_FSTATVFS 1 |
| 2296 | #else |
| 2297 | #define PATH_HAVE_FSTATVFS 0 |
| 2298 | #endif |
| 2299 | |
| 2300 | #ifdef HAVE_FTRUNCATE |
| 2301 | #define PATH_HAVE_FTRUNCATE 1 |
| 2302 | #else |
| 2303 | #define PATH_HAVE_FTRUNCATE 0 |
| 2304 | #endif |
| 2305 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2306 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2307 | #ifdef MS_WINDOWS |
| 2308 | #undef PATH_HAVE_FTRUNCATE |
| 2309 | #define PATH_HAVE_FTRUNCATE 1 |
| 2310 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2311 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2312 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2313 | |
| 2314 | class path_t_converter(CConverter): |
| 2315 | |
| 2316 | type = "path_t" |
| 2317 | impl_by_reference = True |
| 2318 | parse_by_reference = True |
| 2319 | |
| 2320 | converter = 'path_converter' |
| 2321 | |
| 2322 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2323 | # right now path_t doesn't support default values. |
| 2324 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2325 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2326 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2327 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2328 | if self.c_default not in (None, 'Py_None'): |
| 2329 | 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] | 2330 | |
| 2331 | self.nullable = nullable |
| 2332 | self.allow_fd = allow_fd |
| 2333 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2334 | def pre_render(self): |
| 2335 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2336 | if isinstance(value, str): |
| 2337 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2338 | return str(int(bool(value))) |
| 2339 | |
| 2340 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2341 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2342 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2343 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2344 | strify(self.nullable), |
| 2345 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2346 | ) |
| 2347 | |
| 2348 | def cleanup(self): |
| 2349 | return "path_cleanup(&" + self.name + ");\n" |
| 2350 | |
| 2351 | |
| 2352 | class dir_fd_converter(CConverter): |
| 2353 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2354 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2355 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2356 | if self.default in (unspecified, None): |
| 2357 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2358 | if isinstance(requires, str): |
| 2359 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2360 | else: |
| 2361 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2362 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2363 | class fildes_converter(CConverter): |
| 2364 | type = 'int' |
| 2365 | converter = 'fildes_converter' |
| 2366 | |
| 2367 | class uid_t_converter(CConverter): |
| 2368 | type = "uid_t" |
| 2369 | converter = '_Py_Uid_Converter' |
| 2370 | |
| 2371 | class gid_t_converter(CConverter): |
| 2372 | type = "gid_t" |
| 2373 | converter = '_Py_Gid_Converter' |
| 2374 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2375 | class dev_t_converter(CConverter): |
| 2376 | type = 'dev_t' |
| 2377 | converter = '_Py_Dev_Converter' |
| 2378 | |
| 2379 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2380 | type = 'dev_t' |
| 2381 | conversion_fn = '_PyLong_FromDev' |
| 2382 | unsigned_cast = '(dev_t)' |
| 2383 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2384 | class FSConverter_converter(CConverter): |
| 2385 | type = 'PyObject *' |
| 2386 | converter = 'PyUnicode_FSConverter' |
| 2387 | def converter_init(self): |
| 2388 | if self.default is not unspecified: |
| 2389 | fail("FSConverter_converter does not support default values") |
| 2390 | self.c_default = 'NULL' |
| 2391 | |
| 2392 | def cleanup(self): |
| 2393 | return "Py_XDECREF(" + self.name + ");\n" |
| 2394 | |
| 2395 | class pid_t_converter(CConverter): |
| 2396 | type = 'pid_t' |
| 2397 | format_unit = '" _Py_PARSE_PID "' |
| 2398 | |
| 2399 | class idtype_t_converter(int_converter): |
| 2400 | type = 'idtype_t' |
| 2401 | |
| 2402 | class id_t_converter(CConverter): |
| 2403 | type = 'id_t' |
| 2404 | format_unit = '" _Py_PARSE_PID "' |
| 2405 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2406 | class intptr_t_converter(CConverter): |
| 2407 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2408 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2409 | |
| 2410 | class Py_off_t_converter(CConverter): |
| 2411 | type = 'Py_off_t' |
| 2412 | converter = 'Py_off_t_converter' |
| 2413 | |
| 2414 | class Py_off_t_return_converter(long_return_converter): |
| 2415 | type = 'Py_off_t' |
| 2416 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2417 | |
| 2418 | class path_confname_converter(CConverter): |
| 2419 | type="int" |
| 2420 | converter="conv_path_confname" |
| 2421 | |
| 2422 | class confstr_confname_converter(path_confname_converter): |
| 2423 | converter='conv_confstr_confname' |
| 2424 | |
| 2425 | class sysconf_confname_converter(path_confname_converter): |
| 2426 | converter="conv_sysconf_confname" |
| 2427 | |
| 2428 | class sched_param_converter(CConverter): |
| 2429 | type = 'struct sched_param' |
| 2430 | converter = 'convert_sched_param' |
| 2431 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2432 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2433 | [python start generated code]*/ |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 2434 | /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2435 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2436 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2437 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2438 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2439 | |
| 2440 | path : path_t(allow_fd=True) |
Xiang Zhang | 4459e00 | 2017-01-22 13:04:17 +0800 | [diff] [blame] | 2441 | Path to be examined; can be string, bytes, path-like object or |
| 2442 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2443 | |
| 2444 | * |
| 2445 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2446 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2447 | If not None, it should be a file descriptor open to a directory, |
| 2448 | and path should be a relative string; path will then be relative to |
| 2449 | that directory. |
| 2450 | |
| 2451 | follow_symlinks: bool = True |
| 2452 | If False, and the last element of the path is a symbolic link, |
| 2453 | stat will examine the symbolic link itself instead of the file |
| 2454 | the link points to. |
| 2455 | |
| 2456 | Perform a stat system call on the given path. |
| 2457 | |
| 2458 | dir_fd and follow_symlinks may not be implemented |
| 2459 | on your platform. If they are unavailable, using them will raise a |
| 2460 | NotImplementedError. |
| 2461 | |
| 2462 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2463 | an open file descriptor. |
| 2464 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2465 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2466 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2467 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2468 | os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks) |
Xiang Zhang | 4459e00 | 2017-01-22 13:04:17 +0800 | [diff] [blame] | 2469 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=270bd64e7bb3c8f7]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2470 | { |
| 2471 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2472 | } |
| 2473 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2474 | |
| 2475 | /*[clinic input] |
| 2476 | os.lstat |
| 2477 | |
| 2478 | path : path_t |
| 2479 | |
| 2480 | * |
| 2481 | |
| 2482 | dir_fd : dir_fd(requires='fstatat') = None |
| 2483 | |
| 2484 | Perform a stat system call on the given path, without following symbolic links. |
| 2485 | |
| 2486 | Like stat(), but do not follow symbolic links. |
| 2487 | Equivalent to stat(path, follow_symlinks=False). |
| 2488 | [clinic start generated code]*/ |
| 2489 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2490 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2491 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2492 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2493 | { |
| 2494 | int follow_symlinks = 0; |
| 2495 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2496 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2497 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2498 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2499 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2500 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2501 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2502 | path: path_t |
| 2503 | Path to be tested; can be string or bytes |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2504 | |
| 2505 | mode: int |
| 2506 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2507 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2508 | |
| 2509 | * |
| 2510 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2511 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2512 | If not None, it should be a file descriptor open to a directory, |
| 2513 | and path should be relative; path will then be relative to that |
| 2514 | directory. |
| 2515 | |
| 2516 | effective_ids: bool = False |
| 2517 | If True, access will use the effective uid/gid instead of |
| 2518 | the real uid/gid. |
| 2519 | |
| 2520 | follow_symlinks: bool = True |
| 2521 | If False, and the last element of the path is a symbolic link, |
| 2522 | access will examine the symbolic link itself instead of the file |
| 2523 | the link points to. |
| 2524 | |
| 2525 | Use the real uid/gid to test for access to a path. |
| 2526 | |
| 2527 | {parameters} |
| 2528 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2529 | on your platform. If they are unavailable, using them will raise a |
| 2530 | NotImplementedError. |
| 2531 | |
| 2532 | Note that most operations will use the effective uid/gid, therefore this |
| 2533 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2534 | has the specified access to the path. |
| 2535 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2536 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2537 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2538 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2539 | 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] | 2540 | int effective_ids, int follow_symlinks) |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2541 | /*[clinic end generated code: output=cf84158bc90b1a77 input=8e8c3a6ba791fee3]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2542 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2543 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2544 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2545 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2546 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2547 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2548 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2549 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2550 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2551 | #ifndef HAVE_FACCESSAT |
| 2552 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2553 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2554 | |
| 2555 | if (effective_ids) { |
| 2556 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2557 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2558 | } |
| 2559 | #endif |
| 2560 | |
| 2561 | #ifdef MS_WINDOWS |
| 2562 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2563 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2564 | Py_END_ALLOW_THREADS |
| 2565 | |
| 2566 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2567 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2568 | * * we didn't get a -1, and |
| 2569 | * * write access wasn't requested, |
| 2570 | * * or the file isn't read-only, |
| 2571 | * * or it's a directory. |
| 2572 | * (Directories cannot be read-only on Windows.) |
| 2573 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2574 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2575 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2576 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2577 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2578 | #else |
| 2579 | |
| 2580 | Py_BEGIN_ALLOW_THREADS |
| 2581 | #ifdef HAVE_FACCESSAT |
| 2582 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2583 | effective_ids || |
| 2584 | !follow_symlinks) { |
| 2585 | int flags = 0; |
| 2586 | if (!follow_symlinks) |
| 2587 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2588 | if (effective_ids) |
| 2589 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2590 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2591 | } |
| 2592 | else |
| 2593 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2594 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2595 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2596 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2597 | #endif |
| 2598 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2599 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2602 | #ifndef F_OK |
| 2603 | #define F_OK 0 |
| 2604 | #endif |
| 2605 | #ifndef R_OK |
| 2606 | #define R_OK 4 |
| 2607 | #endif |
| 2608 | #ifndef W_OK |
| 2609 | #define W_OK 2 |
| 2610 | #endif |
| 2611 | #ifndef X_OK |
| 2612 | #define X_OK 1 |
| 2613 | #endif |
| 2614 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2615 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2616 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2617 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2618 | os.ttyname -> DecodeFSDefault |
| 2619 | |
| 2620 | fd: int |
| 2621 | Integer file descriptor handle. |
| 2622 | |
| 2623 | / |
| 2624 | |
| 2625 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2626 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2627 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2628 | static char * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2629 | os_ttyname_impl(PyObject *module, int fd) |
| 2630 | /*[clinic end generated code: output=ed16ad216d813591 input=5f72ca83e76b3b45]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2631 | { |
| 2632 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2633 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2634 | ret = ttyname(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2635 | if (ret == NULL) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2636 | posix_error(); |
| 2637 | return ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2638 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2639 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2640 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2641 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2642 | /*[clinic input] |
| 2643 | os.ctermid |
| 2644 | |
| 2645 | Return the name of the controlling terminal for this process. |
| 2646 | [clinic start generated code]*/ |
| 2647 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2648 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2649 | os_ctermid_impl(PyObject *module) |
| 2650 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2651 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2652 | char *ret; |
| 2653 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2654 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2655 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2656 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2657 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2658 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2659 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2660 | if (ret == NULL) |
| 2661 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2662 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2663 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2664 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2665 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2666 | |
| 2667 | /*[clinic input] |
| 2668 | os.chdir |
| 2669 | |
| 2670 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2671 | |
| 2672 | Change the current working directory to the specified path. |
| 2673 | |
| 2674 | path may always be specified as a string. |
| 2675 | On some platforms, path may also be specified as an open file descriptor. |
| 2676 | If this functionality is unavailable, using it raises an exception. |
| 2677 | [clinic start generated code]*/ |
| 2678 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2679 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2680 | os_chdir_impl(PyObject *module, path_t *path) |
| 2681 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2682 | { |
| 2683 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2684 | |
| 2685 | Py_BEGIN_ALLOW_THREADS |
| 2686 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2687 | /* on unix, success = 0, on windows, success = !0 */ |
| 2688 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2689 | #else |
| 2690 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2691 | if (path->fd != -1) |
| 2692 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2693 | else |
| 2694 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2695 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2696 | #endif |
| 2697 | Py_END_ALLOW_THREADS |
| 2698 | |
| 2699 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2700 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2703 | Py_RETURN_NONE; |
| 2704 | } |
| 2705 | |
| 2706 | |
| 2707 | #ifdef HAVE_FCHDIR |
| 2708 | /*[clinic input] |
| 2709 | os.fchdir |
| 2710 | |
| 2711 | fd: fildes |
| 2712 | |
| 2713 | Change to the directory of the given file descriptor. |
| 2714 | |
| 2715 | fd must be opened on a directory, not a file. |
| 2716 | Equivalent to os.chdir(fd). |
| 2717 | |
| 2718 | [clinic start generated code]*/ |
| 2719 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2720 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2721 | os_fchdir_impl(PyObject *module, int fd) |
| 2722 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2723 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2724 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2725 | } |
| 2726 | #endif /* HAVE_FCHDIR */ |
| 2727 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2728 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2729 | /*[clinic input] |
| 2730 | os.chmod |
| 2731 | |
| 2732 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
| 2733 | Path to be modified. May always be specified as a str or bytes. |
| 2734 | On some platforms, path may also be specified as an open file descriptor. |
| 2735 | If this functionality is unavailable, using it raises an exception. |
| 2736 | |
| 2737 | mode: int |
| 2738 | Operating-system mode bitfield. |
| 2739 | |
| 2740 | * |
| 2741 | |
| 2742 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2743 | If not None, it should be a file descriptor open to a directory, |
| 2744 | and path should be relative; path will then be relative to that |
| 2745 | directory. |
| 2746 | |
| 2747 | follow_symlinks: bool = True |
| 2748 | If False, and the last element of the path is a symbolic link, |
| 2749 | chmod will modify the symbolic link itself instead of the file |
| 2750 | the link points to. |
| 2751 | |
| 2752 | Change the access permissions of a file. |
| 2753 | |
| 2754 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2755 | an open file descriptor. |
| 2756 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2757 | If they are unavailable, using them will raise a NotImplementedError. |
| 2758 | |
| 2759 | [clinic start generated code]*/ |
| 2760 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2761 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2762 | 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] | 2763 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2764 | /*[clinic end generated code: output=5cf6a94915cc7bff input=7f1618e5e15cc196]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2765 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2766 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2767 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2768 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2769 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2770 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2771 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2772 | #ifdef HAVE_FCHMODAT |
| 2773 | int fchmodat_nofollow_unsupported = 0; |
| 2774 | #endif |
| 2775 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2776 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2777 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2778 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2779 | #endif |
| 2780 | |
| 2781 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2782 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2783 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2784 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2785 | result = 0; |
| 2786 | else { |
| 2787 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2788 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2789 | else |
| 2790 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2791 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2792 | } |
| 2793 | Py_END_ALLOW_THREADS |
| 2794 | |
| 2795 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2796 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2797 | } |
| 2798 | #else /* MS_WINDOWS */ |
| 2799 | Py_BEGIN_ALLOW_THREADS |
| 2800 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2801 | if (path->fd != -1) |
| 2802 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2803 | else |
| 2804 | #endif |
| 2805 | #ifdef HAVE_LCHMOD |
| 2806 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2807 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2808 | else |
| 2809 | #endif |
| 2810 | #ifdef HAVE_FCHMODAT |
| 2811 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2812 | /* |
| 2813 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2814 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2815 | * and then says it isn't implemented yet. |
| 2816 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2817 | * |
| 2818 | * Once it is supported, os.chmod will automatically |
| 2819 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2820 | * Until then, we need to be careful what exception we raise. |
| 2821 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2822 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2823 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2824 | /* |
| 2825 | * But wait! We can't throw the exception without allowing threads, |
| 2826 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2827 | */ |
| 2828 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2829 | result && |
| 2830 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2831 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2832 | } |
| 2833 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2834 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2835 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2836 | Py_END_ALLOW_THREADS |
| 2837 | |
| 2838 | if (result) { |
| 2839 | #ifdef HAVE_FCHMODAT |
| 2840 | if (fchmodat_nofollow_unsupported) { |
| 2841 | if (dir_fd != DEFAULT_DIR_FD) |
| 2842 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2843 | dir_fd, follow_symlinks); |
| 2844 | else |
| 2845 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 2846 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2847 | } |
| 2848 | else |
| 2849 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2850 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2851 | } |
| 2852 | #endif |
| 2853 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2854 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2857 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2858 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2859 | /*[clinic input] |
| 2860 | os.fchmod |
| 2861 | |
| 2862 | fd: int |
| 2863 | mode: int |
| 2864 | |
| 2865 | Change the access permissions of the file given by file descriptor fd. |
| 2866 | |
| 2867 | Equivalent to os.chmod(fd, mode). |
| 2868 | [clinic start generated code]*/ |
| 2869 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2870 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2871 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 2872 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2873 | { |
| 2874 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2875 | int async_err = 0; |
| 2876 | |
| 2877 | do { |
| 2878 | Py_BEGIN_ALLOW_THREADS |
| 2879 | res = fchmod(fd, mode); |
| 2880 | Py_END_ALLOW_THREADS |
| 2881 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 2882 | if (res != 0) |
| 2883 | return (!async_err) ? posix_error() : NULL; |
| 2884 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2885 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2886 | } |
| 2887 | #endif /* HAVE_FCHMOD */ |
| 2888 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2889 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2890 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2891 | /*[clinic input] |
| 2892 | os.lchmod |
| 2893 | |
| 2894 | path: path_t |
| 2895 | mode: int |
| 2896 | |
| 2897 | Change the access permissions of a file, without following symbolic links. |
| 2898 | |
| 2899 | If path is a symlink, this affects the link itself rather than the target. |
| 2900 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 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_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 2905 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2906 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2907 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2908 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2909 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2910 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2911 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2912 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2913 | return NULL; |
| 2914 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2915 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2916 | } |
| 2917 | #endif /* HAVE_LCHMOD */ |
| 2918 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2919 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2920 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2921 | /*[clinic input] |
| 2922 | os.chflags |
| 2923 | |
| 2924 | path: path_t |
| 2925 | flags: unsigned_long(bitwise=True) |
| 2926 | follow_symlinks: bool=True |
| 2927 | |
| 2928 | Set file flags. |
| 2929 | |
| 2930 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 2931 | link, chflags will change flags on the symbolic link itself instead of the |
| 2932 | file the link points to. |
| 2933 | follow_symlinks may not be implemented on your platform. If it is |
| 2934 | unavailable, using it will raise a NotImplementedError. |
| 2935 | |
| 2936 | [clinic start generated code]*/ |
| 2937 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2938 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2939 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2940 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2941 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2942 | { |
| 2943 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2944 | |
| 2945 | #ifndef HAVE_LCHFLAGS |
| 2946 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2947 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2948 | #endif |
| 2949 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2950 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2951 | #ifdef HAVE_LCHFLAGS |
| 2952 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2953 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2954 | else |
| 2955 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2956 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2957 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2958 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2959 | if (result) |
| 2960 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2961 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2962 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2963 | } |
| 2964 | #endif /* HAVE_CHFLAGS */ |
| 2965 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2966 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2967 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2968 | /*[clinic input] |
| 2969 | os.lchflags |
| 2970 | |
| 2971 | path: path_t |
| 2972 | flags: unsigned_long(bitwise=True) |
| 2973 | |
| 2974 | Set file flags. |
| 2975 | |
| 2976 | This function will not follow symbolic links. |
| 2977 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 2978 | [clinic start generated code]*/ |
| 2979 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2980 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2981 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 2982 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2983 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2984 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2985 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2986 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2987 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2988 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2989 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2990 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2991 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2992 | } |
| 2993 | #endif /* HAVE_LCHFLAGS */ |
| 2994 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2995 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2996 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2997 | /*[clinic input] |
| 2998 | os.chroot |
| 2999 | path: path_t |
| 3000 | |
| 3001 | Change root directory to path. |
| 3002 | |
| 3003 | [clinic start generated code]*/ |
| 3004 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3005 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3006 | os_chroot_impl(PyObject *module, path_t *path) |
| 3007 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3008 | { |
| 3009 | int res; |
| 3010 | Py_BEGIN_ALLOW_THREADS |
| 3011 | res = chroot(path->narrow); |
| 3012 | Py_END_ALLOW_THREADS |
| 3013 | if (res < 0) |
| 3014 | return path_error(path); |
| 3015 | Py_RETURN_NONE; |
| 3016 | } |
| 3017 | #endif /* HAVE_CHROOT */ |
| 3018 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3019 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3020 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3021 | /*[clinic input] |
| 3022 | os.fsync |
| 3023 | |
| 3024 | fd: fildes |
| 3025 | |
| 3026 | Force write of fd to disk. |
| 3027 | [clinic start generated code]*/ |
| 3028 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3029 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3030 | os_fsync_impl(PyObject *module, int fd) |
| 3031 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3032 | { |
| 3033 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3034 | } |
| 3035 | #endif /* HAVE_FSYNC */ |
| 3036 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3037 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3038 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3039 | /*[clinic input] |
| 3040 | os.sync |
| 3041 | |
| 3042 | Force write of everything to disk. |
| 3043 | [clinic start generated code]*/ |
| 3044 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3045 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3046 | os_sync_impl(PyObject *module) |
| 3047 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3048 | { |
| 3049 | Py_BEGIN_ALLOW_THREADS |
| 3050 | sync(); |
| 3051 | Py_END_ALLOW_THREADS |
| 3052 | Py_RETURN_NONE; |
| 3053 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3054 | #endif /* HAVE_SYNC */ |
| 3055 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3056 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3057 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3058 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3059 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3060 | #endif |
| 3061 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3062 | /*[clinic input] |
| 3063 | os.fdatasync |
| 3064 | |
| 3065 | fd: fildes |
| 3066 | |
| 3067 | Force write of fd to disk without forcing update of metadata. |
| 3068 | [clinic start generated code]*/ |
| 3069 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3070 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3071 | os_fdatasync_impl(PyObject *module, int fd) |
| 3072 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3073 | { |
| 3074 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3075 | } |
| 3076 | #endif /* HAVE_FDATASYNC */ |
| 3077 | |
| 3078 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3079 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3080 | /*[clinic input] |
| 3081 | os.chown |
| 3082 | |
| 3083 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
| 3084 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 3085 | |
| 3086 | uid: uid_t |
| 3087 | |
| 3088 | gid: gid_t |
| 3089 | |
| 3090 | * |
| 3091 | |
| 3092 | dir_fd : dir_fd(requires='fchownat') = None |
| 3093 | If not None, it should be a file descriptor open to a directory, |
| 3094 | and path should be relative; path will then be relative to that |
| 3095 | directory. |
| 3096 | |
| 3097 | follow_symlinks: bool = True |
| 3098 | If False, and the last element of the path is a symbolic link, |
| 3099 | stat will examine the symbolic link itself instead of the file |
| 3100 | the link points to. |
| 3101 | |
| 3102 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3103 | |
| 3104 | path may always be specified as a string. |
| 3105 | On some platforms, path may also be specified as an open file descriptor. |
| 3106 | If this functionality is unavailable, using it raises an exception. |
| 3107 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3108 | and path should be relative; path will then be relative to that directory. |
| 3109 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3110 | link, chown will modify the symbolic link itself instead of the file the |
| 3111 | link points to. |
| 3112 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3113 | an open file descriptor. |
| 3114 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3115 | If they are unavailable, using them will raise a NotImplementedError. |
| 3116 | |
| 3117 | [clinic start generated code]*/ |
| 3118 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3119 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3120 | 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] | 3121 | int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3122 | /*[clinic end generated code: output=4beadab0db5f70cd input=a61cc35574814d5d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3123 | { |
| 3124 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3125 | |
| 3126 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3127 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3128 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3129 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3130 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3131 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3132 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3133 | |
| 3134 | #ifdef __APPLE__ |
| 3135 | /* |
| 3136 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3137 | * (But we still have an lchown symbol because of weak-linking.) |
| 3138 | * It doesn't have fchownat either. So there's no possibility |
| 3139 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3140 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3141 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3142 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3143 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3144 | } |
| 3145 | #endif |
| 3146 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3147 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3148 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3149 | if (path->fd != -1) |
| 3150 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3151 | else |
| 3152 | #endif |
| 3153 | #ifdef HAVE_LCHOWN |
| 3154 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3155 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3156 | else |
| 3157 | #endif |
| 3158 | #ifdef HAVE_FCHOWNAT |
| 3159 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3160 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3161 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3162 | else |
| 3163 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3164 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3165 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3166 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3167 | if (result) |
| 3168 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3169 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3170 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3171 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3172 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3173 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3174 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3175 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3176 | /*[clinic input] |
| 3177 | os.fchown |
| 3178 | |
| 3179 | fd: int |
| 3180 | uid: uid_t |
| 3181 | gid: gid_t |
| 3182 | |
| 3183 | Change the owner and group id of the file specified by file descriptor. |
| 3184 | |
| 3185 | Equivalent to os.chown(fd, uid, gid). |
| 3186 | |
| 3187 | [clinic start generated code]*/ |
| 3188 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3189 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3190 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3191 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3192 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3193 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3194 | int async_err = 0; |
| 3195 | |
| 3196 | do { |
| 3197 | Py_BEGIN_ALLOW_THREADS |
| 3198 | res = fchown(fd, uid, gid); |
| 3199 | Py_END_ALLOW_THREADS |
| 3200 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3201 | if (res != 0) |
| 3202 | return (!async_err) ? posix_error() : NULL; |
| 3203 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3204 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3205 | } |
| 3206 | #endif /* HAVE_FCHOWN */ |
| 3207 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3208 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3209 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3210 | /*[clinic input] |
| 3211 | os.lchown |
| 3212 | |
| 3213 | path : path_t |
| 3214 | uid: uid_t |
| 3215 | gid: gid_t |
| 3216 | |
| 3217 | Change the owner and group id of path to the numeric uid and gid. |
| 3218 | |
| 3219 | This function will not follow symbolic links. |
| 3220 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3221 | [clinic start generated code]*/ |
| 3222 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3223 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3224 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3225 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3226 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3227 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3228 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3229 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3230 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3231 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3232 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3233 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3234 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3235 | } |
| 3236 | #endif /* HAVE_LCHOWN */ |
| 3237 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3238 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3239 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3240 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3241 | { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3242 | char *buf, *tmpbuf; |
| 3243 | char *cwd; |
| 3244 | const size_t chunk = 1024; |
| 3245 | size_t buflen = 0; |
| 3246 | PyObject *obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3247 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3248 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3249 | if (!use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3250 | wchar_t wbuf[MAXPATHLEN]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3251 | wchar_t *wbuf2 = wbuf; |
| 3252 | PyObject *resobj; |
| 3253 | DWORD len; |
| 3254 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3255 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3256 | /* If the buffer is large enough, len does not include the |
| 3257 | terminating \0. If the buffer is too small, len includes |
| 3258 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3259 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3260 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3261 | if (wbuf2) |
| 3262 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3263 | } |
| 3264 | Py_END_ALLOW_THREADS |
| 3265 | if (!wbuf2) { |
| 3266 | PyErr_NoMemory(); |
| 3267 | return NULL; |
| 3268 | } |
| 3269 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3270 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3271 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3272 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3273 | } |
| 3274 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3275 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3276 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3277 | return resobj; |
| 3278 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3279 | |
| 3280 | if (win32_warn_bytes_api()) |
| 3281 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3282 | #endif |
| 3283 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3284 | buf = cwd = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3285 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3286 | do { |
| 3287 | buflen += chunk; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3288 | #ifdef MS_WINDOWS |
| 3289 | if (buflen > INT_MAX) { |
| 3290 | PyErr_NoMemory(); |
| 3291 | break; |
| 3292 | } |
| 3293 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3294 | tmpbuf = PyMem_RawRealloc(buf, buflen); |
| 3295 | if (tmpbuf == NULL) |
| 3296 | break; |
| 3297 | |
| 3298 | buf = tmpbuf; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3299 | #ifdef MS_WINDOWS |
| 3300 | cwd = getcwd(buf, (int)buflen); |
| 3301 | #else |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3302 | cwd = getcwd(buf, buflen); |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3303 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3304 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3305 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3306 | |
| 3307 | if (cwd == NULL) { |
| 3308 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3309 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3310 | } |
| 3311 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3312 | if (use_bytes) |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3313 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 3314 | else |
| 3315 | obj = PyUnicode_DecodeFSDefault(buf); |
| 3316 | PyMem_RawFree(buf); |
| 3317 | |
| 3318 | return obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3319 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3320 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3321 | |
| 3322 | /*[clinic input] |
| 3323 | os.getcwd |
| 3324 | |
| 3325 | Return a unicode string representing the current working directory. |
| 3326 | [clinic start generated code]*/ |
| 3327 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3328 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3329 | os_getcwd_impl(PyObject *module) |
| 3330 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3331 | { |
| 3332 | return posix_getcwd(0); |
| 3333 | } |
| 3334 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3335 | |
| 3336 | /*[clinic input] |
| 3337 | os.getcwdb |
| 3338 | |
| 3339 | Return a bytes string representing the current working directory. |
| 3340 | [clinic start generated code]*/ |
| 3341 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3342 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3343 | os_getcwdb_impl(PyObject *module) |
| 3344 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3345 | { |
| 3346 | return posix_getcwd(1); |
| 3347 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3348 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3349 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3350 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3351 | #define HAVE_LINK 1 |
| 3352 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3353 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3354 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3355 | /*[clinic input] |
| 3356 | |
| 3357 | os.link |
| 3358 | |
| 3359 | src : path_t |
| 3360 | dst : path_t |
| 3361 | * |
| 3362 | src_dir_fd : dir_fd = None |
| 3363 | dst_dir_fd : dir_fd = None |
| 3364 | follow_symlinks: bool = True |
| 3365 | |
| 3366 | Create a hard link to a file. |
| 3367 | |
| 3368 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3369 | descriptor open to a directory, and the respective path string (src or dst) |
| 3370 | should be relative; the path will then be relative to that directory. |
| 3371 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3372 | link, link will create a link to the symbolic link itself instead of the |
| 3373 | file the link points to. |
| 3374 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3375 | platform. If they are unavailable, using them will raise a |
| 3376 | NotImplementedError. |
| 3377 | [clinic start generated code]*/ |
| 3378 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3379 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3380 | os_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] | 3381 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3382 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3383 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3384 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3385 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3386 | #else |
| 3387 | int result; |
| 3388 | #endif |
| 3389 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3390 | #ifndef HAVE_LINKAT |
| 3391 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3392 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3393 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3394 | } |
| 3395 | #endif |
| 3396 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3397 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3398 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3399 | PyErr_SetString(PyExc_NotImplementedError, |
| 3400 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3401 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3402 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3403 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3404 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3405 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3406 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3407 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3408 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3409 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3410 | if (!result) |
| 3411 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3412 | #else |
| 3413 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3414 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3415 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3416 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3417 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3418 | result = linkat(src_dir_fd, src->narrow, |
| 3419 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3420 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3421 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3422 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3423 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3424 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3425 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3426 | if (result) |
| 3427 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3428 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3429 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3430 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3431 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3432 | #endif |
| 3433 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3434 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3435 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3436 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3437 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3438 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3439 | PyObject *v; |
| 3440 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3441 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3442 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3443 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3444 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3445 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3446 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3447 | WIN32_FIND_DATAW wFileData; |
| 3448 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3449 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3450 | if (!path->wide) { /* Default arg: "." */ |
| 3451 | po_wchars = L"."; |
| 3452 | len = 1; |
| 3453 | } else { |
| 3454 | po_wchars = path->wide; |
| 3455 | len = wcslen(path->wide); |
| 3456 | } |
| 3457 | /* The +5 is so we can append "\\*.*\0" */ |
| 3458 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3459 | if (!wnamebuf) { |
| 3460 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3461 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3462 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3463 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3464 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3465 | wchar_t wch = wnamebuf[len-1]; |
| 3466 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3467 | wnamebuf[len++] = SEP; |
| 3468 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3469 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3470 | if ((list = PyList_New(0)) == NULL) { |
| 3471 | goto exit; |
| 3472 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3473 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3474 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3475 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3476 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3477 | int error = GetLastError(); |
| 3478 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3479 | goto exit; |
| 3480 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3481 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3482 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3483 | } |
| 3484 | do { |
| 3485 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3486 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3487 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3488 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3489 | wcslen(wFileData.cFileName)); |
| 3490 | if (path->narrow && v) { |
| 3491 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3492 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3493 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3494 | Py_DECREF(list); |
| 3495 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3496 | break; |
| 3497 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3498 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3499 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3500 | Py_DECREF(list); |
| 3501 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3502 | break; |
| 3503 | } |
| 3504 | Py_DECREF(v); |
| 3505 | } |
| 3506 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3507 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3508 | Py_END_ALLOW_THREADS |
| 3509 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3510 | it got to the end of the directory. */ |
| 3511 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3512 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3513 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3514 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3515 | } |
| 3516 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3517 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3518 | exit: |
| 3519 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3520 | if (FindClose(hFindFile) == FALSE) { |
| 3521 | if (list != NULL) { |
| 3522 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3523 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3524 | } |
| 3525 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3526 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3527 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3528 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3529 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3530 | } /* end of _listdir_windows_no_opendir */ |
| 3531 | |
| 3532 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3533 | |
| 3534 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3535 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3536 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3537 | PyObject *v; |
| 3538 | DIR *dirp = NULL; |
| 3539 | struct dirent *ep; |
| 3540 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3541 | #ifdef HAVE_FDOPENDIR |
| 3542 | int fd = -1; |
| 3543 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3544 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3545 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3546 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3547 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3548 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3549 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3550 | if (fd == -1) |
| 3551 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3552 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3553 | return_str = 1; |
| 3554 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3555 | Py_BEGIN_ALLOW_THREADS |
| 3556 | dirp = fdopendir(fd); |
| 3557 | Py_END_ALLOW_THREADS |
| 3558 | } |
| 3559 | else |
| 3560 | #endif |
| 3561 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3562 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3563 | if (path->narrow) { |
| 3564 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3565 | /* only return bytes if they specified a bytes-like object */ |
| 3566 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3567 | } |
| 3568 | else { |
| 3569 | name = "."; |
| 3570 | return_str = 1; |
| 3571 | } |
| 3572 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3573 | Py_BEGIN_ALLOW_THREADS |
| 3574 | dirp = opendir(name); |
| 3575 | Py_END_ALLOW_THREADS |
| 3576 | } |
| 3577 | |
| 3578 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3579 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3580 | #ifdef HAVE_FDOPENDIR |
| 3581 | if (fd != -1) { |
| 3582 | Py_BEGIN_ALLOW_THREADS |
| 3583 | close(fd); |
| 3584 | Py_END_ALLOW_THREADS |
| 3585 | } |
| 3586 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3587 | goto exit; |
| 3588 | } |
| 3589 | if ((list = PyList_New(0)) == NULL) { |
| 3590 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3591 | } |
| 3592 | for (;;) { |
| 3593 | errno = 0; |
| 3594 | Py_BEGIN_ALLOW_THREADS |
| 3595 | ep = readdir(dirp); |
| 3596 | Py_END_ALLOW_THREADS |
| 3597 | if (ep == NULL) { |
| 3598 | if (errno == 0) { |
| 3599 | break; |
| 3600 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3601 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3602 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3603 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3604 | } |
| 3605 | } |
| 3606 | if (ep->d_name[0] == '.' && |
| 3607 | (NAMLEN(ep) == 1 || |
| 3608 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3609 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3610 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3611 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3612 | else |
| 3613 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3614 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3615 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3616 | break; |
| 3617 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3618 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3619 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3620 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3621 | break; |
| 3622 | } |
| 3623 | Py_DECREF(v); |
| 3624 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3625 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3626 | exit: |
| 3627 | if (dirp != NULL) { |
| 3628 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3629 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3630 | if (fd > -1) |
| 3631 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3632 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3633 | closedir(dirp); |
| 3634 | Py_END_ALLOW_THREADS |
| 3635 | } |
| 3636 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3637 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3638 | } /* end of _posix_listdir */ |
| 3639 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3640 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3641 | |
| 3642 | /*[clinic input] |
| 3643 | os.listdir |
| 3644 | |
| 3645 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3646 | |
| 3647 | Return a list containing the names of the files in the directory. |
| 3648 | |
| 3649 | path can be specified as either str or bytes. If path is bytes, |
| 3650 | the filenames returned will also be bytes; in all other circumstances |
| 3651 | the filenames returned will be str. |
| 3652 | If path is None, uses the path='.'. |
| 3653 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3654 | the file descriptor must refer to a directory. |
| 3655 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3656 | |
| 3657 | The list is in arbitrary order. It does not include the special |
| 3658 | entries '.' and '..' even if they are present in the directory. |
| 3659 | |
| 3660 | |
| 3661 | [clinic start generated code]*/ |
| 3662 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3663 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3664 | os_listdir_impl(PyObject *module, path_t *path) |
| 3665 | /*[clinic end generated code: output=293045673fcd1a75 input=09e300416e3cd729]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3666 | { |
| 3667 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3668 | return _listdir_windows_no_opendir(path, NULL); |
| 3669 | #else |
| 3670 | return _posix_listdir(path, NULL); |
| 3671 | #endif |
| 3672 | } |
| 3673 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3674 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3675 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3676 | /*[clinic input] |
| 3677 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3678 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3679 | path: path_t |
| 3680 | / |
| 3681 | |
| 3682 | [clinic start generated code]*/ |
| 3683 | |
| 3684 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3685 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 3686 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3687 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3688 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
| 3689 | wchar_t *wtemp; |
| 3690 | DWORD result; |
| 3691 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3692 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3693 | result = GetFullPathNameW(path->wide, |
| 3694 | Py_ARRAY_LENGTH(woutbuf), |
| 3695 | woutbuf, &wtemp); |
| 3696 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
| 3697 | woutbufp = PyMem_New(wchar_t, result); |
| 3698 | if (!woutbufp) |
| 3699 | return PyErr_NoMemory(); |
| 3700 | result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3701 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3702 | if (result) { |
| 3703 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
| 3704 | if (path->narrow) |
| 3705 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3706 | } else |
| 3707 | v = win32_error_object("GetFullPathNameW", path->object); |
| 3708 | if (woutbufp != woutbuf) |
| 3709 | PyMem_Free(woutbufp); |
| 3710 | return v; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3711 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3712 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3713 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3714 | /*[clinic input] |
| 3715 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3716 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3717 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3718 | / |
| 3719 | |
| 3720 | A helper function for samepath on windows. |
| 3721 | [clinic start generated code]*/ |
| 3722 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3723 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3724 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 3725 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3726 | { |
| 3727 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3728 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 3729 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3730 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3731 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3732 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3733 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3734 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3735 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3736 | 0, /* desired access */ |
| 3737 | 0, /* share mode */ |
| 3738 | NULL, /* security attributes */ |
| 3739 | OPEN_EXISTING, |
| 3740 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3741 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3742 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3743 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3744 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3745 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3746 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3747 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3748 | |
| 3749 | /* We have a good handle to the target, use it to determine the |
| 3750 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3751 | while (1) { |
| 3752 | Py_BEGIN_ALLOW_THREADS |
| 3753 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 3754 | buf_size, VOLUME_NAME_DOS); |
| 3755 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3756 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3757 | if (!result_length) { |
| 3758 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 3759 | path->object); |
| 3760 | goto cleanup; |
| 3761 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3762 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3763 | if (result_length < buf_size) { |
| 3764 | break; |
| 3765 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3766 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3767 | wchar_t *tmp; |
| 3768 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 3769 | result_length * sizeof(*tmp)); |
| 3770 | if (!tmp) { |
| 3771 | result = PyErr_NoMemory(); |
| 3772 | goto cleanup; |
| 3773 | } |
| 3774 | |
| 3775 | buf_size = result_length; |
| 3776 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3777 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3778 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3779 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3780 | if (path->narrow) |
| 3781 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3782 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3783 | cleanup: |
| 3784 | if (target_path != buf) { |
| 3785 | PyMem_Free(target_path); |
| 3786 | } |
| 3787 | CloseHandle(hFile); |
| 3788 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3789 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3790 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3791 | /*[clinic input] |
| 3792 | os._isdir |
| 3793 | |
| 3794 | path: path_t |
| 3795 | / |
| 3796 | |
Serhiy Storchaka | 579f038 | 2016-11-08 20:21:22 +0200 | [diff] [blame] | 3797 | Return true if the pathname refers to an existing directory. |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3798 | [clinic start generated code]*/ |
| 3799 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3800 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3801 | os__isdir_impl(PyObject *module, path_t *path) |
Serhiy Storchaka | 579f038 | 2016-11-08 20:21:22 +0200 | [diff] [blame] | 3802 | /*[clinic end generated code: output=75f56f32720836cb input=5e0800149c0ad95f]*/ |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3803 | { |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3804 | DWORD attributes; |
| 3805 | |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3806 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3807 | attributes = GetFileAttributesW(path->wide); |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3808 | Py_END_ALLOW_THREADS |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3809 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3810 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3811 | Py_RETURN_FALSE; |
| 3812 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3813 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3814 | Py_RETURN_TRUE; |
| 3815 | else |
| 3816 | Py_RETURN_FALSE; |
| 3817 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3818 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3819 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3820 | /*[clinic input] |
| 3821 | os._getvolumepathname |
| 3822 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3823 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3824 | |
| 3825 | A helper function for ismount on Win32. |
| 3826 | [clinic start generated code]*/ |
| 3827 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3828 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3829 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 3830 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3831 | { |
| 3832 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3833 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3834 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3835 | BOOL ret; |
| 3836 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3837 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3838 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3839 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 3840 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3841 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 3842 | return NULL; |
| 3843 | } |
| 3844 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3845 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3846 | if (mountpath == NULL) |
| 3847 | return PyErr_NoMemory(); |
| 3848 | |
| 3849 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3850 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3851 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3852 | Py_END_ALLOW_THREADS |
| 3853 | |
| 3854 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3855 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3856 | goto exit; |
| 3857 | } |
| 3858 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3859 | if (path->narrow) |
| 3860 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3861 | |
| 3862 | exit: |
| 3863 | PyMem_Free(mountpath); |
| 3864 | return result; |
| 3865 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3866 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3867 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3868 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3869 | |
| 3870 | /*[clinic input] |
| 3871 | os.mkdir |
| 3872 | |
| 3873 | path : path_t |
| 3874 | |
| 3875 | mode: int = 0o777 |
| 3876 | |
| 3877 | * |
| 3878 | |
| 3879 | dir_fd : dir_fd(requires='mkdirat') = None |
| 3880 | |
| 3881 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3882 | |
| 3883 | Create a directory. |
| 3884 | |
| 3885 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3886 | and path should be relative; path will then be relative to that directory. |
| 3887 | dir_fd may not be implemented on your platform. |
| 3888 | If it is unavailable, using it will raise a NotImplementedError. |
| 3889 | |
| 3890 | The mode argument is ignored on Windows. |
| 3891 | [clinic start generated code]*/ |
| 3892 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3893 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3894 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 3895 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3896 | { |
| 3897 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3898 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3899 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3900 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3901 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3902 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3903 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3904 | if (!result) |
| 3905 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3906 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3907 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3908 | #if HAVE_MKDIRAT |
| 3909 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3910 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3911 | else |
| 3912 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 3913 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3914 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3915 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3916 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3917 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3918 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3919 | if (result < 0) |
| 3920 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3921 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3922 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3925 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3926 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3927 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3928 | #include <sys/resource.h> |
| 3929 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3930 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3931 | |
| 3932 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3933 | /*[clinic input] |
| 3934 | os.nice |
| 3935 | |
| 3936 | increment: int |
| 3937 | / |
| 3938 | |
| 3939 | Add increment to the priority of process and return the new priority. |
| 3940 | [clinic start generated code]*/ |
| 3941 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3942 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3943 | os_nice_impl(PyObject *module, int increment) |
| 3944 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3945 | { |
| 3946 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3947 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3948 | /* There are two flavours of 'nice': one that returns the new |
| 3949 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 3950 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3951 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3952 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3953 | If we are of the nice family that returns the new priority, we |
| 3954 | need to clear errno before the call, and check if errno is filled |
| 3955 | before calling posix_error() on a returnvalue of -1, because the |
| 3956 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3957 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3958 | errno = 0; |
| 3959 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3960 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3961 | if (value == 0) |
| 3962 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3963 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3964 | if (value == -1 && errno != 0) |
| 3965 | /* either nice() or getpriority() returned an error */ |
| 3966 | return posix_error(); |
| 3967 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3968 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3969 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3970 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3971 | |
| 3972 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3973 | /*[clinic input] |
| 3974 | os.getpriority |
| 3975 | |
| 3976 | which: int |
| 3977 | who: int |
| 3978 | |
| 3979 | Return program scheduling priority. |
| 3980 | [clinic start generated code]*/ |
| 3981 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3982 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3983 | os_getpriority_impl(PyObject *module, int which, int who) |
| 3984 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3985 | { |
| 3986 | int retval; |
| 3987 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3988 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3989 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3990 | if (errno != 0) |
| 3991 | return posix_error(); |
| 3992 | return PyLong_FromLong((long)retval); |
| 3993 | } |
| 3994 | #endif /* HAVE_GETPRIORITY */ |
| 3995 | |
| 3996 | |
| 3997 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3998 | /*[clinic input] |
| 3999 | os.setpriority |
| 4000 | |
| 4001 | which: int |
| 4002 | who: int |
| 4003 | priority: int |
| 4004 | |
| 4005 | Set program scheduling priority. |
| 4006 | [clinic start generated code]*/ |
| 4007 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4008 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4009 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4010 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4011 | { |
| 4012 | int retval; |
| 4013 | |
| 4014 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4015 | if (retval == -1) |
| 4016 | return posix_error(); |
| 4017 | Py_RETURN_NONE; |
| 4018 | } |
| 4019 | #endif /* HAVE_SETPRIORITY */ |
| 4020 | |
| 4021 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4022 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4023 | 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] | 4024 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4025 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4026 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4027 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4028 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4029 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4030 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4031 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4032 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4033 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4034 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4035 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4036 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4037 | #ifndef HAVE_RENAMEAT |
| 4038 | if (dir_fd_specified) { |
| 4039 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4040 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4041 | } |
| 4042 | #endif |
| 4043 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4044 | #ifdef MS_WINDOWS |
| 4045 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4046 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4047 | Py_END_ALLOW_THREADS |
| 4048 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4049 | if (!result) |
| 4050 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4051 | |
| 4052 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4053 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4054 | PyErr_Format(PyExc_ValueError, |
| 4055 | "%s: src and dst must be the same type", function_name); |
| 4056 | return NULL; |
| 4057 | } |
| 4058 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4059 | Py_BEGIN_ALLOW_THREADS |
| 4060 | #ifdef HAVE_RENAMEAT |
| 4061 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4062 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4063 | else |
| 4064 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4065 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4066 | Py_END_ALLOW_THREADS |
| 4067 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4068 | if (result) |
| 4069 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4070 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4071 | Py_RETURN_NONE; |
| 4072 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4073 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4074 | |
| 4075 | /*[clinic input] |
| 4076 | os.rename |
| 4077 | |
| 4078 | src : path_t |
| 4079 | dst : path_t |
| 4080 | * |
| 4081 | src_dir_fd : dir_fd = None |
| 4082 | dst_dir_fd : dir_fd = None |
| 4083 | |
| 4084 | Rename a file or directory. |
| 4085 | |
| 4086 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4087 | descriptor open to a directory, and the respective path string (src or dst) |
| 4088 | should be relative; the path will then be relative to that directory. |
| 4089 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4090 | If they are unavailable, using them will raise a NotImplementedError. |
| 4091 | [clinic start generated code]*/ |
| 4092 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4093 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4094 | 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] | 4095 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4096 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4097 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4098 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4099 | } |
| 4100 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4101 | |
| 4102 | /*[clinic input] |
| 4103 | os.replace = os.rename |
| 4104 | |
| 4105 | Rename a file or directory, overwriting the destination. |
| 4106 | |
| 4107 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4108 | descriptor open to a directory, and the respective path string (src or dst) |
| 4109 | should be relative; the path will then be relative to that directory. |
| 4110 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4111 | If they are unavailable, using them will raise a NotImplementedError." |
| 4112 | [clinic start generated code]*/ |
| 4113 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4114 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4115 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4116 | int dst_dir_fd) |
| 4117 | /*[clinic end generated code: output=1968c02e7857422b input=25515dfb107c8421]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4118 | { |
| 4119 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4120 | } |
| 4121 | |
| 4122 | |
| 4123 | /*[clinic input] |
| 4124 | os.rmdir |
| 4125 | |
| 4126 | path: path_t |
| 4127 | * |
| 4128 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4129 | |
| 4130 | Remove a directory. |
| 4131 | |
| 4132 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4133 | and path should be relative; path will then be relative to that directory. |
| 4134 | dir_fd may not be implemented on your platform. |
| 4135 | If it is unavailable, using it will raise a NotImplementedError. |
| 4136 | [clinic start generated code]*/ |
| 4137 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4138 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4139 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4140 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4141 | { |
| 4142 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4143 | |
| 4144 | Py_BEGIN_ALLOW_THREADS |
| 4145 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4146 | /* Windows, success=1, UNIX, success=0 */ |
| 4147 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4148 | #else |
| 4149 | #ifdef HAVE_UNLINKAT |
| 4150 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4151 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4152 | else |
| 4153 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4154 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4155 | #endif |
| 4156 | Py_END_ALLOW_THREADS |
| 4157 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4158 | if (result) |
| 4159 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4160 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4161 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4164 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4165 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4166 | #ifdef MS_WINDOWS |
| 4167 | /*[clinic input] |
| 4168 | os.system -> long |
| 4169 | |
| 4170 | command: Py_UNICODE |
| 4171 | |
| 4172 | Execute the command in a subshell. |
| 4173 | [clinic start generated code]*/ |
| 4174 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4175 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4176 | os_system_impl(PyObject *module, Py_UNICODE *command) |
| 4177 | /*[clinic end generated code: output=96c4dffee36dfb48 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4178 | { |
| 4179 | long result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4180 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4181 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4182 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4183 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4184 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4185 | return result; |
| 4186 | } |
| 4187 | #else /* MS_WINDOWS */ |
| 4188 | /*[clinic input] |
| 4189 | os.system -> long |
| 4190 | |
| 4191 | command: FSConverter |
| 4192 | |
| 4193 | Execute the command in a subshell. |
| 4194 | [clinic start generated code]*/ |
| 4195 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4196 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4197 | os_system_impl(PyObject *module, PyObject *command) |
| 4198 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4199 | { |
| 4200 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4201 | const char *bytes = PyBytes_AsString(command); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4202 | Py_BEGIN_ALLOW_THREADS |
| 4203 | result = system(bytes); |
| 4204 | Py_END_ALLOW_THREADS |
| 4205 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4206 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4207 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4208 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4209 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4210 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4211 | /*[clinic input] |
| 4212 | os.umask |
| 4213 | |
| 4214 | mask: int |
| 4215 | / |
| 4216 | |
| 4217 | Set the current numeric umask and return the previous umask. |
| 4218 | [clinic start generated code]*/ |
| 4219 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4220 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4221 | os_umask_impl(PyObject *module, int mask) |
| 4222 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4223 | { |
| 4224 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4225 | if (i < 0) |
| 4226 | return posix_error(); |
| 4227 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4228 | } |
| 4229 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4230 | #ifdef MS_WINDOWS |
| 4231 | |
| 4232 | /* override the default DeleteFileW behavior so that directory |
| 4233 | symlinks can be removed with this function, the same as with |
| 4234 | Unix symlinks */ |
| 4235 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4236 | { |
| 4237 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4238 | WIN32_FIND_DATAW find_data; |
| 4239 | HANDLE find_data_handle; |
| 4240 | int is_directory = 0; |
| 4241 | int is_link = 0; |
| 4242 | |
| 4243 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4244 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4245 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4246 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4247 | it is a symlink */ |
| 4248 | if(is_directory && |
| 4249 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4250 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4251 | |
| 4252 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4253 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4254 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4255 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4256 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4257 | FindClose(find_data_handle); |
| 4258 | } |
| 4259 | } |
| 4260 | } |
| 4261 | |
| 4262 | if (is_directory && is_link) |
| 4263 | return RemoveDirectoryW(lpFileName); |
| 4264 | |
| 4265 | return DeleteFileW(lpFileName); |
| 4266 | } |
| 4267 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4268 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4269 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4270 | /*[clinic input] |
| 4271 | os.unlink |
| 4272 | |
| 4273 | path: path_t |
| 4274 | * |
| 4275 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4276 | |
| 4277 | Remove a file (same as remove()). |
| 4278 | |
| 4279 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4280 | and path should be relative; path will then be relative to that directory. |
| 4281 | dir_fd may not be implemented on your platform. |
| 4282 | If it is unavailable, using it will raise a NotImplementedError. |
| 4283 | |
| 4284 | [clinic start generated code]*/ |
| 4285 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4286 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4287 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4288 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4289 | { |
| 4290 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4291 | |
| 4292 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4293 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4294 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4295 | /* Windows, success=1, UNIX, success=0 */ |
| 4296 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4297 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4298 | #ifdef HAVE_UNLINKAT |
| 4299 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4300 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4301 | else |
| 4302 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4303 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4304 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4305 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4306 | Py_END_ALLOW_THREADS |
| 4307 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4308 | if (result) |
| 4309 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4310 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4311 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4314 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4315 | /*[clinic input] |
| 4316 | os.remove = os.unlink |
| 4317 | |
| 4318 | Remove a file (same as unlink()). |
| 4319 | |
| 4320 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4321 | and path should be relative; path will then be relative to that directory. |
| 4322 | dir_fd may not be implemented on your platform. |
| 4323 | If it is unavailable, using it will raise a NotImplementedError. |
| 4324 | [clinic start generated code]*/ |
| 4325 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4326 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4327 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4328 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4329 | { |
| 4330 | return os_unlink_impl(module, path, dir_fd); |
| 4331 | } |
| 4332 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4333 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4334 | static PyStructSequence_Field uname_result_fields[] = { |
| 4335 | {"sysname", "operating system name"}, |
| 4336 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4337 | {"release", "operating system release"}, |
| 4338 | {"version", "operating system version"}, |
| 4339 | {"machine", "hardware identifier"}, |
| 4340 | {NULL} |
| 4341 | }; |
| 4342 | |
| 4343 | PyDoc_STRVAR(uname_result__doc__, |
| 4344 | "uname_result: Result from os.uname().\n\n\ |
| 4345 | This object may be accessed either as a tuple of\n\ |
| 4346 | (sysname, nodename, release, version, machine),\n\ |
| 4347 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4348 | \n\ |
| 4349 | See os.uname for more information."); |
| 4350 | |
| 4351 | static PyStructSequence_Desc uname_result_desc = { |
| 4352 | "uname_result", /* name */ |
| 4353 | uname_result__doc__, /* doc */ |
| 4354 | uname_result_fields, |
| 4355 | 5 |
| 4356 | }; |
| 4357 | |
| 4358 | static PyTypeObject UnameResultType; |
| 4359 | |
| 4360 | |
| 4361 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4362 | /*[clinic input] |
| 4363 | os.uname |
| 4364 | |
| 4365 | Return an object identifying the current operating system. |
| 4366 | |
| 4367 | The object behaves like a named tuple with the following fields: |
| 4368 | (sysname, nodename, release, version, machine) |
| 4369 | |
| 4370 | [clinic start generated code]*/ |
| 4371 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4372 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4373 | os_uname_impl(PyObject *module) |
| 4374 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4375 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4376 | struct utsname u; |
| 4377 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4378 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4379 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4380 | Py_BEGIN_ALLOW_THREADS |
| 4381 | res = uname(&u); |
| 4382 | Py_END_ALLOW_THREADS |
| 4383 | if (res < 0) |
| 4384 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4385 | |
| 4386 | value = PyStructSequence_New(&UnameResultType); |
| 4387 | if (value == NULL) |
| 4388 | return NULL; |
| 4389 | |
| 4390 | #define SET(i, field) \ |
| 4391 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4392 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4393 | if (!o) { \ |
| 4394 | Py_DECREF(value); \ |
| 4395 | return NULL; \ |
| 4396 | } \ |
| 4397 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4398 | } \ |
| 4399 | |
| 4400 | SET(0, u.sysname); |
| 4401 | SET(1, u.nodename); |
| 4402 | SET(2, u.release); |
| 4403 | SET(3, u.version); |
| 4404 | SET(4, u.machine); |
| 4405 | |
| 4406 | #undef SET |
| 4407 | |
| 4408 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4409 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4410 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4411 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4412 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4413 | |
| 4414 | typedef struct { |
| 4415 | int now; |
| 4416 | time_t atime_s; |
| 4417 | long atime_ns; |
| 4418 | time_t mtime_s; |
| 4419 | long mtime_ns; |
| 4420 | } utime_t; |
| 4421 | |
| 4422 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4423 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4424 | * they also intentionally leak the declaration of a pointer named "time" |
| 4425 | */ |
| 4426 | #define UTIME_TO_TIMESPEC \ |
| 4427 | struct timespec ts[2]; \ |
| 4428 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4429 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4430 | time = NULL; \ |
| 4431 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4432 | ts[0].tv_sec = ut->atime_s; \ |
| 4433 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4434 | ts[1].tv_sec = ut->mtime_s; \ |
| 4435 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4436 | time = ts; \ |
| 4437 | } \ |
| 4438 | |
| 4439 | #define UTIME_TO_TIMEVAL \ |
| 4440 | struct timeval tv[2]; \ |
| 4441 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4442 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4443 | time = NULL; \ |
| 4444 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4445 | tv[0].tv_sec = ut->atime_s; \ |
| 4446 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4447 | tv[1].tv_sec = ut->mtime_s; \ |
| 4448 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4449 | time = tv; \ |
| 4450 | } \ |
| 4451 | |
| 4452 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4453 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4454 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4455 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4456 | time = NULL; \ |
| 4457 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4458 | u.actime = ut->atime_s; \ |
| 4459 | u.modtime = ut->mtime_s; \ |
| 4460 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4461 | } |
| 4462 | |
| 4463 | #define UTIME_TO_TIME_T \ |
| 4464 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4465 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4466 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4467 | time = NULL; \ |
| 4468 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4469 | timet[0] = ut->atime_s; \ |
| 4470 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4471 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4472 | } \ |
| 4473 | |
| 4474 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4475 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4476 | |
| 4477 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4478 | 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] | 4479 | { |
| 4480 | #ifdef HAVE_UTIMENSAT |
| 4481 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4482 | UTIME_TO_TIMESPEC; |
| 4483 | return utimensat(dir_fd, path, time, flags); |
| 4484 | #elif defined(HAVE_FUTIMESAT) |
| 4485 | UTIME_TO_TIMEVAL; |
| 4486 | /* |
| 4487 | * follow_symlinks will never be false here; |
| 4488 | * we only allow !follow_symlinks and dir_fd together |
| 4489 | * if we have utimensat() |
| 4490 | */ |
| 4491 | assert(follow_symlinks); |
| 4492 | return futimesat(dir_fd, path, time); |
| 4493 | #endif |
| 4494 | } |
| 4495 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4496 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4497 | #else |
| 4498 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4499 | #endif |
| 4500 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4501 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4502 | |
| 4503 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4504 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4505 | { |
| 4506 | #ifdef HAVE_FUTIMENS |
| 4507 | UTIME_TO_TIMESPEC; |
| 4508 | return futimens(fd, time); |
| 4509 | #else |
| 4510 | UTIME_TO_TIMEVAL; |
| 4511 | return futimes(fd, time); |
| 4512 | #endif |
| 4513 | } |
| 4514 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4515 | #define PATH_UTIME_HAVE_FD 1 |
| 4516 | #else |
| 4517 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4518 | #endif |
| 4519 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4520 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4521 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4522 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4523 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4524 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4525 | |
| 4526 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4527 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4528 | { |
| 4529 | #ifdef HAVE_UTIMENSAT |
| 4530 | UTIME_TO_TIMESPEC; |
| 4531 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4532 | #else |
| 4533 | UTIME_TO_TIMEVAL; |
| 4534 | return lutimes(path, time); |
| 4535 | #endif |
| 4536 | } |
| 4537 | |
| 4538 | #endif |
| 4539 | |
| 4540 | #ifndef MS_WINDOWS |
| 4541 | |
| 4542 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4543 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4544 | { |
| 4545 | #ifdef HAVE_UTIMENSAT |
| 4546 | UTIME_TO_TIMESPEC; |
| 4547 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4548 | #elif defined(HAVE_UTIMES) |
| 4549 | UTIME_TO_TIMEVAL; |
| 4550 | return utimes(path, time); |
| 4551 | #elif defined(HAVE_UTIME_H) |
| 4552 | UTIME_TO_UTIMBUF; |
| 4553 | return utime(path, time); |
| 4554 | #else |
| 4555 | UTIME_TO_TIME_T; |
| 4556 | return utime(path, time); |
| 4557 | #endif |
| 4558 | } |
| 4559 | |
| 4560 | #endif |
| 4561 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4562 | static int |
| 4563 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4564 | { |
| 4565 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4566 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4567 | divmod = PyNumber_Divmod(py_long, billion); |
| 4568 | if (!divmod) |
| 4569 | goto exit; |
| 4570 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4571 | if ((*s == -1) && PyErr_Occurred()) |
| 4572 | goto exit; |
| 4573 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4574 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4575 | goto exit; |
| 4576 | |
| 4577 | result = 1; |
| 4578 | exit: |
| 4579 | Py_XDECREF(divmod); |
| 4580 | return result; |
| 4581 | } |
| 4582 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4583 | |
| 4584 | /*[clinic input] |
| 4585 | os.utime |
| 4586 | |
| 4587 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
| 4588 | times: object = NULL |
| 4589 | * |
| 4590 | ns: object = NULL |
| 4591 | dir_fd: dir_fd(requires='futimensat') = None |
| 4592 | follow_symlinks: bool=True |
| 4593 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4594 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4595 | |
| 4596 | Set the access and modified time of path. |
| 4597 | |
| 4598 | path may always be specified as a string. |
| 4599 | On some platforms, path may also be specified as an open file descriptor. |
| 4600 | If this functionality is unavailable, using it raises an exception. |
| 4601 | |
| 4602 | If times is not None, it must be a tuple (atime, mtime); |
| 4603 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4604 | 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] | 4605 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4606 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4607 | 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] | 4608 | Specifying tuples for both times and ns is an error. |
| 4609 | |
| 4610 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4611 | and path should be relative; path will then be relative to that directory. |
| 4612 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4613 | link, utime will modify the symbolic link itself instead of the file the |
| 4614 | link points to. |
| 4615 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4616 | as an open file descriptor. |
| 4617 | dir_fd and follow_symlinks may not be available on your platform. |
| 4618 | If they are unavailable, using them will raise a NotImplementedError. |
| 4619 | |
| 4620 | [clinic start generated code]*/ |
| 4621 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4622 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4623 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4624 | int dir_fd, int follow_symlinks) |
| 4625 | /*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4626 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4627 | #ifdef MS_WINDOWS |
| 4628 | HANDLE hFile; |
| 4629 | FILETIME atime, mtime; |
| 4630 | #else |
| 4631 | int result; |
| 4632 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4633 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4634 | PyObject *return_value = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4635 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4636 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4637 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4638 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4639 | if (times && (times != Py_None) && ns) { |
| 4640 | PyErr_SetString(PyExc_ValueError, |
| 4641 | "utime: you may specify either 'times'" |
| 4642 | " or 'ns' but not both"); |
| 4643 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4647 | time_t a_sec, m_sec; |
| 4648 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4649 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4650 | PyErr_SetString(PyExc_TypeError, |
| 4651 | "utime: 'times' must be either" |
| 4652 | " a tuple of two ints or None"); |
| 4653 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4654 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4655 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4656 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4657 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4658 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4659 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4660 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4661 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4662 | utime.atime_s = a_sec; |
| 4663 | utime.atime_ns = a_nsec; |
| 4664 | utime.mtime_s = m_sec; |
| 4665 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4666 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4667 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4668 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4669 | PyErr_SetString(PyExc_TypeError, |
| 4670 | "utime: 'ns' must be a tuple of two ints"); |
| 4671 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4672 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4673 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4674 | 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] | 4675 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4676 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4677 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4678 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4679 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4680 | } |
| 4681 | else { |
| 4682 | /* times and ns are both None/unspecified. use "now". */ |
| 4683 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4684 | } |
| 4685 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4686 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4687 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4688 | goto exit; |
| 4689 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4690 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4691 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4692 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4693 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4694 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4695 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4696 | #if !defined(HAVE_UTIMENSAT) |
| 4697 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4698 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4699 | "utime: cannot use dir_fd and follow_symlinks " |
| 4700 | "together on this platform"); |
| 4701 | goto exit; |
| 4702 | } |
| 4703 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4704 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4705 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4706 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4707 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 4708 | NULL, OPEN_EXISTING, |
| 4709 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4710 | Py_END_ALLOW_THREADS |
| 4711 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4712 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4713 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4714 | } |
| 4715 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4716 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4717 | GetSystemTimeAsFileTime(&mtime); |
| 4718 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4719 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4720 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4721 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4722 | _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] | 4723 | } |
| 4724 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4725 | /* Avoid putting the file name into the error here, |
| 4726 | as that may confuse the user into believing that |
| 4727 | something is wrong with the file, when it also |
| 4728 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4729 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4730 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4731 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4732 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4733 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4734 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4735 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4736 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4737 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4738 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4739 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4740 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4741 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4742 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4743 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4744 | else |
| 4745 | #endif |
| 4746 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4747 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4748 | if (path->fd != -1) |
| 4749 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4750 | else |
| 4751 | #endif |
| 4752 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4753 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4754 | |
| 4755 | Py_END_ALLOW_THREADS |
| 4756 | |
| 4757 | if (result < 0) { |
| 4758 | /* see previous comment about not putting filename in error here */ |
| 4759 | return_value = posix_error(); |
| 4760 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4761 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4762 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4763 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4764 | |
| 4765 | Py_INCREF(Py_None); |
| 4766 | return_value = Py_None; |
| 4767 | |
| 4768 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4769 | #ifdef MS_WINDOWS |
| 4770 | if (hFile != INVALID_HANDLE_VALUE) |
| 4771 | CloseHandle(hFile); |
| 4772 | #endif |
| 4773 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4776 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4777 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4778 | |
| 4779 | /*[clinic input] |
| 4780 | os._exit |
| 4781 | |
| 4782 | status: int |
| 4783 | |
| 4784 | Exit to the system with specified status, without normal exit processing. |
| 4785 | [clinic start generated code]*/ |
| 4786 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4787 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4788 | os__exit_impl(PyObject *module, int status) |
| 4789 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4790 | { |
| 4791 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4792 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4795 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4796 | #define EXECV_CHAR wchar_t |
| 4797 | #else |
| 4798 | #define EXECV_CHAR char |
| 4799 | #endif |
| 4800 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4801 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4802 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4803 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4804 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4805 | Py_ssize_t i; |
| 4806 | for (i = 0; i < count; i++) |
| 4807 | PyMem_Free(array[i]); |
| 4808 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4809 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4810 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4811 | static int |
| 4812 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4813 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4814 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4815 | PyObject *ub; |
| 4816 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4817 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4818 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4819 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4820 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 4821 | if (*out) |
| 4822 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4823 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4824 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4825 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4826 | size = PyBytes_GET_SIZE(ub); |
| 4827 | *out = PyMem_Malloc(size + 1); |
| 4828 | if (*out) { |
| 4829 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 4830 | result = 1; |
| 4831 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4832 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4833 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4834 | Py_DECREF(ub); |
| 4835 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4836 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4837 | #endif |
| 4838 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4839 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4840 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4841 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4842 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4843 | Py_ssize_t i, pos, envc; |
| 4844 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4845 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4846 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4847 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4848 | i = PyMapping_Size(env); |
| 4849 | if (i < 0) |
| 4850 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4851 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4852 | if (envlist == NULL) { |
| 4853 | PyErr_NoMemory(); |
| 4854 | return NULL; |
| 4855 | } |
| 4856 | envc = 0; |
| 4857 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4858 | if (!keys) |
| 4859 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4860 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4861 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4862 | goto error; |
| 4863 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4864 | PyErr_Format(PyExc_TypeError, |
| 4865 | "env.keys() or env.values() is not a list"); |
| 4866 | goto error; |
| 4867 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4868 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4869 | for (pos = 0; pos < i; pos++) { |
| 4870 | key = PyList_GetItem(keys, pos); |
| 4871 | val = PyList_GetItem(vals, pos); |
| 4872 | if (!key || !val) |
| 4873 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4874 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4875 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4876 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 4877 | goto error; |
| 4878 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 4879 | Py_DECREF(key2); |
| 4880 | goto error; |
| 4881 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4882 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 4883 | defining hidden environment variables. */ |
| 4884 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 4885 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 4886 | { |
| 4887 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 4888 | Py_DECREF(key2); |
| 4889 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4890 | goto error; |
| 4891 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4892 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 4893 | #else |
| 4894 | if (!PyUnicode_FSConverter(key, &key2)) |
| 4895 | goto error; |
| 4896 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 4897 | Py_DECREF(key2); |
| 4898 | goto error; |
| 4899 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4900 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 4901 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 4902 | { |
| 4903 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 4904 | Py_DECREF(key2); |
| 4905 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 4906 | goto error; |
| 4907 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4908 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 4909 | PyBytes_AS_STRING(val2)); |
| 4910 | #endif |
| 4911 | Py_DECREF(key2); |
| 4912 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4913 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4914 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4915 | |
| 4916 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 4917 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4918 | goto error; |
| 4919 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 4920 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4921 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4922 | } |
| 4923 | Py_DECREF(vals); |
| 4924 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4925 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4926 | envlist[envc] = 0; |
| 4927 | *envc_ptr = envc; |
| 4928 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4929 | |
| 4930 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4931 | Py_XDECREF(keys); |
| 4932 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4933 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4934 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4935 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4936 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4937 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4938 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4939 | { |
| 4940 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4941 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4942 | if (argvlist == NULL) { |
| 4943 | PyErr_NoMemory(); |
| 4944 | return NULL; |
| 4945 | } |
| 4946 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4947 | PyObject* item = PySequence_ITEM(argv, i); |
| 4948 | if (item == NULL) |
| 4949 | goto fail; |
| 4950 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4951 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4952 | goto fail; |
| 4953 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4954 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4955 | } |
| 4956 | argvlist[*argc] = NULL; |
| 4957 | return argvlist; |
| 4958 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4959 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4960 | free_string_array(argvlist, *argc); |
| 4961 | return NULL; |
| 4962 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4963 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4964 | #endif |
| 4965 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4966 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4967 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4968 | /*[clinic input] |
| 4969 | os.execv |
| 4970 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4971 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4972 | Path of executable file. |
| 4973 | argv: object |
| 4974 | Tuple or list of strings. |
| 4975 | / |
| 4976 | |
| 4977 | Execute an executable path with arguments, replacing current process. |
| 4978 | [clinic start generated code]*/ |
| 4979 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4980 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4981 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 4982 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4983 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4984 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4985 | Py_ssize_t argc; |
| 4986 | |
| 4987 | /* execv has two arguments: (path, argv), where |
| 4988 | argv is a list or tuple of strings. */ |
| 4989 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4990 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4991 | PyErr_SetString(PyExc_TypeError, |
| 4992 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4993 | return NULL; |
| 4994 | } |
| 4995 | argc = PySequence_Size(argv); |
| 4996 | if (argc < 1) { |
| 4997 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4998 | return NULL; |
| 4999 | } |
| 5000 | |
| 5001 | argvlist = parse_arglist(argv, &argc); |
| 5002 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5003 | return NULL; |
| 5004 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5005 | if (!argvlist[0][0]) { |
| 5006 | PyErr_SetString(PyExc_ValueError, |
| 5007 | "execv() arg 2 first element cannot be empty"); |
| 5008 | free_string_array(argvlist, argc); |
| 5009 | return NULL; |
| 5010 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5011 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5012 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5013 | #ifdef HAVE_WEXECV |
| 5014 | _wexecv(path->wide, argvlist); |
| 5015 | #else |
| 5016 | execv(path->narrow, argvlist); |
| 5017 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5018 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5019 | |
| 5020 | /* If we get here it's definitely an error */ |
| 5021 | |
| 5022 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5023 | return posix_error(); |
| 5024 | } |
| 5025 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5026 | |
| 5027 | /*[clinic input] |
| 5028 | os.execve |
| 5029 | |
| 5030 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5031 | Path of executable file. |
| 5032 | argv: object |
| 5033 | Tuple or list of strings. |
| 5034 | env: object |
| 5035 | Dictionary of strings mapping to strings. |
| 5036 | |
| 5037 | Execute an executable path with arguments, replacing current process. |
| 5038 | [clinic start generated code]*/ |
| 5039 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5040 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5041 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5042 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5043 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5044 | EXECV_CHAR **argvlist = NULL; |
| 5045 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5046 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5047 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5048 | /* execve has three arguments: (path, argv, env), where |
| 5049 | argv is a list or tuple of strings and env is a dictionary |
| 5050 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5051 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5052 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5053 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5054 | "execve: argv must be a tuple or list"); |
| 5055 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5056 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5057 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5058 | if (argc < 1) { |
| 5059 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5060 | return NULL; |
| 5061 | } |
| 5062 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5063 | if (!PyMapping_Check(env)) { |
| 5064 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5065 | "execve: environment must be a mapping object"); |
| 5066 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5067 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5068 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5069 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5070 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5071 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5072 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5073 | if (!argvlist[0][0]) { |
| 5074 | PyErr_SetString(PyExc_ValueError, |
| 5075 | "execve: argv first element cannot be empty"); |
| 5076 | goto fail; |
| 5077 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5078 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5079 | envlist = parse_envlist(env, &envc); |
| 5080 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5081 | goto fail; |
| 5082 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5083 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5084 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5085 | if (path->fd > -1) |
| 5086 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5087 | else |
| 5088 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5089 | #ifdef HAVE_WEXECV |
| 5090 | _wexecve(path->wide, argvlist, envlist); |
| 5091 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5092 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5093 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5094 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5095 | |
| 5096 | /* If we get here it's definitely an error */ |
| 5097 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5098 | path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5099 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5100 | free_string_array(envlist, envc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5101 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5102 | if (argvlist) |
| 5103 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5104 | return NULL; |
| 5105 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5106 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5107 | #endif /* HAVE_EXECV */ |
| 5108 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5109 | #ifdef HAVE_POSIX_SPAWN |
| 5110 | |
| 5111 | enum posix_spawn_file_actions_identifier { |
| 5112 | POSIX_SPAWN_OPEN, |
| 5113 | POSIX_SPAWN_CLOSE, |
| 5114 | POSIX_SPAWN_DUP2 |
| 5115 | }; |
| 5116 | |
| 5117 | /*[clinic input] |
| 5118 | |
| 5119 | os.posix_spawn |
| 5120 | path: path_t |
| 5121 | Path of executable file. |
| 5122 | argv: object |
| 5123 | Tuple or list of strings. |
| 5124 | env: object |
| 5125 | Dictionary of strings mapping to strings. |
| 5126 | file_actions: object = None |
| 5127 | FileActions object. |
| 5128 | / |
| 5129 | |
| 5130 | Execute the program specified by path in a new process. |
| 5131 | [clinic start generated code]*/ |
| 5132 | |
| 5133 | static PyObject * |
| 5134 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5135 | PyObject *env, PyObject *file_actions) |
| 5136 | /*[clinic end generated code: output=d023521f541c709c input=0ec9f1cfdc890be5]*/ |
| 5137 | { |
| 5138 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5139 | EXECV_CHAR **envlist = NULL; |
| 5140 | posix_spawn_file_actions_t _file_actions; |
| 5141 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5142 | Py_ssize_t argc, envc; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5143 | PyObject* result = NULL; |
| 5144 | PyObject* seq = NULL; |
| 5145 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5146 | |
| 5147 | /* posix_spawn has three arguments: (path, argv, env), where |
| 5148 | argv is a list or tuple of strings and env is a dictionary |
| 5149 | like posix.environ. */ |
| 5150 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5151 | if (!PySequence_Check(argv)) { |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5152 | PyErr_SetString(PyExc_TypeError, |
| 5153 | "posix_spawn: argv must be a tuple or list"); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5154 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5155 | } |
| 5156 | argc = PySequence_Size(argv); |
| 5157 | if (argc < 1) { |
| 5158 | PyErr_SetString(PyExc_ValueError, "posix_spawn: argv must not be empty"); |
| 5159 | return NULL; |
| 5160 | } |
| 5161 | |
| 5162 | if (!PyMapping_Check(env)) { |
| 5163 | PyErr_SetString(PyExc_TypeError, |
| 5164 | "posix_spawn: environment must be a mapping object"); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5165 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | argvlist = parse_arglist(argv, &argc); |
| 5169 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5170 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5171 | } |
| 5172 | if (!argvlist[0][0]) { |
| 5173 | PyErr_SetString(PyExc_ValueError, |
| 5174 | "posix_spawn: argv first element cannot be empty"); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5175 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
| 5178 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5179 | if (envlist == NULL) { |
| 5180 | goto exit; |
| 5181 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5182 | |
| 5183 | pid_t pid; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5184 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5185 | if(posix_spawn_file_actions_init(&_file_actions) != 0){ |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5186 | PyErr_SetString(PyExc_OSError, |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5187 | "Error initializing file actions"); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5188 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5191 | file_actionsp = &_file_actions; |
| 5192 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5193 | seq = PySequence_Fast(file_actions, "file_actions must be a sequence"); |
| 5194 | if(seq == NULL) { |
| 5195 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5196 | } |
| 5197 | PyObject* file_actions_obj; |
| 5198 | PyObject* mode_obj; |
| 5199 | |
| 5200 | for (int i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) { |
| 5201 | file_actions_obj = PySequence_Fast_GET_ITEM(seq, i); |
| 5202 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5203 | if(!PySequence_Check(file_actions_obj) | !PySequence_Size(file_actions_obj)) { |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5204 | PyErr_SetString(PyExc_TypeError,"Each file_action element must be a non empty sequence"); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5205 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5206 | } |
| 5207 | |
| 5208 | |
| 5209 | mode_obj = PySequence_Fast_GET_ITEM(file_actions_obj, 0); |
| 5210 | int mode = PyLong_AsLong(mode_obj); |
| 5211 | |
| 5212 | /* Populate the file_actions object */ |
| 5213 | |
| 5214 | switch(mode) { |
| 5215 | |
| 5216 | case POSIX_SPAWN_OPEN: |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5217 | if(PySequence_Size(file_actions_obj) != 5) { |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5218 | PyErr_SetString(PyExc_TypeError,"A open file_action object must have 5 elements"); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5219 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5220 | } |
| 5221 | |
| 5222 | long open_fd = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 1)); |
| 5223 | if(PyErr_Occurred()) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5224 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5225 | } |
| 5226 | const char* open_path = PyUnicode_AsUTF8(PySequence_GetItem(file_actions_obj, 2)); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5227 | if(open_path == NULL) { |
| 5228 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5229 | } |
| 5230 | long open_oflag = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 3)); |
| 5231 | if(PyErr_Occurred()) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5232 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5233 | } |
| 5234 | long open_mode = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 4)); |
| 5235 | if(PyErr_Occurred()) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5236 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5237 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5238 | if(posix_spawn_file_actions_addopen(file_actionsp, open_fd, open_path, open_oflag, open_mode)) { |
| 5239 | PyErr_SetString(PyExc_OSError,"Failed to add open file action"); |
| 5240 | goto exit; |
| 5241 | } |
| 5242 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5243 | break; |
| 5244 | |
| 5245 | case POSIX_SPAWN_CLOSE: |
| 5246 | if(PySequence_Size(file_actions_obj) != 2){ |
| 5247 | PyErr_SetString(PyExc_TypeError,"A close file_action object must have 2 elements"); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5248 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | long close_fd = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 1)); |
| 5252 | if(PyErr_Occurred()) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5253 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5254 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5255 | if(posix_spawn_file_actions_addclose(file_actionsp, close_fd)) { |
| 5256 | PyErr_SetString(PyExc_OSError,"Failed to add close file action"); |
| 5257 | goto exit; |
| 5258 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5259 | break; |
| 5260 | |
| 5261 | case POSIX_SPAWN_DUP2: |
| 5262 | if(PySequence_Size(file_actions_obj) != 3){ |
| 5263 | PyErr_SetString(PyExc_TypeError,"A dup2 file_action object must have 3 elements"); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5264 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5265 | } |
| 5266 | |
| 5267 | long fd1 = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 1)); |
| 5268 | if(PyErr_Occurred()) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5269 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5270 | } |
| 5271 | long fd2 = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 2)); |
| 5272 | if(PyErr_Occurred()) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5273 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5274 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5275 | if(posix_spawn_file_actions_adddup2(file_actionsp, fd1, fd2)) { |
| 5276 | PyErr_SetString(PyExc_OSError,"Failed to add dup2 file action"); |
| 5277 | goto exit; |
| 5278 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5279 | break; |
| 5280 | |
| 5281 | default: |
| 5282 | PyErr_SetString(PyExc_TypeError,"Unknown file_actions identifier"); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5283 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5284 | } |
| 5285 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5286 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5287 | |
| 5288 | _Py_BEGIN_SUPPRESS_IPH |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5289 | int err_code = posix_spawn(&pid, path->narrow, file_actionsp, NULL, argvlist, envlist); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5290 | _Py_END_SUPPRESS_IPH |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5291 | if(err_code) { |
| 5292 | PyErr_SetString(PyExc_OSError,"posix_spawn call failed"); |
| 5293 | goto exit; |
| 5294 | } |
| 5295 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5296 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5297 | exit: |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5298 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5299 | Py_XDECREF(seq); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5300 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5301 | if(file_actionsp) { |
| 5302 | posix_spawn_file_actions_destroy(file_actionsp); |
| 5303 | } |
| 5304 | |
| 5305 | if (envlist) { |
| 5306 | free_string_array(envlist, envc); |
| 5307 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5308 | |
| 5309 | if (argvlist) { |
| 5310 | free_string_array(argvlist, argc); |
| 5311 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5312 | |
| 5313 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5314 | |
| 5315 | |
| 5316 | } |
| 5317 | #endif /* HAVE_POSIX_SPAWN */ |
| 5318 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5319 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5320 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5321 | /*[clinic input] |
| 5322 | os.spawnv |
| 5323 | |
| 5324 | mode: int |
| 5325 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5326 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5327 | Path of executable file. |
| 5328 | argv: object |
| 5329 | Tuple or list of strings. |
| 5330 | / |
| 5331 | |
| 5332 | Execute the program specified by path in a new process. |
| 5333 | [clinic start generated code]*/ |
| 5334 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5335 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5336 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5337 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5338 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5339 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5340 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5341 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5342 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5343 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5344 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5345 | /* spawnv has three arguments: (mode, path, argv), where |
| 5346 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5347 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5348 | if (PyList_Check(argv)) { |
| 5349 | argc = PyList_Size(argv); |
| 5350 | getitem = PyList_GetItem; |
| 5351 | } |
| 5352 | else if (PyTuple_Check(argv)) { |
| 5353 | argc = PyTuple_Size(argv); |
| 5354 | getitem = PyTuple_GetItem; |
| 5355 | } |
| 5356 | else { |
| 5357 | PyErr_SetString(PyExc_TypeError, |
| 5358 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5359 | return NULL; |
| 5360 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5361 | if (argc == 0) { |
| 5362 | PyErr_SetString(PyExc_ValueError, |
| 5363 | "spawnv() arg 2 cannot be empty"); |
| 5364 | return NULL; |
| 5365 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5366 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5367 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5368 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5369 | return PyErr_NoMemory(); |
| 5370 | } |
| 5371 | for (i = 0; i < argc; i++) { |
| 5372 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5373 | &argvlist[i])) { |
| 5374 | free_string_array(argvlist, i); |
| 5375 | PyErr_SetString( |
| 5376 | PyExc_TypeError, |
| 5377 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5378 | return NULL; |
| 5379 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5380 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5381 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5382 | PyErr_SetString( |
| 5383 | PyExc_ValueError, |
| 5384 | "spawnv() arg 2 first element cannot be empty"); |
| 5385 | return NULL; |
| 5386 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5387 | } |
| 5388 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5389 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5390 | if (mode == _OLD_P_OVERLAY) |
| 5391 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5392 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5393 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5394 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5395 | #ifdef HAVE_WSPAWNV |
| 5396 | spawnval = _wspawnv(mode, path->wide, argvlist); |
| 5397 | #else |
| 5398 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 5399 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5400 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5401 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5402 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5403 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5404 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5405 | if (spawnval == -1) |
| 5406 | return posix_error(); |
| 5407 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5408 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5409 | } |
| 5410 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5411 | /*[clinic input] |
| 5412 | os.spawnve |
| 5413 | |
| 5414 | mode: int |
| 5415 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5416 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5417 | Path of executable file. |
| 5418 | argv: object |
| 5419 | Tuple or list of strings. |
| 5420 | env: object |
| 5421 | Dictionary of strings mapping to strings. |
| 5422 | / |
| 5423 | |
| 5424 | Execute the program specified by path in a new process. |
| 5425 | [clinic start generated code]*/ |
| 5426 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5427 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5428 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5429 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5430 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5431 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5432 | EXECV_CHAR **argvlist; |
| 5433 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5434 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5435 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5436 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5437 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5438 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5439 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5440 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5441 | argv is a list or tuple of strings and env is a dictionary |
| 5442 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5443 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5444 | if (PyList_Check(argv)) { |
| 5445 | argc = PyList_Size(argv); |
| 5446 | getitem = PyList_GetItem; |
| 5447 | } |
| 5448 | else if (PyTuple_Check(argv)) { |
| 5449 | argc = PyTuple_Size(argv); |
| 5450 | getitem = PyTuple_GetItem; |
| 5451 | } |
| 5452 | else { |
| 5453 | PyErr_SetString(PyExc_TypeError, |
| 5454 | "spawnve() arg 2 must be a tuple or list"); |
| 5455 | goto fail_0; |
| 5456 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5457 | if (argc == 0) { |
| 5458 | PyErr_SetString(PyExc_ValueError, |
| 5459 | "spawnve() arg 2 cannot be empty"); |
| 5460 | goto fail_0; |
| 5461 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5462 | if (!PyMapping_Check(env)) { |
| 5463 | PyErr_SetString(PyExc_TypeError, |
| 5464 | "spawnve() arg 3 must be a mapping object"); |
| 5465 | goto fail_0; |
| 5466 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5467 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5468 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5469 | if (argvlist == NULL) { |
| 5470 | PyErr_NoMemory(); |
| 5471 | goto fail_0; |
| 5472 | } |
| 5473 | for (i = 0; i < argc; i++) { |
| 5474 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5475 | &argvlist[i])) |
| 5476 | { |
| 5477 | lastarg = i; |
| 5478 | goto fail_1; |
| 5479 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5480 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5481 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5482 | PyErr_SetString( |
| 5483 | PyExc_ValueError, |
| 5484 | "spawnv() arg 2 first element cannot be empty"); |
| 5485 | goto fail_1; |
| 5486 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5487 | } |
| 5488 | lastarg = argc; |
| 5489 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5490 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5491 | envlist = parse_envlist(env, &envc); |
| 5492 | if (envlist == NULL) |
| 5493 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5494 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5495 | if (mode == _OLD_P_OVERLAY) |
| 5496 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5497 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5498 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5499 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5500 | #ifdef HAVE_WSPAWNV |
| 5501 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
| 5502 | #else |
| 5503 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 5504 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5505 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5506 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5507 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5508 | if (spawnval == -1) |
| 5509 | (void) posix_error(); |
| 5510 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5511 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5512 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5513 | while (--envc >= 0) |
| 5514 | PyMem_DEL(envlist[envc]); |
| 5515 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5516 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5517 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5518 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5519 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5520 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5521 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5522 | #endif /* HAVE_SPAWNV */ |
| 5523 | |
| 5524 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5525 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5526 | |
| 5527 | /* Helper function to validate arguments. |
| 5528 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 5529 | If obj is non-NULL it must be callable. */ |
| 5530 | static int |
| 5531 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 5532 | { |
| 5533 | if (obj && !PyCallable_Check(obj)) { |
| 5534 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
| 5535 | obj_name, Py_TYPE(obj)->tp_name); |
| 5536 | return -1; |
| 5537 | } |
| 5538 | return 0; |
| 5539 | } |
| 5540 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5541 | /*[clinic input] |
| 5542 | os.register_at_fork |
| 5543 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5544 | * |
| 5545 | before: object=NULL |
| 5546 | A callable to be called in the parent before the fork() syscall. |
| 5547 | after_in_child: object=NULL |
| 5548 | A callable to be called in the child after fork(). |
| 5549 | after_in_parent: object=NULL |
| 5550 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5551 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5552 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5553 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5554 | 'before' callbacks are called in reverse order. |
| 5555 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5556 | |
| 5557 | [clinic start generated code]*/ |
| 5558 | |
| 5559 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5560 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 5561 | PyObject *after_in_child, PyObject *after_in_parent) |
| 5562 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5563 | { |
| 5564 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5565 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5566 | if (!before && !after_in_child && !after_in_parent) { |
| 5567 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 5568 | return NULL; |
| 5569 | } |
| 5570 | if (check_null_or_callable(before, "before") || |
| 5571 | check_null_or_callable(after_in_child, "after_in_child") || |
| 5572 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5573 | return NULL; |
| 5574 | } |
| 5575 | interp = PyThreadState_Get()->interp; |
| 5576 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5577 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5578 | return NULL; |
| 5579 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5580 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5581 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 5582 | } |
| 5583 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 5584 | return NULL; |
| 5585 | } |
| 5586 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5587 | } |
| 5588 | #endif /* HAVE_FORK */ |
| 5589 | |
| 5590 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5591 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5592 | /*[clinic input] |
| 5593 | os.fork1 |
| 5594 | |
| 5595 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 5596 | |
| 5597 | Return 0 to child process and PID of child to parent process. |
| 5598 | [clinic start generated code]*/ |
| 5599 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5600 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5601 | os_fork1_impl(PyObject *module) |
| 5602 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5603 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5604 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5605 | |
| 5606 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5607 | pid = fork1(); |
| 5608 | if (pid == 0) { |
| 5609 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5610 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5611 | } else { |
| 5612 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5613 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5614 | } |
| 5615 | if (pid == -1) |
| 5616 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5617 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5618 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5619 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5620 | |
| 5621 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5622 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5623 | /*[clinic input] |
| 5624 | os.fork |
| 5625 | |
| 5626 | Fork a child process. |
| 5627 | |
| 5628 | Return 0 to child process and PID of child to parent process. |
| 5629 | [clinic start generated code]*/ |
| 5630 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5631 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5632 | os_fork_impl(PyObject *module) |
| 5633 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5634 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5635 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5636 | |
| 5637 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5638 | pid = fork(); |
| 5639 | if (pid == 0) { |
| 5640 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5641 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5642 | } else { |
| 5643 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 5644 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5645 | } |
| 5646 | if (pid == -1) |
| 5647 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5648 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5649 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5650 | #endif /* HAVE_FORK */ |
| 5651 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5652 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5653 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5654 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5655 | /*[clinic input] |
| 5656 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5657 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5658 | policy: int |
| 5659 | |
| 5660 | Get the maximum scheduling priority for policy. |
| 5661 | [clinic start generated code]*/ |
| 5662 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5663 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5664 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 5665 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5666 | { |
| 5667 | int max; |
| 5668 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5669 | max = sched_get_priority_max(policy); |
| 5670 | if (max < 0) |
| 5671 | return posix_error(); |
| 5672 | return PyLong_FromLong(max); |
| 5673 | } |
| 5674 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5675 | |
| 5676 | /*[clinic input] |
| 5677 | os.sched_get_priority_min |
| 5678 | |
| 5679 | policy: int |
| 5680 | |
| 5681 | Get the minimum scheduling priority for policy. |
| 5682 | [clinic start generated code]*/ |
| 5683 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5684 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5685 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 5686 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5687 | { |
| 5688 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5689 | if (min < 0) |
| 5690 | return posix_error(); |
| 5691 | return PyLong_FromLong(min); |
| 5692 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5693 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5694 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5695 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5696 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5697 | /*[clinic input] |
| 5698 | os.sched_getscheduler |
| 5699 | pid: pid_t |
| 5700 | / |
| 5701 | |
| 5702 | Get the scheduling policy for the process identifiedy by pid. |
| 5703 | |
| 5704 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 5705 | [clinic start generated code]*/ |
| 5706 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5707 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5708 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
| 5709 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5710 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5711 | int policy; |
| 5712 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5713 | policy = sched_getscheduler(pid); |
| 5714 | if (policy < 0) |
| 5715 | return posix_error(); |
| 5716 | return PyLong_FromLong(policy); |
| 5717 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5718 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5719 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5720 | |
| 5721 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5722 | /*[clinic input] |
| 5723 | class os.sched_param "PyObject *" "&SchedParamType" |
| 5724 | |
| 5725 | @classmethod |
| 5726 | os.sched_param.__new__ |
| 5727 | |
| 5728 | sched_priority: object |
| 5729 | A scheduling parameter. |
| 5730 | |
| 5731 | Current has only one field: sched_priority"); |
| 5732 | [clinic start generated code]*/ |
| 5733 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5734 | static PyObject * |
| 5735 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5736 | /*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5737 | { |
| 5738 | PyObject *res; |
| 5739 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5740 | res = PyStructSequence_New(type); |
| 5741 | if (!res) |
| 5742 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5743 | Py_INCREF(sched_priority); |
| 5744 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5745 | return res; |
| 5746 | } |
| 5747 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5748 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5749 | PyDoc_VAR(os_sched_param__doc__); |
| 5750 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5751 | static PyStructSequence_Field sched_param_fields[] = { |
| 5752 | {"sched_priority", "the scheduling priority"}, |
| 5753 | {0} |
| 5754 | }; |
| 5755 | |
| 5756 | static PyStructSequence_Desc sched_param_desc = { |
| 5757 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5758 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5759 | sched_param_fields, |
| 5760 | 1 |
| 5761 | }; |
| 5762 | |
| 5763 | static int |
| 5764 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5765 | { |
| 5766 | long priority; |
| 5767 | |
| 5768 | if (Py_TYPE(param) != &SchedParamType) { |
| 5769 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5770 | return 0; |
| 5771 | } |
| 5772 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5773 | if (priority == -1 && PyErr_Occurred()) |
| 5774 | return 0; |
| 5775 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5776 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5777 | return 0; |
| 5778 | } |
| 5779 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5780 | return 1; |
| 5781 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5782 | #endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5783 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5784 | |
| 5785 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5786 | /*[clinic input] |
| 5787 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5788 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5789 | pid: pid_t |
| 5790 | policy: int |
| 5791 | param: sched_param |
| 5792 | / |
| 5793 | |
| 5794 | Set the scheduling policy for the process identified by pid. |
| 5795 | |
| 5796 | If pid is 0, the calling process is changed. |
| 5797 | param is an instance of sched_param. |
| 5798 | [clinic start generated code]*/ |
| 5799 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5800 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5801 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5802 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5803 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5804 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5805 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5806 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5807 | ** scheduling policy under Solaris/Illumos, and others. |
| 5808 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5809 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5810 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5811 | return posix_error(); |
| 5812 | Py_RETURN_NONE; |
| 5813 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5814 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5815 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5816 | |
| 5817 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5818 | /*[clinic input] |
| 5819 | os.sched_getparam |
| 5820 | pid: pid_t |
| 5821 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5822 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5823 | Returns scheduling parameters for the process identified by pid. |
| 5824 | |
| 5825 | If pid is 0, returns parameters for the calling process. |
| 5826 | Return value is an instance of sched_param. |
| 5827 | [clinic start generated code]*/ |
| 5828 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5829 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5830 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 5831 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5832 | { |
| 5833 | struct sched_param param; |
| 5834 | PyObject *result; |
| 5835 | PyObject *priority; |
| 5836 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5837 | if (sched_getparam(pid, ¶m)) |
| 5838 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5839 | result = PyStructSequence_New(&SchedParamType); |
| 5840 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5841 | return NULL; |
| 5842 | priority = PyLong_FromLong(param.sched_priority); |
| 5843 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5844 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5845 | return NULL; |
| 5846 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5847 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 5848 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5849 | } |
| 5850 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5851 | |
| 5852 | /*[clinic input] |
| 5853 | os.sched_setparam |
| 5854 | pid: pid_t |
| 5855 | param: sched_param |
| 5856 | / |
| 5857 | |
| 5858 | Set scheduling parameters for the process identified by pid. |
| 5859 | |
| 5860 | If pid is 0, sets parameters for the calling process. |
| 5861 | param should be an instance of sched_param. |
| 5862 | [clinic start generated code]*/ |
| 5863 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5864 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5865 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5866 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5867 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5868 | { |
| 5869 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5870 | return posix_error(); |
| 5871 | Py_RETURN_NONE; |
| 5872 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5873 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5874 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5875 | |
| 5876 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5877 | /*[clinic input] |
| 5878 | os.sched_rr_get_interval -> double |
| 5879 | pid: pid_t |
| 5880 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5881 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5882 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 5883 | |
| 5884 | Value returned is a float. |
| 5885 | [clinic start generated code]*/ |
| 5886 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5887 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5888 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 5889 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5890 | { |
| 5891 | struct timespec interval; |
| 5892 | if (sched_rr_get_interval(pid, &interval)) { |
| 5893 | posix_error(); |
| 5894 | return -1.0; |
| 5895 | } |
| 5896 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 5897 | } |
| 5898 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5899 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5900 | |
| 5901 | /*[clinic input] |
| 5902 | os.sched_yield |
| 5903 | |
| 5904 | Voluntarily relinquish the CPU. |
| 5905 | [clinic start generated code]*/ |
| 5906 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5907 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5908 | os_sched_yield_impl(PyObject *module) |
| 5909 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5910 | { |
| 5911 | if (sched_yield()) |
| 5912 | return posix_error(); |
| 5913 | Py_RETURN_NONE; |
| 5914 | } |
| 5915 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5916 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5917 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5918 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5919 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5920 | /*[clinic input] |
| 5921 | os.sched_setaffinity |
| 5922 | pid: pid_t |
| 5923 | mask : object |
| 5924 | / |
| 5925 | |
| 5926 | Set the CPU affinity of the process identified by pid to mask. |
| 5927 | |
| 5928 | mask should be an iterable of integers identifying CPUs. |
| 5929 | [clinic start generated code]*/ |
| 5930 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5931 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5932 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 5933 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5934 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5935 | int ncpus; |
| 5936 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5937 | cpu_set_t *cpu_set = NULL; |
| 5938 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5939 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5940 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5941 | if (iterator == NULL) |
| 5942 | return NULL; |
| 5943 | |
| 5944 | ncpus = NCPUS_START; |
| 5945 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5946 | cpu_set = CPU_ALLOC(ncpus); |
| 5947 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5948 | PyErr_NoMemory(); |
| 5949 | goto error; |
| 5950 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5951 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5952 | |
| 5953 | while ((item = PyIter_Next(iterator))) { |
| 5954 | long cpu; |
| 5955 | if (!PyLong_Check(item)) { |
| 5956 | PyErr_Format(PyExc_TypeError, |
| 5957 | "expected an iterator of ints, " |
| 5958 | "but iterator yielded %R", |
| 5959 | Py_TYPE(item)); |
| 5960 | Py_DECREF(item); |
| 5961 | goto error; |
| 5962 | } |
| 5963 | cpu = PyLong_AsLong(item); |
| 5964 | Py_DECREF(item); |
| 5965 | if (cpu < 0) { |
| 5966 | if (!PyErr_Occurred()) |
| 5967 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5968 | goto error; |
| 5969 | } |
| 5970 | if (cpu > INT_MAX - 1) { |
| 5971 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5972 | goto error; |
| 5973 | } |
| 5974 | if (cpu >= ncpus) { |
| 5975 | /* Grow CPU mask to fit the CPU number */ |
| 5976 | int newncpus = ncpus; |
| 5977 | cpu_set_t *newmask; |
| 5978 | size_t newsetsize; |
| 5979 | while (newncpus <= cpu) { |
| 5980 | if (newncpus > INT_MAX / 2) |
| 5981 | newncpus = cpu + 1; |
| 5982 | else |
| 5983 | newncpus = newncpus * 2; |
| 5984 | } |
| 5985 | newmask = CPU_ALLOC(newncpus); |
| 5986 | if (newmask == NULL) { |
| 5987 | PyErr_NoMemory(); |
| 5988 | goto error; |
| 5989 | } |
| 5990 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5991 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5992 | memcpy(newmask, cpu_set, setsize); |
| 5993 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5994 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5995 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5996 | ncpus = newncpus; |
| 5997 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5998 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5999 | } |
| 6000 | Py_CLEAR(iterator); |
| 6001 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6002 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6003 | posix_error(); |
| 6004 | goto error; |
| 6005 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6006 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6007 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6008 | |
| 6009 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6010 | if (cpu_set) |
| 6011 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6012 | Py_XDECREF(iterator); |
| 6013 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6014 | } |
| 6015 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6016 | |
| 6017 | /*[clinic input] |
| 6018 | os.sched_getaffinity |
| 6019 | pid: pid_t |
| 6020 | / |
| 6021 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6022 | 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] | 6023 | |
| 6024 | The affinity is returned as a set of CPU identifiers. |
| 6025 | [clinic start generated code]*/ |
| 6026 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6027 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6028 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6029 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6030 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6031 | int cpu, ncpus, count; |
| 6032 | size_t setsize; |
| 6033 | cpu_set_t *mask = NULL; |
| 6034 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6035 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6036 | ncpus = NCPUS_START; |
| 6037 | while (1) { |
| 6038 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6039 | mask = CPU_ALLOC(ncpus); |
| 6040 | if (mask == NULL) |
| 6041 | return PyErr_NoMemory(); |
| 6042 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6043 | break; |
| 6044 | CPU_FREE(mask); |
| 6045 | if (errno != EINVAL) |
| 6046 | return posix_error(); |
| 6047 | if (ncpus > INT_MAX / 2) { |
| 6048 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6049 | "a large enough CPU set"); |
| 6050 | return NULL; |
| 6051 | } |
| 6052 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6053 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6054 | |
| 6055 | res = PySet_New(NULL); |
| 6056 | if (res == NULL) |
| 6057 | goto error; |
| 6058 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6059 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6060 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6061 | --count; |
| 6062 | if (cpu_num == NULL) |
| 6063 | goto error; |
| 6064 | if (PySet_Add(res, cpu_num)) { |
| 6065 | Py_DECREF(cpu_num); |
| 6066 | goto error; |
| 6067 | } |
| 6068 | Py_DECREF(cpu_num); |
| 6069 | } |
| 6070 | } |
| 6071 | CPU_FREE(mask); |
| 6072 | return res; |
| 6073 | |
| 6074 | error: |
| 6075 | if (mask) |
| 6076 | CPU_FREE(mask); |
| 6077 | Py_XDECREF(res); |
| 6078 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6079 | } |
| 6080 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6081 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6082 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6083 | #endif /* HAVE_SCHED_H */ |
| 6084 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6085 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6086 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6087 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6088 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6089 | #define DEV_PTY_FILE "/dev/ptc" |
| 6090 | #define HAVE_DEV_PTMX |
| 6091 | #else |
| 6092 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6093 | #endif |
| 6094 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6095 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6096 | #ifdef HAVE_PTY_H |
| 6097 | #include <pty.h> |
| 6098 | #else |
| 6099 | #ifdef HAVE_LIBUTIL_H |
| 6100 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6101 | #else |
| 6102 | #ifdef HAVE_UTIL_H |
| 6103 | #include <util.h> |
| 6104 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6105 | #endif /* HAVE_LIBUTIL_H */ |
| 6106 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6107 | #ifdef HAVE_STROPTS_H |
| 6108 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6109 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6110 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6111 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6112 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6113 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6114 | /*[clinic input] |
| 6115 | os.openpty |
| 6116 | |
| 6117 | Open a pseudo-terminal. |
| 6118 | |
| 6119 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6120 | for both the master and slave ends. |
| 6121 | [clinic start generated code]*/ |
| 6122 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6123 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6124 | os_openpty_impl(PyObject *module) |
| 6125 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6126 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6127 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6128 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6129 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6130 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6131 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6132 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6133 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6134 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6135 | #endif |
| 6136 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6137 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6138 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6139 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6140 | goto posix_error; |
| 6141 | |
| 6142 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6143 | goto error; |
| 6144 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6145 | goto error; |
| 6146 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6147 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6148 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6149 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6150 | goto posix_error; |
| 6151 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6152 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6153 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6154 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6155 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6156 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6157 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6158 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6159 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6160 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6161 | goto posix_error; |
| 6162 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6163 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6164 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6165 | /* change permission of slave */ |
| 6166 | if (grantpt(master_fd) < 0) { |
| 6167 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6168 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6169 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6170 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6171 | /* unlock slave */ |
| 6172 | if (unlockpt(master_fd) < 0) { |
| 6173 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6174 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6175 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6176 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6177 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6178 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6179 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6180 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6181 | goto posix_error; |
| 6182 | |
| 6183 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6184 | if (slave_fd == -1) |
| 6185 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6186 | |
| 6187 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6188 | goto posix_error; |
| 6189 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6190 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6191 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6192 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6193 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6194 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6195 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6196 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6197 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6198 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6199 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6200 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6201 | posix_error: |
| 6202 | posix_error(); |
| 6203 | error: |
| 6204 | if (master_fd != -1) |
| 6205 | close(master_fd); |
| 6206 | if (slave_fd != -1) |
| 6207 | close(slave_fd); |
| 6208 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6209 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6210 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6211 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6212 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6213 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6214 | /*[clinic input] |
| 6215 | os.forkpty |
| 6216 | |
| 6217 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6218 | |
| 6219 | Returns a tuple of (pid, master_fd). |
| 6220 | Like fork(), return pid of 0 to the child process, |
| 6221 | and pid of child to the parent process. |
| 6222 | To both, return fd of newly opened pseudo-terminal. |
| 6223 | [clinic start generated code]*/ |
| 6224 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6225 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6226 | os_forkpty_impl(PyObject *module) |
| 6227 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6228 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6229 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6230 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6231 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6232 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6233 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6234 | if (pid == 0) { |
| 6235 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6236 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6237 | } else { |
| 6238 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6239 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6240 | } |
| 6241 | if (pid == -1) |
| 6242 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6243 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6244 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6245 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6246 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6247 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6248 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6249 | /*[clinic input] |
| 6250 | os.getegid |
| 6251 | |
| 6252 | Return the current process's effective group id. |
| 6253 | [clinic start generated code]*/ |
| 6254 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6255 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6256 | os_getegid_impl(PyObject *module) |
| 6257 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6258 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6259 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6260 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6261 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6262 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6263 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6264 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6265 | /*[clinic input] |
| 6266 | os.geteuid |
| 6267 | |
| 6268 | Return the current process's effective user id. |
| 6269 | [clinic start generated code]*/ |
| 6270 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6271 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6272 | os_geteuid_impl(PyObject *module) |
| 6273 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6274 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6275 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6276 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6277 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6278 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6279 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6280 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6281 | /*[clinic input] |
| 6282 | os.getgid |
| 6283 | |
| 6284 | Return the current process's group id. |
| 6285 | [clinic start generated code]*/ |
| 6286 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6287 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6288 | os_getgid_impl(PyObject *module) |
| 6289 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6290 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6291 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6292 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6293 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6294 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6295 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6296 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6297 | /*[clinic input] |
| 6298 | os.getpid |
| 6299 | |
| 6300 | Return the current process id. |
| 6301 | [clinic start generated code]*/ |
| 6302 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6303 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6304 | os_getpid_impl(PyObject *module) |
| 6305 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6306 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6307 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6308 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6309 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6310 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6311 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6312 | |
| 6313 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6314 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6315 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6316 | Returns a list of groups to which a user belongs.\n\n\ |
| 6317 | user: username to lookup\n\ |
| 6318 | group: base group id of the user"); |
| 6319 | |
| 6320 | static PyObject * |
| 6321 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6322 | { |
| 6323 | #ifdef NGROUPS_MAX |
| 6324 | #define MAX_GROUPS NGROUPS_MAX |
| 6325 | #else |
| 6326 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6327 | #define MAX_GROUPS 64 |
| 6328 | #endif |
| 6329 | |
| 6330 | const char *user; |
| 6331 | int i, ngroups; |
| 6332 | PyObject *list; |
| 6333 | #ifdef __APPLE__ |
| 6334 | int *groups, basegid; |
| 6335 | #else |
| 6336 | gid_t *groups, basegid; |
| 6337 | #endif |
| 6338 | ngroups = MAX_GROUPS; |
| 6339 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6340 | #ifdef __APPLE__ |
| 6341 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6342 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6343 | #else |
| 6344 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6345 | _Py_Gid_Converter, &basegid)) |
| 6346 | return NULL; |
| 6347 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6348 | |
| 6349 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6350 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6351 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6352 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6353 | #endif |
| 6354 | if (groups == NULL) |
| 6355 | return PyErr_NoMemory(); |
| 6356 | |
| 6357 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6358 | PyMem_Del(groups); |
| 6359 | return posix_error(); |
| 6360 | } |
| 6361 | |
| 6362 | list = PyList_New(ngroups); |
| 6363 | if (list == NULL) { |
| 6364 | PyMem_Del(groups); |
| 6365 | return NULL; |
| 6366 | } |
| 6367 | |
| 6368 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6369 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6370 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6371 | #else |
| 6372 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6373 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6374 | if (o == NULL) { |
| 6375 | Py_DECREF(list); |
| 6376 | PyMem_Del(groups); |
| 6377 | return NULL; |
| 6378 | } |
| 6379 | PyList_SET_ITEM(list, i, o); |
| 6380 | } |
| 6381 | |
| 6382 | PyMem_Del(groups); |
| 6383 | |
| 6384 | return list; |
| 6385 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6386 | #endif /* HAVE_GETGROUPLIST */ |
| 6387 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6388 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6389 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6390 | /*[clinic input] |
| 6391 | os.getgroups |
| 6392 | |
| 6393 | Return list of supplemental group IDs for the process. |
| 6394 | [clinic start generated code]*/ |
| 6395 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6396 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6397 | os_getgroups_impl(PyObject *module) |
| 6398 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6399 | { |
| 6400 | PyObject *result = NULL; |
| 6401 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6402 | #ifdef NGROUPS_MAX |
| 6403 | #define MAX_GROUPS NGROUPS_MAX |
| 6404 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6405 | /* 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] | 6406 | #define MAX_GROUPS 64 |
| 6407 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6408 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6409 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6410 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6411 | * This is a helper variable to store the intermediate result when |
| 6412 | * that happens. |
| 6413 | * |
| 6414 | * To keep the code readable the OSX behaviour is unconditional, |
| 6415 | * according to the POSIX spec this should be safe on all unix-y |
| 6416 | * systems. |
| 6417 | */ |
| 6418 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6419 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6420 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6421 | #ifdef __APPLE__ |
| 6422 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6423 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6424 | * always first call getgroups with length 0 to get the actual number |
| 6425 | * of groups. |
| 6426 | */ |
| 6427 | n = getgroups(0, NULL); |
| 6428 | if (n < 0) { |
| 6429 | return posix_error(); |
| 6430 | } else if (n <= MAX_GROUPS) { |
| 6431 | /* groups will fit in existing array */ |
| 6432 | alt_grouplist = grouplist; |
| 6433 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6434 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6435 | if (alt_grouplist == NULL) { |
| 6436 | errno = EINVAL; |
| 6437 | return posix_error(); |
| 6438 | } |
| 6439 | } |
| 6440 | |
| 6441 | n = getgroups(n, alt_grouplist); |
| 6442 | if (n == -1) { |
| 6443 | if (alt_grouplist != grouplist) { |
| 6444 | PyMem_Free(alt_grouplist); |
| 6445 | } |
| 6446 | return posix_error(); |
| 6447 | } |
| 6448 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6449 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6450 | if (n < 0) { |
| 6451 | if (errno == EINVAL) { |
| 6452 | n = getgroups(0, NULL); |
| 6453 | if (n == -1) { |
| 6454 | return posix_error(); |
| 6455 | } |
| 6456 | if (n == 0) { |
| 6457 | /* Avoid malloc(0) */ |
| 6458 | alt_grouplist = grouplist; |
| 6459 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6460 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6461 | if (alt_grouplist == NULL) { |
| 6462 | errno = EINVAL; |
| 6463 | return posix_error(); |
| 6464 | } |
| 6465 | n = getgroups(n, alt_grouplist); |
| 6466 | if (n == -1) { |
| 6467 | PyMem_Free(alt_grouplist); |
| 6468 | return posix_error(); |
| 6469 | } |
| 6470 | } |
| 6471 | } else { |
| 6472 | return posix_error(); |
| 6473 | } |
| 6474 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6475 | #endif |
| 6476 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6477 | result = PyList_New(n); |
| 6478 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6479 | int i; |
| 6480 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6481 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6482 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6483 | Py_DECREF(result); |
| 6484 | result = NULL; |
| 6485 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6486 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6487 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6488 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6489 | } |
| 6490 | |
| 6491 | if (alt_grouplist != grouplist) { |
| 6492 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6493 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6494 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6495 | return result; |
| 6496 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6497 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6498 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6499 | #ifdef HAVE_INITGROUPS |
| 6500 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6501 | "initgroups(username, gid) -> None\n\n\ |
| 6502 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6503 | the groups of which the specified username is a member, plus the specified\n\ |
| 6504 | group id."); |
| 6505 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6506 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6507 | static PyObject * |
| 6508 | posix_initgroups(PyObject *self, PyObject *args) |
| 6509 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6510 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6511 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6512 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6513 | #ifdef __APPLE__ |
| 6514 | int gid; |
| 6515 | #else |
| 6516 | gid_t gid; |
| 6517 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6518 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6519 | #ifdef __APPLE__ |
| 6520 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6521 | PyUnicode_FSConverter, &oname, |
| 6522 | &gid)) |
| 6523 | #else |
| 6524 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6525 | PyUnicode_FSConverter, &oname, |
| 6526 | _Py_Gid_Converter, &gid)) |
| 6527 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6528 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6529 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6530 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6531 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6532 | Py_DECREF(oname); |
| 6533 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6534 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6535 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 6536 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6537 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6538 | #endif /* HAVE_INITGROUPS */ |
| 6539 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6540 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6541 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6542 | /*[clinic input] |
| 6543 | os.getpgid |
| 6544 | |
| 6545 | pid: pid_t |
| 6546 | |
| 6547 | Call the system call getpgid(), and return the result. |
| 6548 | [clinic start generated code]*/ |
| 6549 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6550 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6551 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 6552 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6553 | { |
| 6554 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6555 | if (pgid < 0) |
| 6556 | return posix_error(); |
| 6557 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6558 | } |
| 6559 | #endif /* HAVE_GETPGID */ |
| 6560 | |
| 6561 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6562 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6563 | /*[clinic input] |
| 6564 | os.getpgrp |
| 6565 | |
| 6566 | Return the current process group id. |
| 6567 | [clinic start generated code]*/ |
| 6568 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6569 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6570 | os_getpgrp_impl(PyObject *module) |
| 6571 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6572 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6573 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6574 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6575 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6576 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6577 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6578 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6579 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6580 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6581 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6582 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6583 | /*[clinic input] |
| 6584 | os.setpgrp |
| 6585 | |
| 6586 | Make the current process the leader of its process group. |
| 6587 | [clinic start generated code]*/ |
| 6588 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6589 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6590 | os_setpgrp_impl(PyObject *module) |
| 6591 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6592 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6593 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6594 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6595 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6596 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6597 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6598 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 6599 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6600 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6601 | #endif /* HAVE_SETPGRP */ |
| 6602 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6603 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6604 | |
| 6605 | #ifdef MS_WINDOWS |
| 6606 | #include <tlhelp32.h> |
| 6607 | |
| 6608 | static PyObject* |
| 6609 | win32_getppid() |
| 6610 | { |
| 6611 | HANDLE snapshot; |
| 6612 | pid_t mypid; |
| 6613 | PyObject* result = NULL; |
| 6614 | BOOL have_record; |
| 6615 | PROCESSENTRY32 pe; |
| 6616 | |
| 6617 | mypid = getpid(); /* This function never fails */ |
| 6618 | |
| 6619 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6620 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6621 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6622 | |
| 6623 | pe.dwSize = sizeof(pe); |
| 6624 | have_record = Process32First(snapshot, &pe); |
| 6625 | while (have_record) { |
| 6626 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6627 | /* We could cache the ulong value in a static variable. */ |
| 6628 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6629 | break; |
| 6630 | } |
| 6631 | |
| 6632 | have_record = Process32Next(snapshot, &pe); |
| 6633 | } |
| 6634 | |
| 6635 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6636 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6637 | * error anyway, so let's raise it. */ |
| 6638 | if (!result) |
| 6639 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6640 | |
| 6641 | CloseHandle(snapshot); |
| 6642 | |
| 6643 | return result; |
| 6644 | } |
| 6645 | #endif /*MS_WINDOWS*/ |
| 6646 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6647 | |
| 6648 | /*[clinic input] |
| 6649 | os.getppid |
| 6650 | |
| 6651 | Return the parent's process id. |
| 6652 | |
| 6653 | If the parent process has already exited, Windows machines will still |
| 6654 | return its id; others systems will return the id of the 'init' process (1). |
| 6655 | [clinic start generated code]*/ |
| 6656 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6657 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6658 | os_getppid_impl(PyObject *module) |
| 6659 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6660 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6661 | #ifdef MS_WINDOWS |
| 6662 | return win32_getppid(); |
| 6663 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6664 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6665 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6666 | } |
| 6667 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6668 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6669 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6670 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6671 | /*[clinic input] |
| 6672 | os.getlogin |
| 6673 | |
| 6674 | Return the actual login name. |
| 6675 | [clinic start generated code]*/ |
| 6676 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6677 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6678 | os_getlogin_impl(PyObject *module) |
| 6679 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6680 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6681 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6682 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6683 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6684 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6685 | |
| 6686 | if (GetUserNameW(user_name, &num_chars)) { |
| 6687 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6688 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6689 | } |
| 6690 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6691 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6692 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6693 | char *name; |
| 6694 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6695 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6696 | errno = 0; |
| 6697 | name = getlogin(); |
| 6698 | if (name == NULL) { |
| 6699 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6700 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6701 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6702 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6703 | } |
| 6704 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6705 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6706 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6707 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6708 | return result; |
| 6709 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6710 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6711 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6712 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6713 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6714 | /*[clinic input] |
| 6715 | os.getuid |
| 6716 | |
| 6717 | Return the current process's user id. |
| 6718 | [clinic start generated code]*/ |
| 6719 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6720 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6721 | os_getuid_impl(PyObject *module) |
| 6722 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6723 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6724 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6725 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6726 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6727 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6728 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6729 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6730 | #define HAVE_KILL |
| 6731 | #endif /* MS_WINDOWS */ |
| 6732 | |
| 6733 | #ifdef HAVE_KILL |
| 6734 | /*[clinic input] |
| 6735 | os.kill |
| 6736 | |
| 6737 | pid: pid_t |
| 6738 | signal: Py_ssize_t |
| 6739 | / |
| 6740 | |
| 6741 | Kill a process with a signal. |
| 6742 | [clinic start generated code]*/ |
| 6743 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6744 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6745 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 6746 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6747 | #ifndef MS_WINDOWS |
| 6748 | { |
| 6749 | if (kill(pid, (int)signal) == -1) |
| 6750 | return posix_error(); |
| 6751 | Py_RETURN_NONE; |
| 6752 | } |
| 6753 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6754 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6755 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6756 | DWORD sig = (DWORD)signal; |
| 6757 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6758 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6759 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6760 | /* Console processes which share a common console can be sent CTRL+C or |
| 6761 | CTRL+BREAK events, provided they handle said events. */ |
| 6762 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6763 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6764 | err = GetLastError(); |
| 6765 | PyErr_SetFromWindowsErr(err); |
| 6766 | } |
| 6767 | else |
| 6768 | Py_RETURN_NONE; |
| 6769 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6770 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6771 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6772 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6773 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6774 | if (handle == NULL) { |
| 6775 | err = GetLastError(); |
| 6776 | return PyErr_SetFromWindowsErr(err); |
| 6777 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6778 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6779 | if (TerminateProcess(handle, sig) == 0) { |
| 6780 | err = GetLastError(); |
| 6781 | result = PyErr_SetFromWindowsErr(err); |
| 6782 | } else { |
| 6783 | Py_INCREF(Py_None); |
| 6784 | result = Py_None; |
| 6785 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6786 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6787 | CloseHandle(handle); |
| 6788 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6789 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6790 | #endif /* !MS_WINDOWS */ |
| 6791 | #endif /* HAVE_KILL */ |
| 6792 | |
| 6793 | |
| 6794 | #ifdef HAVE_KILLPG |
| 6795 | /*[clinic input] |
| 6796 | os.killpg |
| 6797 | |
| 6798 | pgid: pid_t |
| 6799 | signal: int |
| 6800 | / |
| 6801 | |
| 6802 | Kill a process group with a signal. |
| 6803 | [clinic start generated code]*/ |
| 6804 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6805 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6806 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 6807 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6808 | { |
| 6809 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6810 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6811 | take the same type. Moreover, pid_t is always at least as wide as |
| 6812 | int (else compilation of this module fails), which is safe. */ |
| 6813 | if (killpg(pgid, signal) == -1) |
| 6814 | return posix_error(); |
| 6815 | Py_RETURN_NONE; |
| 6816 | } |
| 6817 | #endif /* HAVE_KILLPG */ |
| 6818 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6819 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6820 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6821 | #ifdef HAVE_SYS_LOCK_H |
| 6822 | #include <sys/lock.h> |
| 6823 | #endif |
| 6824 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6825 | /*[clinic input] |
| 6826 | os.plock |
| 6827 | op: int |
| 6828 | / |
| 6829 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6830 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6831 | [clinic start generated code]*/ |
| 6832 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6833 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6834 | os_plock_impl(PyObject *module, int op) |
| 6835 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6836 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6837 | if (plock(op) == -1) |
| 6838 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6839 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6840 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6841 | #endif /* HAVE_PLOCK */ |
| 6842 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6843 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6844 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6845 | /*[clinic input] |
| 6846 | os.setuid |
| 6847 | |
| 6848 | uid: uid_t |
| 6849 | / |
| 6850 | |
| 6851 | Set the current process's user id. |
| 6852 | [clinic start generated code]*/ |
| 6853 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6854 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6855 | os_setuid_impl(PyObject *module, uid_t uid) |
| 6856 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6857 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6858 | if (setuid(uid) < 0) |
| 6859 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6860 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6861 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6862 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6863 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6864 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6865 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6866 | /*[clinic input] |
| 6867 | os.seteuid |
| 6868 | |
| 6869 | euid: uid_t |
| 6870 | / |
| 6871 | |
| 6872 | Set the current process's effective user id. |
| 6873 | [clinic start generated code]*/ |
| 6874 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6875 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6876 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 6877 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6878 | { |
| 6879 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6880 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6881 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6882 | } |
| 6883 | #endif /* HAVE_SETEUID */ |
| 6884 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6885 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6886 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6887 | /*[clinic input] |
| 6888 | os.setegid |
| 6889 | |
| 6890 | egid: gid_t |
| 6891 | / |
| 6892 | |
| 6893 | Set the current process's effective group id. |
| 6894 | [clinic start generated code]*/ |
| 6895 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6896 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6897 | os_setegid_impl(PyObject *module, gid_t egid) |
| 6898 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6899 | { |
| 6900 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6901 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6902 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6903 | } |
| 6904 | #endif /* HAVE_SETEGID */ |
| 6905 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6906 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6907 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6908 | /*[clinic input] |
| 6909 | os.setreuid |
| 6910 | |
| 6911 | ruid: uid_t |
| 6912 | euid: uid_t |
| 6913 | / |
| 6914 | |
| 6915 | Set the current process's real and effective user ids. |
| 6916 | [clinic start generated code]*/ |
| 6917 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6918 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6919 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 6920 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6921 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6922 | if (setreuid(ruid, euid) < 0) { |
| 6923 | return posix_error(); |
| 6924 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 6925 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6926 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6927 | } |
| 6928 | #endif /* HAVE_SETREUID */ |
| 6929 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6930 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6931 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6932 | /*[clinic input] |
| 6933 | os.setregid |
| 6934 | |
| 6935 | rgid: gid_t |
| 6936 | egid: gid_t |
| 6937 | / |
| 6938 | |
| 6939 | Set the current process's real and effective group ids. |
| 6940 | [clinic start generated code]*/ |
| 6941 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6942 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6943 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 6944 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6945 | { |
| 6946 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6947 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6948 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6949 | } |
| 6950 | #endif /* HAVE_SETREGID */ |
| 6951 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6952 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6953 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6954 | /*[clinic input] |
| 6955 | os.setgid |
| 6956 | gid: gid_t |
| 6957 | / |
| 6958 | |
| 6959 | Set the current process's group id. |
| 6960 | [clinic start generated code]*/ |
| 6961 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6962 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6963 | os_setgid_impl(PyObject *module, gid_t gid) |
| 6964 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6965 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6966 | if (setgid(gid) < 0) |
| 6967 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6968 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6969 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6970 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6971 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6972 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6973 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6974 | /*[clinic input] |
| 6975 | os.setgroups |
| 6976 | |
| 6977 | groups: object |
| 6978 | / |
| 6979 | |
| 6980 | Set the groups of the current process to list. |
| 6981 | [clinic start generated code]*/ |
| 6982 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6983 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6984 | os_setgroups(PyObject *module, PyObject *groups) |
| 6985 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6986 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 6987 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6988 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6989 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6990 | if (!PySequence_Check(groups)) { |
| 6991 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6992 | return NULL; |
| 6993 | } |
| 6994 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 6995 | if (len < 0) { |
| 6996 | return NULL; |
| 6997 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6998 | if (len > MAX_GROUPS) { |
| 6999 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7000 | return NULL; |
| 7001 | } |
| 7002 | for(i = 0; i < len; i++) { |
| 7003 | PyObject *elem; |
| 7004 | elem = PySequence_GetItem(groups, i); |
| 7005 | if (!elem) |
| 7006 | return NULL; |
| 7007 | if (!PyLong_Check(elem)) { |
| 7008 | PyErr_SetString(PyExc_TypeError, |
| 7009 | "groups must be integers"); |
| 7010 | Py_DECREF(elem); |
| 7011 | return NULL; |
| 7012 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7013 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7014 | Py_DECREF(elem); |
| 7015 | return NULL; |
| 7016 | } |
| 7017 | } |
| 7018 | Py_DECREF(elem); |
| 7019 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7020 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7021 | if (setgroups(len, grouplist) < 0) |
| 7022 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7023 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7024 | } |
| 7025 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7026 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7027 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7028 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7029 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7030 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7031 | PyObject *result; |
| 7032 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 7033 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7034 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7035 | if (pid == -1) |
| 7036 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7037 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7038 | if (struct_rusage == NULL) { |
| 7039 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7040 | if (m == NULL) |
| 7041 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 7042 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7043 | Py_DECREF(m); |
| 7044 | if (struct_rusage == NULL) |
| 7045 | return NULL; |
| 7046 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7047 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7048 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7049 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 7050 | if (!result) |
| 7051 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7052 | |
| 7053 | #ifndef doubletime |
| 7054 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7055 | #endif |
| 7056 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7057 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7058 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7059 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7060 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7061 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7062 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7063 | SET_INT(result, 2, ru->ru_maxrss); |
| 7064 | SET_INT(result, 3, ru->ru_ixrss); |
| 7065 | SET_INT(result, 4, ru->ru_idrss); |
| 7066 | SET_INT(result, 5, ru->ru_isrss); |
| 7067 | SET_INT(result, 6, ru->ru_minflt); |
| 7068 | SET_INT(result, 7, ru->ru_majflt); |
| 7069 | SET_INT(result, 8, ru->ru_nswap); |
| 7070 | SET_INT(result, 9, ru->ru_inblock); |
| 7071 | SET_INT(result, 10, ru->ru_oublock); |
| 7072 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7073 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7074 | SET_INT(result, 13, ru->ru_nsignals); |
| 7075 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7076 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7077 | #undef SET_INT |
| 7078 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7079 | if (PyErr_Occurred()) { |
| 7080 | Py_DECREF(result); |
| 7081 | return NULL; |
| 7082 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7083 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7084 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7085 | } |
| 7086 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7087 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7088 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7089 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7090 | /*[clinic input] |
| 7091 | os.wait3 |
| 7092 | |
| 7093 | options: int |
| 7094 | Wait for completion of a child process. |
| 7095 | |
| 7096 | Returns a tuple of information about the child process: |
| 7097 | (pid, status, rusage) |
| 7098 | [clinic start generated code]*/ |
| 7099 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7100 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7101 | os_wait3_impl(PyObject *module, int options) |
| 7102 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7103 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7104 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7105 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7106 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7107 | WAIT_TYPE status; |
| 7108 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7109 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7110 | do { |
| 7111 | Py_BEGIN_ALLOW_THREADS |
| 7112 | pid = wait3(&status, options, &ru); |
| 7113 | Py_END_ALLOW_THREADS |
| 7114 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7115 | if (pid < 0) |
| 7116 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7117 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7118 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7119 | } |
| 7120 | #endif /* HAVE_WAIT3 */ |
| 7121 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7122 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7123 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7124 | /*[clinic input] |
| 7125 | |
| 7126 | os.wait4 |
| 7127 | |
| 7128 | pid: pid_t |
| 7129 | options: int |
| 7130 | |
| 7131 | Wait for completion of a specific child process. |
| 7132 | |
| 7133 | Returns a tuple of information about the child process: |
| 7134 | (pid, status, rusage) |
| 7135 | [clinic start generated code]*/ |
| 7136 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7137 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7138 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7139 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7140 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7141 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7142 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7143 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7144 | WAIT_TYPE status; |
| 7145 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7146 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7147 | do { |
| 7148 | Py_BEGIN_ALLOW_THREADS |
| 7149 | res = wait4(pid, &status, options, &ru); |
| 7150 | Py_END_ALLOW_THREADS |
| 7151 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7152 | if (res < 0) |
| 7153 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7154 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7155 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7156 | } |
| 7157 | #endif /* HAVE_WAIT4 */ |
| 7158 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7159 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7160 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7161 | /*[clinic input] |
| 7162 | os.waitid |
| 7163 | |
| 7164 | idtype: idtype_t |
| 7165 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7166 | id: id_t |
| 7167 | The id to wait on. |
| 7168 | options: int |
| 7169 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7170 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7171 | / |
| 7172 | |
| 7173 | Returns the result of waiting for a process or processes. |
| 7174 | |
| 7175 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7176 | no children in a waitable state. |
| 7177 | [clinic start generated code]*/ |
| 7178 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7179 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7180 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7181 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7182 | { |
| 7183 | PyObject *result; |
| 7184 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7185 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7186 | siginfo_t si; |
| 7187 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7188 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7189 | do { |
| 7190 | Py_BEGIN_ALLOW_THREADS |
| 7191 | res = waitid(idtype, id, &si, options); |
| 7192 | Py_END_ALLOW_THREADS |
| 7193 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7194 | if (res < 0) |
| 7195 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7196 | |
| 7197 | if (si.si_pid == 0) |
| 7198 | Py_RETURN_NONE; |
| 7199 | |
| 7200 | result = PyStructSequence_New(&WaitidResultType); |
| 7201 | if (!result) |
| 7202 | return NULL; |
| 7203 | |
| 7204 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7205 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7206 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7207 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7208 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7209 | if (PyErr_Occurred()) { |
| 7210 | Py_DECREF(result); |
| 7211 | return NULL; |
| 7212 | } |
| 7213 | |
| 7214 | return result; |
| 7215 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7216 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7217 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7218 | |
| 7219 | #if defined(HAVE_WAITPID) |
| 7220 | /*[clinic input] |
| 7221 | os.waitpid |
| 7222 | pid: pid_t |
| 7223 | options: int |
| 7224 | / |
| 7225 | |
| 7226 | Wait for completion of a given child process. |
| 7227 | |
| 7228 | Returns a tuple of information regarding the child process: |
| 7229 | (pid, status) |
| 7230 | |
| 7231 | The options argument is ignored on Windows. |
| 7232 | [clinic start generated code]*/ |
| 7233 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7234 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7235 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7236 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7237 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7238 | pid_t res; |
| 7239 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7240 | WAIT_TYPE status; |
| 7241 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7242 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7243 | do { |
| 7244 | Py_BEGIN_ALLOW_THREADS |
| 7245 | res = waitpid(pid, &status, options); |
| 7246 | Py_END_ALLOW_THREADS |
| 7247 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7248 | if (res < 0) |
| 7249 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7250 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7251 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7252 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7253 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7254 | /* 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] | 7255 | /*[clinic input] |
| 7256 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7257 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7258 | options: int |
| 7259 | / |
| 7260 | |
| 7261 | Wait for completion of a given process. |
| 7262 | |
| 7263 | Returns a tuple of information regarding the process: |
| 7264 | (pid, status << 8) |
| 7265 | |
| 7266 | The options argument is ignored on Windows. |
| 7267 | [clinic start generated code]*/ |
| 7268 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7269 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7270 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7271 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7272 | { |
| 7273 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7274 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7275 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7276 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7277 | do { |
| 7278 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7279 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7280 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7281 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7282 | Py_END_ALLOW_THREADS |
| 7283 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7284 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7285 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7286 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7287 | /* 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] | 7288 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7289 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7290 | #endif |
| 7291 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7292 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7293 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7294 | /*[clinic input] |
| 7295 | os.wait |
| 7296 | |
| 7297 | Wait for completion of a child process. |
| 7298 | |
| 7299 | Returns a tuple of information about the child process: |
| 7300 | (pid, status) |
| 7301 | [clinic start generated code]*/ |
| 7302 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7303 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7304 | os_wait_impl(PyObject *module) |
| 7305 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7306 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7307 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7308 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7309 | WAIT_TYPE status; |
| 7310 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7311 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7312 | do { |
| 7313 | Py_BEGIN_ALLOW_THREADS |
| 7314 | pid = wait(&status); |
| 7315 | Py_END_ALLOW_THREADS |
| 7316 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7317 | if (pid < 0) |
| 7318 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7319 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7320 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7321 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7322 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7323 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7324 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7325 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 7326 | PyDoc_STRVAR(readlink__doc__, |
| 7327 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 7328 | Return a string representing the path to which the symbolic link points.\n\ |
| 7329 | \n\ |
| 7330 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7331 | and path should be relative; path will then be relative to that directory.\n\ |
| 7332 | dir_fd may not be implemented on your platform.\n\ |
| 7333 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7334 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7335 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7336 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7337 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7338 | /* AC 3.5: merge win32 and not together */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7339 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7340 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7341 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7342 | path_t path; |
| 7343 | int dir_fd = DEFAULT_DIR_FD; |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 7344 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7345 | ssize_t length; |
| 7346 | PyObject *return_value = NULL; |
| 7347 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7348 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7349 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7350 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7351 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 7352 | path_converter, &path, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7353 | READLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7354 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7355 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7356 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7357 | #ifdef HAVE_READLINKAT |
| 7358 | if (dir_fd != DEFAULT_DIR_FD) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 7359 | length = readlinkat(dir_fd, path.narrow, buffer, MAXPATHLEN); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 7360 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7361 | #endif |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 7362 | length = readlink(path.narrow, buffer, MAXPATHLEN); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7363 | Py_END_ALLOW_THREADS |
| 7364 | |
| 7365 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7366 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7367 | goto exit; |
| 7368 | } |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 7369 | buffer[length] = '\0'; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7370 | |
| 7371 | if (PyUnicode_Check(path.object)) |
| 7372 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7373 | else |
| 7374 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 7375 | exit: |
| 7376 | path_cleanup(&path); |
| 7377 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7378 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7379 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7380 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7381 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7382 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 7383 | |
| 7384 | static PyObject * |
| 7385 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 7386 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7387 | const wchar_t *path; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7388 | DWORD n_bytes_returned; |
| 7389 | DWORD io_result; |
| 7390 | PyObject *po, *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7391 | int dir_fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7392 | HANDLE reparse_point_handle; |
| 7393 | |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 7394 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7395 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7396 | const wchar_t *print_name; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7397 | |
| 7398 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7399 | |
| 7400 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7401 | &po, |
| 7402 | dir_fd_unavailable, &dir_fd |
| 7403 | )) |
| 7404 | return NULL; |
| 7405 | |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 7406 | path = _PyUnicode_AsUnicode(po); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7407 | if (path == NULL) |
| 7408 | return NULL; |
| 7409 | |
| 7410 | /* First get a handle to the reparse point */ |
| 7411 | Py_BEGIN_ALLOW_THREADS |
| 7412 | reparse_point_handle = CreateFileW( |
| 7413 | path, |
| 7414 | 0, |
| 7415 | 0, |
| 7416 | 0, |
| 7417 | OPEN_EXISTING, |
| 7418 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7419 | 0); |
| 7420 | Py_END_ALLOW_THREADS |
| 7421 | |
| 7422 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 7423 | return win32_error_object("readlink", po); |
| 7424 | |
| 7425 | Py_BEGIN_ALLOW_THREADS |
| 7426 | /* New call DeviceIoControl to read the reparse point */ |
| 7427 | io_result = DeviceIoControl( |
| 7428 | reparse_point_handle, |
| 7429 | FSCTL_GET_REPARSE_POINT, |
| 7430 | 0, 0, /* in buffer */ |
| 7431 | target_buffer, sizeof(target_buffer), |
| 7432 | &n_bytes_returned, |
| 7433 | 0 /* we're not using OVERLAPPED_IO */ |
| 7434 | ); |
| 7435 | CloseHandle(reparse_point_handle); |
| 7436 | Py_END_ALLOW_THREADS |
| 7437 | |
| 7438 | if (io_result==0) |
| 7439 | return win32_error_object("readlink", po); |
| 7440 | |
| 7441 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7442 | { |
| 7443 | PyErr_SetString(PyExc_ValueError, |
| 7444 | "not a symbolic link"); |
| 7445 | return NULL; |
| 7446 | } |
SSE4 | 3c34aad | 2018-02-13 00:10:35 +0700 | [diff] [blame] | 7447 | print_name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7448 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7449 | |
| 7450 | result = PyUnicode_FromWideChar(print_name, |
SSE4 | 3c34aad | 2018-02-13 00:10:35 +0700 | [diff] [blame] | 7451 | rdb->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7452 | return result; |
| 7453 | } |
| 7454 | |
| 7455 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7456 | |
| 7457 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7458 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7459 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7460 | |
| 7461 | #if defined(MS_WINDOWS) |
| 7462 | |
| 7463 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7464 | static BOOLEAN (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7465 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7466 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7467 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7468 | { |
| 7469 | HINSTANCE hKernel32; |
| 7470 | /* only recheck */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7471 | if (Py_CreateSymbolicLinkW) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7472 | return 1; |
| 7473 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7474 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7475 | "CreateSymbolicLinkW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7476 | return Py_CreateSymbolicLinkW != NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7477 | } |
| 7478 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7479 | /* Remove the last portion of the path - return 0 on success */ |
| 7480 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7481 | _dirnameW(WCHAR *path) |
| 7482 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7483 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7484 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 7485 | if (length == MAX_PATH) { |
| 7486 | return -1; |
| 7487 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7488 | |
| 7489 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7490 | for(ptr = path + length; ptr != path; ptr--) { |
| 7491 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7492 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7493 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7494 | } |
| 7495 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7496 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7497 | } |
| 7498 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7499 | /* Is this path absolute? */ |
| 7500 | static int |
| 7501 | _is_absW(const WCHAR *path) |
| 7502 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7503 | return path[0] == L'\\' || path[0] == L'/' || |
| 7504 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7505 | } |
| 7506 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7507 | /* join root and rest with a backslash - return 0 on success */ |
| 7508 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7509 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7510 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7511 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7512 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7513 | } |
| 7514 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7515 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 7516 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7517 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7518 | |
| 7519 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 7520 | return -1; |
| 7521 | } |
| 7522 | |
| 7523 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7524 | } |
| 7525 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7526 | /* Return True if the path at src relative to dest is a directory */ |
| 7527 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7528 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7529 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7530 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7531 | WCHAR dest_parent[MAX_PATH]; |
| 7532 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7533 | |
| 7534 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7535 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 7536 | _dirnameW(dest_parent)) { |
| 7537 | return 0; |
| 7538 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7539 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7540 | if (_joinW(src_resolved, dest_parent, src)) { |
| 7541 | return 0; |
| 7542 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7543 | return ( |
| 7544 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7545 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7546 | ); |
| 7547 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7548 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7549 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7550 | |
| 7551 | /*[clinic input] |
| 7552 | os.symlink |
| 7553 | src: path_t |
| 7554 | dst: path_t |
| 7555 | target_is_directory: bool = False |
| 7556 | * |
| 7557 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7558 | |
| 7559 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7560 | |
| 7561 | Create a symbolic link pointing to src named dst. |
| 7562 | |
| 7563 | target_is_directory is required on Windows if the target is to be |
| 7564 | interpreted as a directory. (On Windows, symlink requires |
| 7565 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7566 | target_is_directory is ignored on non-Windows platforms. |
| 7567 | |
| 7568 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7569 | and path should be relative; path will then be relative to that directory. |
| 7570 | dir_fd may not be implemented on your platform. |
| 7571 | If it is unavailable, using it will raise a NotImplementedError. |
| 7572 | |
| 7573 | [clinic start generated code]*/ |
| 7574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7575 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7576 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7577 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7578 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7579 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7580 | #ifdef MS_WINDOWS |
| 7581 | DWORD result; |
| 7582 | #else |
| 7583 | int result; |
| 7584 | #endif |
| 7585 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7586 | #ifdef MS_WINDOWS |
| 7587 | if (!check_CreateSymbolicLink()) { |
| 7588 | PyErr_SetString(PyExc_NotImplementedError, |
| 7589 | "CreateSymbolicLink functions not found"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7590 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7591 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7592 | if (!win32_can_symlink) { |
| 7593 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7594 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7595 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7596 | #endif |
| 7597 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7598 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7599 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7600 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7601 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7602 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7603 | target_is_directory |= _check_dirW(src->wide, dst->wide); |
| 7604 | result = Py_CreateSymbolicLinkW(dst->wide, src->wide, |
| 7605 | target_is_directory); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7606 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7607 | Py_END_ALLOW_THREADS |
| 7608 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7609 | if (!result) |
| 7610 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7611 | |
| 7612 | #else |
| 7613 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 7614 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 7615 | PyErr_SetString(PyExc_ValueError, |
| 7616 | "symlink: src and dst must be the same type"); |
| 7617 | return NULL; |
| 7618 | } |
| 7619 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7620 | Py_BEGIN_ALLOW_THREADS |
| 7621 | #if HAVE_SYMLINKAT |
| 7622 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7623 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7624 | else |
| 7625 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7626 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7627 | Py_END_ALLOW_THREADS |
| 7628 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7629 | if (result) |
| 7630 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7631 | #endif |
| 7632 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7633 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7634 | } |
| 7635 | #endif /* HAVE_SYMLINK */ |
| 7636 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7637 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7638 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7639 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7640 | static PyStructSequence_Field times_result_fields[] = { |
| 7641 | {"user", "user time"}, |
| 7642 | {"system", "system time"}, |
| 7643 | {"children_user", "user time of children"}, |
| 7644 | {"children_system", "system time of children"}, |
| 7645 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7646 | {NULL} |
| 7647 | }; |
| 7648 | |
| 7649 | PyDoc_STRVAR(times_result__doc__, |
| 7650 | "times_result: Result from os.times().\n\n\ |
| 7651 | This object may be accessed either as a tuple of\n\ |
| 7652 | (user, system, children_user, children_system, elapsed),\n\ |
| 7653 | or via the attributes user, system, children_user, children_system,\n\ |
| 7654 | and elapsed.\n\ |
| 7655 | \n\ |
| 7656 | See os.times for more information."); |
| 7657 | |
| 7658 | static PyStructSequence_Desc times_result_desc = { |
| 7659 | "times_result", /* name */ |
| 7660 | times_result__doc__, /* doc */ |
| 7661 | times_result_fields, |
| 7662 | 5 |
| 7663 | }; |
| 7664 | |
| 7665 | static PyTypeObject TimesResultType; |
| 7666 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7667 | #ifdef MS_WINDOWS |
| 7668 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7669 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7670 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7671 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7672 | |
| 7673 | static PyObject * |
| 7674 | build_times_result(double user, double system, |
| 7675 | double children_user, double children_system, |
| 7676 | double elapsed) |
| 7677 | { |
| 7678 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7679 | if (value == NULL) |
| 7680 | return NULL; |
| 7681 | |
| 7682 | #define SET(i, field) \ |
| 7683 | { \ |
| 7684 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7685 | if (!o) { \ |
| 7686 | Py_DECREF(value); \ |
| 7687 | return NULL; \ |
| 7688 | } \ |
| 7689 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7690 | } \ |
| 7691 | |
| 7692 | SET(0, user); |
| 7693 | SET(1, system); |
| 7694 | SET(2, children_user); |
| 7695 | SET(3, children_system); |
| 7696 | SET(4, elapsed); |
| 7697 | |
| 7698 | #undef SET |
| 7699 | |
| 7700 | return value; |
| 7701 | } |
| 7702 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7703 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7704 | #ifndef MS_WINDOWS |
| 7705 | #define NEED_TICKS_PER_SECOND |
| 7706 | static long ticks_per_second = -1; |
| 7707 | #endif /* MS_WINDOWS */ |
| 7708 | |
| 7709 | /*[clinic input] |
| 7710 | os.times |
| 7711 | |
| 7712 | Return a collection containing process timing information. |
| 7713 | |
| 7714 | The object returned behaves like a named tuple with these fields: |
| 7715 | (utime, stime, cutime, cstime, elapsed_time) |
| 7716 | All fields are floating point numbers. |
| 7717 | [clinic start generated code]*/ |
| 7718 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7719 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7720 | os_times_impl(PyObject *module) |
| 7721 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7722 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7723 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7724 | FILETIME create, exit, kernel, user; |
| 7725 | HANDLE hProc; |
| 7726 | hProc = GetCurrentProcess(); |
| 7727 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7728 | /* The fields of a FILETIME structure are the hi and lo part |
| 7729 | of a 64-bit value expressed in 100 nanosecond units. |
| 7730 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7731 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7732 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7733 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7734 | (double)(user.dwHighDateTime*429.4967296 + |
| 7735 | user.dwLowDateTime*1e-7), |
| 7736 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7737 | kernel.dwLowDateTime*1e-7), |
| 7738 | (double)0, |
| 7739 | (double)0, |
| 7740 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7741 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7742 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7743 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7744 | |
| 7745 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7746 | struct tms t; |
| 7747 | clock_t c; |
| 7748 | errno = 0; |
| 7749 | c = times(&t); |
| 7750 | if (c == (clock_t) -1) |
| 7751 | return posix_error(); |
| 7752 | return build_times_result( |
| 7753 | (double)t.tms_utime / ticks_per_second, |
| 7754 | (double)t.tms_stime / ticks_per_second, |
| 7755 | (double)t.tms_cutime / ticks_per_second, |
| 7756 | (double)t.tms_cstime / ticks_per_second, |
| 7757 | (double)c / ticks_per_second); |
| 7758 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7759 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7760 | #endif /* HAVE_TIMES */ |
| 7761 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7762 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7763 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7764 | /*[clinic input] |
| 7765 | os.getsid |
| 7766 | |
| 7767 | pid: pid_t |
| 7768 | / |
| 7769 | |
| 7770 | Call the system call getsid(pid) and return the result. |
| 7771 | [clinic start generated code]*/ |
| 7772 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7773 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7774 | os_getsid_impl(PyObject *module, pid_t pid) |
| 7775 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7776 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7777 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7778 | sid = getsid(pid); |
| 7779 | if (sid < 0) |
| 7780 | return posix_error(); |
| 7781 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7782 | } |
| 7783 | #endif /* HAVE_GETSID */ |
| 7784 | |
| 7785 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7786 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7787 | /*[clinic input] |
| 7788 | os.setsid |
| 7789 | |
| 7790 | Call the system call setsid(). |
| 7791 | [clinic start generated code]*/ |
| 7792 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7793 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7794 | os_setsid_impl(PyObject *module) |
| 7795 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7796 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7797 | if (setsid() < 0) |
| 7798 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7799 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7800 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7801 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7802 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7803 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7804 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7805 | /*[clinic input] |
| 7806 | os.setpgid |
| 7807 | |
| 7808 | pid: pid_t |
| 7809 | pgrp: pid_t |
| 7810 | / |
| 7811 | |
| 7812 | Call the system call setpgid(pid, pgrp). |
| 7813 | [clinic start generated code]*/ |
| 7814 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7815 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7816 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 7817 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7818 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7819 | if (setpgid(pid, pgrp) < 0) |
| 7820 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7821 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7822 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7823 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7824 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7825 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7826 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7827 | /*[clinic input] |
| 7828 | os.tcgetpgrp |
| 7829 | |
| 7830 | fd: int |
| 7831 | / |
| 7832 | |
| 7833 | Return the process group associated with the terminal specified by fd. |
| 7834 | [clinic start generated code]*/ |
| 7835 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7836 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7837 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 7838 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7839 | { |
| 7840 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7841 | if (pgid < 0) |
| 7842 | return posix_error(); |
| 7843 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7844 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7845 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7846 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7847 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7848 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7849 | /*[clinic input] |
| 7850 | os.tcsetpgrp |
| 7851 | |
| 7852 | fd: int |
| 7853 | pgid: pid_t |
| 7854 | / |
| 7855 | |
| 7856 | Set the process group associated with the terminal specified by fd. |
| 7857 | [clinic start generated code]*/ |
| 7858 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7859 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7860 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 7861 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7862 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7863 | if (tcsetpgrp(fd, pgid) < 0) |
| 7864 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7865 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7866 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7867 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7868 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7869 | /* Functions acting on file descriptors */ |
| 7870 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7871 | #ifdef O_CLOEXEC |
| 7872 | extern int _Py_open_cloexec_works; |
| 7873 | #endif |
| 7874 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7875 | |
| 7876 | /*[clinic input] |
| 7877 | os.open -> int |
| 7878 | path: path_t |
| 7879 | flags: int |
| 7880 | mode: int = 0o777 |
| 7881 | * |
| 7882 | dir_fd: dir_fd(requires='openat') = None |
| 7883 | |
| 7884 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7885 | |
| 7886 | Open a file for low level IO. Returns a file descriptor (integer). |
| 7887 | |
| 7888 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7889 | and path should be relative; path will then be relative to that directory. |
| 7890 | dir_fd may not be implemented on your platform. |
| 7891 | If it is unavailable, using it will raise a NotImplementedError. |
| 7892 | [clinic start generated code]*/ |
| 7893 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7894 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7895 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 7896 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7897 | { |
| 7898 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7899 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7900 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7901 | #ifdef O_CLOEXEC |
| 7902 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 7903 | #elif !defined(MS_WINDOWS) |
| 7904 | int *atomic_flag_works = NULL; |
| 7905 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7906 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7907 | #ifdef MS_WINDOWS |
| 7908 | flags |= O_NOINHERIT; |
| 7909 | #elif defined(O_CLOEXEC) |
| 7910 | flags |= O_CLOEXEC; |
| 7911 | #endif |
| 7912 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7913 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7914 | do { |
| 7915 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7916 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7917 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 7918 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7919 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7920 | if (dir_fd != DEFAULT_DIR_FD) |
| 7921 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 7922 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 7923 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7924 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 7925 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7926 | Py_END_ALLOW_THREADS |
| 7927 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7928 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7929 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7930 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7931 | if (!async_err) |
| 7932 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7933 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7934 | } |
| 7935 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7936 | #ifndef MS_WINDOWS |
| 7937 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 7938 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7939 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7940 | } |
| 7941 | #endif |
| 7942 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7943 | return fd; |
| 7944 | } |
| 7945 | |
| 7946 | |
| 7947 | /*[clinic input] |
| 7948 | os.close |
| 7949 | |
| 7950 | fd: int |
| 7951 | |
| 7952 | Close a file descriptor. |
| 7953 | [clinic start generated code]*/ |
| 7954 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7955 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7956 | os_close_impl(PyObject *module, int fd) |
| 7957 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7958 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7959 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7960 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 7961 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 7962 | * for more details. |
| 7963 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7964 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7965 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7966 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7967 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7968 | Py_END_ALLOW_THREADS |
| 7969 | if (res < 0) |
| 7970 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7971 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7972 | } |
| 7973 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7974 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7975 | /*[clinic input] |
| 7976 | os.closerange |
| 7977 | |
| 7978 | fd_low: int |
| 7979 | fd_high: int |
| 7980 | / |
| 7981 | |
| 7982 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 7983 | [clinic start generated code]*/ |
| 7984 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7985 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7986 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 7987 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7988 | { |
| 7989 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7990 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7991 | _Py_BEGIN_SUPPRESS_IPH |
Benjamin Peterson | 207116b | 2016-09-08 11:28:06 -0700 | [diff] [blame] | 7992 | for (i = Py_MAX(fd_low, 0); i < fd_high; i++) |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 7993 | close(i); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7994 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7995 | Py_END_ALLOW_THREADS |
| 7996 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7997 | } |
| 7998 | |
| 7999 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8000 | /*[clinic input] |
| 8001 | os.dup -> int |
| 8002 | |
| 8003 | fd: int |
| 8004 | / |
| 8005 | |
| 8006 | Return a duplicate of a file descriptor. |
| 8007 | [clinic start generated code]*/ |
| 8008 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8009 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8010 | os_dup_impl(PyObject *module, int fd) |
| 8011 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8012 | { |
| 8013 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8014 | } |
| 8015 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8016 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8017 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8018 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8019 | fd: int |
| 8020 | fd2: int |
| 8021 | inheritable: bool=True |
| 8022 | |
| 8023 | Duplicate file descriptor. |
| 8024 | [clinic start generated code]*/ |
| 8025 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8026 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8027 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8028 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8029 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8030 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8031 | #if defined(HAVE_DUP3) && \ |
| 8032 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8033 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8034 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8035 | #endif |
| 8036 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8037 | if (fd < 0 || fd2 < 0) { |
| 8038 | posix_error(); |
| 8039 | return -1; |
| 8040 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8041 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8042 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8043 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8044 | * upon close(), and therefore below. |
| 8045 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8046 | #ifdef MS_WINDOWS |
| 8047 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8048 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8049 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8050 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8051 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8052 | if (res < 0) { |
| 8053 | posix_error(); |
| 8054 | return -1; |
| 8055 | } |
| 8056 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8057 | |
| 8058 | /* Character files like console cannot be make non-inheritable */ |
| 8059 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8060 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8061 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8062 | } |
| 8063 | |
| 8064 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8065 | Py_BEGIN_ALLOW_THREADS |
| 8066 | if (!inheritable) |
| 8067 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8068 | else |
| 8069 | res = dup2(fd, fd2); |
| 8070 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8071 | if (res < 0) { |
| 8072 | posix_error(); |
| 8073 | return -1; |
| 8074 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8075 | |
| 8076 | #else |
| 8077 | |
| 8078 | #ifdef HAVE_DUP3 |
| 8079 | if (!inheritable && dup3_works != 0) { |
| 8080 | Py_BEGIN_ALLOW_THREADS |
| 8081 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8082 | Py_END_ALLOW_THREADS |
| 8083 | if (res < 0) { |
| 8084 | if (dup3_works == -1) |
| 8085 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8086 | if (dup3_works) { |
| 8087 | posix_error(); |
| 8088 | return -1; |
| 8089 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8090 | } |
| 8091 | } |
| 8092 | |
| 8093 | if (inheritable || dup3_works == 0) |
| 8094 | { |
| 8095 | #endif |
| 8096 | Py_BEGIN_ALLOW_THREADS |
| 8097 | res = dup2(fd, fd2); |
| 8098 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8099 | if (res < 0) { |
| 8100 | posix_error(); |
| 8101 | return -1; |
| 8102 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8103 | |
| 8104 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8105 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8106 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8107 | } |
| 8108 | #ifdef HAVE_DUP3 |
| 8109 | } |
| 8110 | #endif |
| 8111 | |
| 8112 | #endif |
| 8113 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8114 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8115 | } |
| 8116 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8117 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8118 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8119 | /*[clinic input] |
| 8120 | os.lockf |
| 8121 | |
| 8122 | fd: int |
| 8123 | An open file descriptor. |
| 8124 | command: int |
| 8125 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8126 | length: Py_off_t |
| 8127 | The number of bytes to lock, starting at the current position. |
| 8128 | / |
| 8129 | |
| 8130 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8131 | |
| 8132 | [clinic start generated code]*/ |
| 8133 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8134 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8135 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8136 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8137 | { |
| 8138 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8139 | |
| 8140 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8141 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8142 | Py_END_ALLOW_THREADS |
| 8143 | |
| 8144 | if (res < 0) |
| 8145 | return posix_error(); |
| 8146 | |
| 8147 | Py_RETURN_NONE; |
| 8148 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8149 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 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.lseek -> Py_off_t |
| 8154 | |
| 8155 | fd: int |
| 8156 | position: Py_off_t |
| 8157 | how: int |
| 8158 | / |
| 8159 | |
| 8160 | Set the position of a file descriptor. Return the new position. |
| 8161 | |
| 8162 | Return the new cursor position in number of bytes |
| 8163 | relative to the beginning of the file. |
| 8164 | [clinic start generated code]*/ |
| 8165 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8166 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8167 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8168 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8169 | { |
| 8170 | Py_off_t result; |
| 8171 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8172 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8173 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8174 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8175 | case 0: how = SEEK_SET; break; |
| 8176 | case 1: how = SEEK_CUR; break; |
| 8177 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8178 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8179 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8180 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8181 | if (PyErr_Occurred()) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8182 | return -1; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8184 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8185 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8186 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8187 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8188 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8189 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8190 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8191 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8192 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8193 | if (result < 0) |
| 8194 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8195 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8196 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8197 | } |
| 8198 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8199 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8200 | /*[clinic input] |
| 8201 | os.read |
| 8202 | fd: int |
| 8203 | length: Py_ssize_t |
| 8204 | / |
| 8205 | |
| 8206 | Read from a file descriptor. Returns a bytes object. |
| 8207 | [clinic start generated code]*/ |
| 8208 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8209 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8210 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 8211 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8212 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8213 | Py_ssize_t n; |
| 8214 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8215 | |
| 8216 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8217 | errno = EINVAL; |
| 8218 | return posix_error(); |
| 8219 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8220 | |
| 8221 | #ifdef MS_WINDOWS |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8222 | /* On Windows, the count parameter of read() is an int */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8223 | if (length > INT_MAX) |
| 8224 | length = INT_MAX; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8225 | #endif |
| 8226 | |
| 8227 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8228 | if (buffer == NULL) |
| 8229 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8230 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8231 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8232 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8233 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8234 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8235 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8236 | |
| 8237 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8238 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8239 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8240 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8241 | } |
| 8242 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8243 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 8244 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8245 | static Py_ssize_t |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8246 | 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] | 8247 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8248 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8249 | Py_ssize_t blen, total = 0; |
| 8250 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8251 | *iov = PyMem_New(struct iovec, cnt); |
| 8252 | if (*iov == NULL) { |
| 8253 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8254 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8255 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8256 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8257 | *buf = PyMem_New(Py_buffer, cnt); |
| 8258 | if (*buf == NULL) { |
| 8259 | PyMem_Del(*iov); |
| 8260 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8261 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8262 | } |
| 8263 | |
| 8264 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8265 | PyObject *item = PySequence_GetItem(seq, i); |
| 8266 | if (item == NULL) |
| 8267 | goto fail; |
| 8268 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8269 | Py_DECREF(item); |
| 8270 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8271 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8272 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8273 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8274 | blen = (*buf)[i].len; |
| 8275 | (*iov)[i].iov_len = blen; |
| 8276 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8277 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8278 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8279 | |
| 8280 | fail: |
| 8281 | PyMem_Del(*iov); |
| 8282 | for (j = 0; j < i; j++) { |
| 8283 | PyBuffer_Release(&(*buf)[j]); |
| 8284 | } |
| 8285 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8286 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8287 | } |
| 8288 | |
| 8289 | static void |
| 8290 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8291 | { |
| 8292 | int i; |
| 8293 | PyMem_Del(iov); |
| 8294 | for (i = 0; i < cnt; i++) { |
| 8295 | PyBuffer_Release(&buf[i]); |
| 8296 | } |
| 8297 | PyMem_Del(buf); |
| 8298 | } |
| 8299 | #endif |
| 8300 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8301 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8302 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8303 | /*[clinic input] |
| 8304 | os.readv -> Py_ssize_t |
| 8305 | |
| 8306 | fd: int |
| 8307 | buffers: object |
| 8308 | / |
| 8309 | |
| 8310 | Read from a file descriptor fd into an iterable of buffers. |
| 8311 | |
| 8312 | The buffers should be mutable buffers accepting bytes. |
| 8313 | readv will transfer data into each buffer until it is full |
| 8314 | and then move on to the next buffer in the sequence to hold |
| 8315 | the rest of the data. |
| 8316 | |
| 8317 | readv returns the total number of bytes read, |
| 8318 | which may be less than the total capacity of all the buffers. |
| 8319 | [clinic start generated code]*/ |
| 8320 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8321 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8322 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 8323 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8324 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8325 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8326 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8327 | struct iovec *iov; |
| 8328 | Py_buffer *buf; |
| 8329 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8330 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8331 | PyErr_SetString(PyExc_TypeError, |
| 8332 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8333 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8334 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8335 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8336 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8337 | if (cnt < 0) |
| 8338 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8339 | |
| 8340 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 8341 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8342 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8343 | do { |
| 8344 | Py_BEGIN_ALLOW_THREADS |
| 8345 | n = readv(fd, iov, cnt); |
| 8346 | Py_END_ALLOW_THREADS |
| 8347 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8348 | |
| 8349 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8350 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8351 | if (!async_err) |
| 8352 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8353 | return -1; |
| 8354 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8355 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8356 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8357 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8358 | #endif /* HAVE_READV */ |
| 8359 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8360 | |
| 8361 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8362 | /*[clinic input] |
| 8363 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 8364 | os.pread |
| 8365 | |
| 8366 | fd: int |
| 8367 | length: int |
| 8368 | offset: Py_off_t |
| 8369 | / |
| 8370 | |
| 8371 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 8372 | |
| 8373 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 8374 | the beginning of the file. The file offset remains unchanged. |
| 8375 | [clinic start generated code]*/ |
| 8376 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8377 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8378 | os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset) |
| 8379 | /*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8380 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8381 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8382 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8383 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8384 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8385 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8386 | errno = EINVAL; |
| 8387 | return posix_error(); |
| 8388 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8389 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8390 | if (buffer == NULL) |
| 8391 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8392 | |
| 8393 | do { |
| 8394 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8395 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8396 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8397 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8398 | Py_END_ALLOW_THREADS |
| 8399 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8400 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8401 | if (n < 0) { |
| 8402 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8403 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8404 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8405 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8406 | _PyBytes_Resize(&buffer, n); |
| 8407 | return buffer; |
| 8408 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8409 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8410 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 8411 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 8412 | /*[clinic input] |
| 8413 | os.preadv -> Py_ssize_t |
| 8414 | |
| 8415 | fd: int |
| 8416 | buffers: object |
| 8417 | offset: Py_off_t |
| 8418 | flags: int = 0 |
| 8419 | / |
| 8420 | |
| 8421 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 8422 | |
| 8423 | Combines the functionality of readv() and pread(). As readv(), it will |
| 8424 | transfer data into each buffer until it is full and then move on to the next |
| 8425 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 8426 | specifies the file offset at which the input operation is to be performed. It |
| 8427 | will return the total number of bytes read (which can be less than the total |
| 8428 | capacity of all the objects). |
| 8429 | |
| 8430 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 8431 | |
| 8432 | - RWF_HIPRI |
| 8433 | - RWF_NOWAIT |
| 8434 | |
| 8435 | Using non-zero flags requires Linux 4.6 or newer. |
| 8436 | [clinic start generated code]*/ |
| 8437 | |
| 8438 | static Py_ssize_t |
| 8439 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 8440 | int flags) |
| 8441 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 8442 | { |
| 8443 | Py_ssize_t cnt, n; |
| 8444 | int async_err = 0; |
| 8445 | struct iovec *iov; |
| 8446 | Py_buffer *buf; |
| 8447 | |
| 8448 | if (!PySequence_Check(buffers)) { |
| 8449 | PyErr_SetString(PyExc_TypeError, |
| 8450 | "preadv2() arg 2 must be a sequence"); |
| 8451 | return -1; |
| 8452 | } |
| 8453 | |
| 8454 | cnt = PySequence_Size(buffers); |
| 8455 | if (cnt < 0) { |
| 8456 | return -1; |
| 8457 | } |
| 8458 | |
| 8459 | #ifndef HAVE_PREADV2 |
| 8460 | if(flags != 0) { |
| 8461 | argument_unavailable_error("preadv2", "flags"); |
| 8462 | return -1; |
| 8463 | } |
| 8464 | #endif |
| 8465 | |
| 8466 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 8467 | return -1; |
| 8468 | } |
| 8469 | #ifdef HAVE_PREADV2 |
| 8470 | do { |
| 8471 | Py_BEGIN_ALLOW_THREADS |
| 8472 | _Py_BEGIN_SUPPRESS_IPH |
| 8473 | n = preadv2(fd, iov, cnt, offset, flags); |
| 8474 | _Py_END_SUPPRESS_IPH |
| 8475 | Py_END_ALLOW_THREADS |
| 8476 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8477 | #else |
| 8478 | do { |
| 8479 | Py_BEGIN_ALLOW_THREADS |
| 8480 | _Py_BEGIN_SUPPRESS_IPH |
| 8481 | n = preadv(fd, iov, cnt, offset); |
| 8482 | _Py_END_SUPPRESS_IPH |
| 8483 | Py_END_ALLOW_THREADS |
| 8484 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8485 | #endif |
| 8486 | |
| 8487 | iov_cleanup(iov, buf, cnt); |
| 8488 | if (n < 0) { |
| 8489 | if (!async_err) { |
| 8490 | posix_error(); |
| 8491 | } |
| 8492 | return -1; |
| 8493 | } |
| 8494 | |
| 8495 | return n; |
| 8496 | } |
| 8497 | #endif /* HAVE_PREADV */ |
| 8498 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8499 | |
| 8500 | /*[clinic input] |
| 8501 | os.write -> Py_ssize_t |
| 8502 | |
| 8503 | fd: int |
| 8504 | data: Py_buffer |
| 8505 | / |
| 8506 | |
| 8507 | Write a bytes object to a file descriptor. |
| 8508 | [clinic start generated code]*/ |
| 8509 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8510 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8511 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 8512 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8513 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8514 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8515 | } |
| 8516 | |
| 8517 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8518 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8519 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8520 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8521 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8522 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8523 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8524 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8525 | static PyObject * |
| 8526 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8527 | { |
| 8528 | int in, out; |
| 8529 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8530 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8531 | off_t offset; |
| 8532 | |
| 8533 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8534 | #ifndef __APPLE__ |
| 8535 | Py_ssize_t len; |
| 8536 | #endif |
| 8537 | PyObject *headers = NULL, *trailers = NULL; |
| 8538 | Py_buffer *hbuf, *tbuf; |
| 8539 | off_t sbytes; |
| 8540 | struct sf_hdtr sf; |
| 8541 | int flags = 0; |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8542 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8543 | static char *keywords[] = {"out", "in", |
| 8544 | "offset", "count", |
| 8545 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8546 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8547 | sf.headers = NULL; |
| 8548 | sf.trailers = NULL; |
| 8549 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8550 | #ifdef __APPLE__ |
| 8551 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8552 | 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] | 8553 | #else |
| 8554 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8555 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8556 | #endif |
| 8557 | &headers, &trailers, &flags)) |
| 8558 | return NULL; |
| 8559 | if (headers != NULL) { |
| 8560 | if (!PySequence_Check(headers)) { |
| 8561 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8562 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8563 | return NULL; |
| 8564 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8565 | Py_ssize_t i = PySequence_Size(headers); |
| 8566 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8567 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8568 | if (i > INT_MAX) { |
| 8569 | PyErr_SetString(PyExc_OverflowError, |
| 8570 | "sendfile() header is too large"); |
| 8571 | return NULL; |
| 8572 | } |
| 8573 | if (i > 0) { |
| 8574 | sf.hdr_cnt = (int)i; |
| 8575 | i = iov_setup(&(sf.headers), &hbuf, |
| 8576 | headers, sf.hdr_cnt, PyBUF_SIMPLE); |
| 8577 | if (i < 0) |
| 8578 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8579 | #ifdef __APPLE__ |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8580 | sbytes += i; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8581 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8582 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8583 | } |
| 8584 | } |
| 8585 | if (trailers != NULL) { |
| 8586 | if (!PySequence_Check(trailers)) { |
| 8587 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8588 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8589 | return NULL; |
| 8590 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8591 | Py_ssize_t i = PySequence_Size(trailers); |
| 8592 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8593 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8594 | if (i > INT_MAX) { |
| 8595 | PyErr_SetString(PyExc_OverflowError, |
| 8596 | "sendfile() trailer is too large"); |
| 8597 | return NULL; |
| 8598 | } |
| 8599 | if (i > 0) { |
| 8600 | sf.trl_cnt = (int)i; |
| 8601 | i = iov_setup(&(sf.trailers), &tbuf, |
| 8602 | trailers, sf.trl_cnt, PyBUF_SIMPLE); |
| 8603 | if (i < 0) |
| 8604 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8605 | #ifdef __APPLE__ |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8606 | sbytes += i; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8607 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8608 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8609 | } |
| 8610 | } |
| 8611 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8612 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8613 | do { |
| 8614 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8615 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8616 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8617 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8618 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8619 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8620 | Py_END_ALLOW_THREADS |
| 8621 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8622 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8623 | |
| 8624 | if (sf.headers != NULL) |
| 8625 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8626 | if (sf.trailers != NULL) |
| 8627 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8628 | |
| 8629 | if (ret < 0) { |
| 8630 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8631 | if (sbytes != 0) { |
| 8632 | // some data has been sent |
| 8633 | goto done; |
| 8634 | } |
| 8635 | else { |
| 8636 | // no data has been sent; upper application is supposed |
| 8637 | // to retry on EAGAIN or EBUSY |
| 8638 | return posix_error(); |
| 8639 | } |
| 8640 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8641 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8642 | } |
| 8643 | goto done; |
| 8644 | |
| 8645 | done: |
| 8646 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8647 | return Py_BuildValue("l", sbytes); |
| 8648 | #else |
| 8649 | return Py_BuildValue("L", sbytes); |
| 8650 | #endif |
| 8651 | |
| 8652 | #else |
| 8653 | Py_ssize_t count; |
| 8654 | PyObject *offobj; |
| 8655 | static char *keywords[] = {"out", "in", |
| 8656 | "offset", "count", NULL}; |
| 8657 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8658 | keywords, &out, &in, &offobj, &count)) |
| 8659 | return NULL; |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 8660 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8661 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8662 | do { |
| 8663 | Py_BEGIN_ALLOW_THREADS |
| 8664 | ret = sendfile(out, in, NULL, count); |
| 8665 | Py_END_ALLOW_THREADS |
| 8666 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8667 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8668 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8669 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8670 | } |
| 8671 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8672 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8673 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8674 | |
| 8675 | do { |
| 8676 | Py_BEGIN_ALLOW_THREADS |
| 8677 | ret = sendfile(out, in, &offset, count); |
| 8678 | Py_END_ALLOW_THREADS |
| 8679 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8680 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8681 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8682 | return Py_BuildValue("n", ret); |
| 8683 | #endif |
| 8684 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8685 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8686 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8687 | |
| 8688 | /*[clinic input] |
| 8689 | os.fstat |
| 8690 | |
| 8691 | fd : int |
| 8692 | |
| 8693 | Perform a stat system call on the given file descriptor. |
| 8694 | |
| 8695 | Like stat(), but for an open file descriptor. |
| 8696 | Equivalent to os.stat(fd). |
| 8697 | [clinic start generated code]*/ |
| 8698 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8699 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8700 | os_fstat_impl(PyObject *module, int fd) |
| 8701 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8702 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8703 | STRUCT_STAT st; |
| 8704 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8705 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8706 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8707 | do { |
| 8708 | Py_BEGIN_ALLOW_THREADS |
| 8709 | res = FSTAT(fd, &st); |
| 8710 | Py_END_ALLOW_THREADS |
| 8711 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8712 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8713 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8714 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8715 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8716 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8717 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8718 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8719 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8720 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8721 | } |
| 8722 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8723 | |
| 8724 | /*[clinic input] |
| 8725 | os.isatty -> bool |
| 8726 | fd: int |
| 8727 | / |
| 8728 | |
| 8729 | Return True if the fd is connected to a terminal. |
| 8730 | |
| 8731 | Return True if the file descriptor is an open file descriptor |
| 8732 | connected to the slave end of a terminal. |
| 8733 | [clinic start generated code]*/ |
| 8734 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8735 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8736 | os_isatty_impl(PyObject *module, int fd) |
| 8737 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8738 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8739 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8740 | _Py_BEGIN_SUPPRESS_IPH |
| 8741 | return_value = isatty(fd); |
| 8742 | _Py_END_SUPPRESS_IPH |
| 8743 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8744 | } |
| 8745 | |
| 8746 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8747 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8748 | /*[clinic input] |
| 8749 | os.pipe |
| 8750 | |
| 8751 | Create a pipe. |
| 8752 | |
| 8753 | Returns a tuple of two file descriptors: |
| 8754 | (read_fd, write_fd) |
| 8755 | [clinic start generated code]*/ |
| 8756 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8757 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8758 | os_pipe_impl(PyObject *module) |
| 8759 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8760 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8761 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8762 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8763 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8764 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8765 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8766 | #else |
| 8767 | int res; |
| 8768 | #endif |
| 8769 | |
| 8770 | #ifdef MS_WINDOWS |
| 8771 | attr.nLength = sizeof(attr); |
| 8772 | attr.lpSecurityDescriptor = NULL; |
| 8773 | attr.bInheritHandle = FALSE; |
| 8774 | |
| 8775 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 8776 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8777 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8778 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 8779 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 8780 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8781 | if (fds[0] == -1 || fds[1] == -1) { |
| 8782 | CloseHandle(read); |
| 8783 | CloseHandle(write); |
| 8784 | ok = 0; |
| 8785 | } |
| 8786 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 8787 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8788 | Py_END_ALLOW_THREADS |
| 8789 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8790 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8791 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8792 | #else |
| 8793 | |
| 8794 | #ifdef HAVE_PIPE2 |
| 8795 | Py_BEGIN_ALLOW_THREADS |
| 8796 | res = pipe2(fds, O_CLOEXEC); |
| 8797 | Py_END_ALLOW_THREADS |
| 8798 | |
| 8799 | if (res != 0 && errno == ENOSYS) |
| 8800 | { |
| 8801 | #endif |
| 8802 | Py_BEGIN_ALLOW_THREADS |
| 8803 | res = pipe(fds); |
| 8804 | Py_END_ALLOW_THREADS |
| 8805 | |
| 8806 | if (res == 0) { |
| 8807 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 8808 | close(fds[0]); |
| 8809 | close(fds[1]); |
| 8810 | return NULL; |
| 8811 | } |
| 8812 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 8813 | close(fds[0]); |
| 8814 | close(fds[1]); |
| 8815 | return NULL; |
| 8816 | } |
| 8817 | } |
| 8818 | #ifdef HAVE_PIPE2 |
| 8819 | } |
| 8820 | #endif |
| 8821 | |
| 8822 | if (res != 0) |
| 8823 | return PyErr_SetFromErrno(PyExc_OSError); |
| 8824 | #endif /* !MS_WINDOWS */ |
| 8825 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8826 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8827 | #endif /* HAVE_PIPE */ |
| 8828 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8829 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8830 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8831 | /*[clinic input] |
| 8832 | os.pipe2 |
| 8833 | |
| 8834 | flags: int |
| 8835 | / |
| 8836 | |
| 8837 | Create a pipe with flags set atomically. |
| 8838 | |
| 8839 | Returns a tuple of two file descriptors: |
| 8840 | (read_fd, write_fd) |
| 8841 | |
| 8842 | flags can be constructed by ORing together one or more of these values: |
| 8843 | O_NONBLOCK, O_CLOEXEC. |
| 8844 | [clinic start generated code]*/ |
| 8845 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8846 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8847 | os_pipe2_impl(PyObject *module, int flags) |
| 8848 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8849 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8850 | int fds[2]; |
| 8851 | int res; |
| 8852 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8853 | res = pipe2(fds, flags); |
| 8854 | if (res != 0) |
| 8855 | return posix_error(); |
| 8856 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8857 | } |
| 8858 | #endif /* HAVE_PIPE2 */ |
| 8859 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8860 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8861 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8862 | /*[clinic input] |
| 8863 | os.writev -> Py_ssize_t |
| 8864 | fd: int |
| 8865 | buffers: object |
| 8866 | / |
| 8867 | |
| 8868 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 8869 | |
| 8870 | Returns the total number of bytes written. |
| 8871 | buffers must be a sequence of bytes-like objects. |
| 8872 | [clinic start generated code]*/ |
| 8873 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8874 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8875 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 8876 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8877 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8878 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8879 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8880 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8881 | struct iovec *iov; |
| 8882 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8883 | |
| 8884 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8885 | PyErr_SetString(PyExc_TypeError, |
| 8886 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8887 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8888 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8889 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8890 | if (cnt < 0) |
| 8891 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8892 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8893 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 8894 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8895 | } |
| 8896 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8897 | do { |
| 8898 | Py_BEGIN_ALLOW_THREADS |
| 8899 | result = writev(fd, iov, cnt); |
| 8900 | Py_END_ALLOW_THREADS |
| 8901 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8902 | |
| 8903 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8904 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8905 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8906 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8907 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8908 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8909 | #endif /* HAVE_WRITEV */ |
| 8910 | |
| 8911 | |
| 8912 | #ifdef HAVE_PWRITE |
| 8913 | /*[clinic input] |
| 8914 | os.pwrite -> Py_ssize_t |
| 8915 | |
| 8916 | fd: int |
| 8917 | buffer: Py_buffer |
| 8918 | offset: Py_off_t |
| 8919 | / |
| 8920 | |
| 8921 | Write bytes to a file descriptor starting at a particular offset. |
| 8922 | |
| 8923 | Write buffer to fd, starting at offset bytes from the beginning of |
| 8924 | the file. Returns the number of bytes writte. Does not change the |
| 8925 | current file offset. |
| 8926 | [clinic start generated code]*/ |
| 8927 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8928 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8929 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 8930 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8931 | { |
| 8932 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8933 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8934 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8935 | do { |
| 8936 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8937 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8938 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8939 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8940 | Py_END_ALLOW_THREADS |
| 8941 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8942 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8943 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8944 | posix_error(); |
| 8945 | return size; |
| 8946 | } |
| 8947 | #endif /* HAVE_PWRITE */ |
| 8948 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 8949 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 8950 | /*[clinic input] |
| 8951 | os.pwritev -> Py_ssize_t |
| 8952 | |
| 8953 | fd: int |
| 8954 | buffers: object |
| 8955 | offset: Py_off_t |
| 8956 | flags: int = 0 |
| 8957 | / |
| 8958 | |
| 8959 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 8960 | |
| 8961 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 8962 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 8963 | buffer is written before proceeding to second, and so on. The operating system may |
| 8964 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 8965 | This function writes the contents of each object to the file descriptor and returns |
| 8966 | the total number of bytes written. |
| 8967 | |
| 8968 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 8969 | |
| 8970 | - RWF_DSYNC |
| 8971 | - RWF_SYNC |
| 8972 | |
| 8973 | Using non-zero flags requires Linux 4.7 or newer. |
| 8974 | [clinic start generated code]*/ |
| 8975 | |
| 8976 | static Py_ssize_t |
| 8977 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 8978 | int flags) |
| 8979 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/ |
| 8980 | { |
| 8981 | Py_ssize_t cnt; |
| 8982 | Py_ssize_t result; |
| 8983 | int async_err = 0; |
| 8984 | struct iovec *iov; |
| 8985 | Py_buffer *buf; |
| 8986 | |
| 8987 | if (!PySequence_Check(buffers)) { |
| 8988 | PyErr_SetString(PyExc_TypeError, |
| 8989 | "pwritev() arg 2 must be a sequence"); |
| 8990 | return -1; |
| 8991 | } |
| 8992 | |
| 8993 | cnt = PySequence_Size(buffers); |
| 8994 | if (cnt < 0) { |
| 8995 | return -1; |
| 8996 | } |
| 8997 | |
| 8998 | #ifndef HAVE_PWRITEV2 |
| 8999 | if(flags != 0) { |
| 9000 | argument_unavailable_error("pwritev2", "flags"); |
| 9001 | return -1; |
| 9002 | } |
| 9003 | #endif |
| 9004 | |
| 9005 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9006 | return -1; |
| 9007 | } |
| 9008 | #ifdef HAVE_PWRITEV2 |
| 9009 | do { |
| 9010 | Py_BEGIN_ALLOW_THREADS |
| 9011 | _Py_BEGIN_SUPPRESS_IPH |
| 9012 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9013 | _Py_END_SUPPRESS_IPH |
| 9014 | Py_END_ALLOW_THREADS |
| 9015 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9016 | #else |
| 9017 | do { |
| 9018 | Py_BEGIN_ALLOW_THREADS |
| 9019 | _Py_BEGIN_SUPPRESS_IPH |
| 9020 | result = pwritev(fd, iov, cnt, offset); |
| 9021 | _Py_END_SUPPRESS_IPH |
| 9022 | Py_END_ALLOW_THREADS |
| 9023 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9024 | #endif |
| 9025 | |
| 9026 | iov_cleanup(iov, buf, cnt); |
| 9027 | if (result < 0) { |
| 9028 | if (!async_err) { |
| 9029 | posix_error(); |
| 9030 | } |
| 9031 | return -1; |
| 9032 | } |
| 9033 | |
| 9034 | return result; |
| 9035 | } |
| 9036 | #endif /* HAVE_PWRITEV */ |
| 9037 | |
| 9038 | |
| 9039 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9040 | |
| 9041 | #ifdef HAVE_MKFIFO |
| 9042 | /*[clinic input] |
| 9043 | os.mkfifo |
| 9044 | |
| 9045 | path: path_t |
| 9046 | mode: int=0o666 |
| 9047 | * |
| 9048 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9049 | |
| 9050 | Create a "fifo" (a POSIX named pipe). |
| 9051 | |
| 9052 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9053 | and path should be relative; path will then be relative to that directory. |
| 9054 | dir_fd may not be implemented on your platform. |
| 9055 | If it is unavailable, using it will raise a NotImplementedError. |
| 9056 | [clinic start generated code]*/ |
| 9057 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9058 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9059 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 9060 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9061 | { |
| 9062 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9063 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9064 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9065 | do { |
| 9066 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9067 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9068 | if (dir_fd != DEFAULT_DIR_FD) |
| 9069 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 9070 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9071 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9072 | result = mkfifo(path->narrow, mode); |
| 9073 | Py_END_ALLOW_THREADS |
| 9074 | } while (result != 0 && errno == EINTR && |
| 9075 | !(async_err = PyErr_CheckSignals())); |
| 9076 | if (result != 0) |
| 9077 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9078 | |
| 9079 | Py_RETURN_NONE; |
| 9080 | } |
| 9081 | #endif /* HAVE_MKFIFO */ |
| 9082 | |
| 9083 | |
| 9084 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 9085 | /*[clinic input] |
| 9086 | os.mknod |
| 9087 | |
| 9088 | path: path_t |
| 9089 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9090 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9091 | * |
| 9092 | dir_fd: dir_fd(requires='mknodat')=None |
| 9093 | |
| 9094 | Create a node in the file system. |
| 9095 | |
| 9096 | Create a node in the file system (file, device special file or named pipe) |
| 9097 | at path. mode specifies both the permissions to use and the |
| 9098 | type of node to be created, being combined (bitwise OR) with one of |
| 9099 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 9100 | device defines the newly created device special file (probably using |
| 9101 | os.makedev()). Otherwise device is ignored. |
| 9102 | |
| 9103 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9104 | and path should be relative; path will then be relative to that directory. |
| 9105 | dir_fd may not be implemented on your platform. |
| 9106 | If it is unavailable, using it will raise a NotImplementedError. |
| 9107 | [clinic start generated code]*/ |
| 9108 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9109 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9110 | 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] | 9111 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9112 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9113 | { |
| 9114 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9115 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9116 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9117 | do { |
| 9118 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9119 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9120 | if (dir_fd != DEFAULT_DIR_FD) |
| 9121 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 9122 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9123 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9124 | result = mknod(path->narrow, mode, device); |
| 9125 | Py_END_ALLOW_THREADS |
| 9126 | } while (result != 0 && errno == EINTR && |
| 9127 | !(async_err = PyErr_CheckSignals())); |
| 9128 | if (result != 0) |
| 9129 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9130 | |
| 9131 | Py_RETURN_NONE; |
| 9132 | } |
| 9133 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 9134 | |
| 9135 | |
| 9136 | #ifdef HAVE_DEVICE_MACROS |
| 9137 | /*[clinic input] |
| 9138 | os.major -> unsigned_int |
| 9139 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9140 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9141 | / |
| 9142 | |
| 9143 | Extracts a device major number from a raw device number. |
| 9144 | [clinic start generated code]*/ |
| 9145 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9146 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9147 | os_major_impl(PyObject *module, dev_t device) |
| 9148 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9149 | { |
| 9150 | return major(device); |
| 9151 | } |
| 9152 | |
| 9153 | |
| 9154 | /*[clinic input] |
| 9155 | os.minor -> unsigned_int |
| 9156 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9157 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9158 | / |
| 9159 | |
| 9160 | Extracts a device minor number from a raw device number. |
| 9161 | [clinic start generated code]*/ |
| 9162 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9163 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9164 | os_minor_impl(PyObject *module, dev_t device) |
| 9165 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9166 | { |
| 9167 | return minor(device); |
| 9168 | } |
| 9169 | |
| 9170 | |
| 9171 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9172 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9173 | |
| 9174 | major: int |
| 9175 | minor: int |
| 9176 | / |
| 9177 | |
| 9178 | Composes a raw device number from the major and minor device numbers. |
| 9179 | [clinic start generated code]*/ |
| 9180 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9181 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9182 | os_makedev_impl(PyObject *module, int major, int minor) |
| 9183 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9184 | { |
| 9185 | return makedev(major, minor); |
| 9186 | } |
| 9187 | #endif /* HAVE_DEVICE_MACROS */ |
| 9188 | |
| 9189 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9190 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9191 | /*[clinic input] |
| 9192 | os.ftruncate |
| 9193 | |
| 9194 | fd: int |
| 9195 | length: Py_off_t |
| 9196 | / |
| 9197 | |
| 9198 | Truncate a file, specified by file descriptor, to a specific length. |
| 9199 | [clinic start generated code]*/ |
| 9200 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9201 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9202 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 9203 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9204 | { |
| 9205 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9206 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9207 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9208 | do { |
| 9209 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9210 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9211 | #ifdef MS_WINDOWS |
| 9212 | result = _chsize_s(fd, length); |
| 9213 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9214 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9215 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9216 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9217 | Py_END_ALLOW_THREADS |
| 9218 | } while (result != 0 && errno == EINTR && |
| 9219 | !(async_err = PyErr_CheckSignals())); |
| 9220 | if (result != 0) |
| 9221 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9222 | Py_RETURN_NONE; |
| 9223 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9224 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9225 | |
| 9226 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9227 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9228 | /*[clinic input] |
| 9229 | os.truncate |
| 9230 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 9231 | length: Py_off_t |
| 9232 | |
| 9233 | Truncate a file, specified by path, to a specific length. |
| 9234 | |
| 9235 | On some platforms, path may also be specified as an open file descriptor. |
| 9236 | If this functionality is unavailable, using it raises an exception. |
| 9237 | [clinic start generated code]*/ |
| 9238 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9239 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9240 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 9241 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9242 | { |
| 9243 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9244 | #ifdef MS_WINDOWS |
| 9245 | int fd; |
| 9246 | #endif |
| 9247 | |
| 9248 | if (path->fd != -1) |
| 9249 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9250 | |
| 9251 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9252 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9253 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 9254 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 9255 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9256 | result = -1; |
| 9257 | else { |
| 9258 | result = _chsize_s(fd, length); |
| 9259 | close(fd); |
| 9260 | if (result < 0) |
| 9261 | errno = result; |
| 9262 | } |
| 9263 | #else |
| 9264 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9265 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9266 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9267 | Py_END_ALLOW_THREADS |
| 9268 | if (result < 0) |
| 9269 | return path_error(path); |
| 9270 | |
| 9271 | Py_RETURN_NONE; |
| 9272 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9273 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9274 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9275 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9276 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 9277 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 9278 | defined, which is the case in Python on AIX. AIX bug report: |
| 9279 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 9280 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 9281 | # define POSIX_FADVISE_AIX_BUG |
| 9282 | #endif |
| 9283 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9284 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9285 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9286 | /*[clinic input] |
| 9287 | os.posix_fallocate |
| 9288 | |
| 9289 | fd: int |
| 9290 | offset: Py_off_t |
| 9291 | length: Py_off_t |
| 9292 | / |
| 9293 | |
| 9294 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 9295 | |
| 9296 | Ensure that the file specified by fd encompasses a range of bytes |
| 9297 | starting at offset bytes from the beginning and continuing for length bytes. |
| 9298 | [clinic start generated code]*/ |
| 9299 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9300 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9301 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9302 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9303 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9304 | { |
| 9305 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9306 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9307 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9308 | do { |
| 9309 | Py_BEGIN_ALLOW_THREADS |
| 9310 | result = posix_fallocate(fd, offset, length); |
| 9311 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9312 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9313 | |
| 9314 | if (result == 0) |
| 9315 | Py_RETURN_NONE; |
| 9316 | |
| 9317 | if (async_err) |
| 9318 | return NULL; |
| 9319 | |
| 9320 | errno = result; |
| 9321 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9322 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9323 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9324 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9325 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9326 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9327 | /*[clinic input] |
| 9328 | os.posix_fadvise |
| 9329 | |
| 9330 | fd: int |
| 9331 | offset: Py_off_t |
| 9332 | length: Py_off_t |
| 9333 | advice: int |
| 9334 | / |
| 9335 | |
| 9336 | Announce an intention to access data in a specific pattern. |
| 9337 | |
| 9338 | Announce an intention to access data in a specific pattern, thus allowing |
| 9339 | the kernel to make optimizations. |
| 9340 | The advice applies to the region of the file specified by fd starting at |
| 9341 | offset and continuing for length bytes. |
| 9342 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 9343 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 9344 | POSIX_FADV_DONTNEED. |
| 9345 | [clinic start generated code]*/ |
| 9346 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9347 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9348 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9349 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9350 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9351 | { |
| 9352 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9353 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9354 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9355 | do { |
| 9356 | Py_BEGIN_ALLOW_THREADS |
| 9357 | result = posix_fadvise(fd, offset, length, advice); |
| 9358 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9359 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9360 | |
| 9361 | if (result == 0) |
| 9362 | Py_RETURN_NONE; |
| 9363 | |
| 9364 | if (async_err) |
| 9365 | return NULL; |
| 9366 | |
| 9367 | errno = result; |
| 9368 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9369 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9370 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9371 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9372 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9373 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 9374 | /* Save putenv() parameters as values here, so we can collect them when they |
| 9375 | * get re-set with another call for the same key. */ |
| 9376 | static PyObject *posix_putenv_garbage; |
| 9377 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9378 | static void |
| 9379 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9380 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9381 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 9382 | * this will cause previous value to be collected. This has to |
| 9383 | * happen after the real putenv() call because the old value |
| 9384 | * was still accessible until then. */ |
| 9385 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 9386 | /* really not much we can do; just leak */ |
| 9387 | PyErr_Clear(); |
| 9388 | else |
| 9389 | Py_DECREF(value); |
| 9390 | } |
| 9391 | |
| 9392 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 9393 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9394 | /*[clinic input] |
| 9395 | os.putenv |
| 9396 | |
| 9397 | name: unicode |
| 9398 | value: unicode |
| 9399 | / |
| 9400 | |
| 9401 | Change or add an environment variable. |
| 9402 | [clinic start generated code]*/ |
| 9403 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9404 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9405 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 9406 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9407 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 9408 | const wchar_t *env; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9409 | Py_ssize_t size; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9410 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9411 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 9412 | defining hidden environment variables. */ |
| 9413 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 9414 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 9415 | { |
| 9416 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 9417 | return NULL; |
| 9418 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9419 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 9420 | if (unicode == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9421 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9422 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9423 | |
| 9424 | env = PyUnicode_AsUnicodeAndSize(unicode, &size); |
| 9425 | if (env == NULL) |
| 9426 | goto error; |
| 9427 | if (size > _MAX_ENV) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9428 | PyErr_Format(PyExc_ValueError, |
| 9429 | "the environment variable is longer than %u characters", |
| 9430 | _MAX_ENV); |
| 9431 | goto error; |
| 9432 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9433 | if (wcslen(env) != (size_t)size) { |
| 9434 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9435 | goto error; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 9436 | } |
| 9437 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9438 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9439 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9440 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9441 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9442 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9443 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9444 | Py_RETURN_NONE; |
| 9445 | |
| 9446 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9447 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9448 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9449 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9450 | #else /* MS_WINDOWS */ |
| 9451 | /*[clinic input] |
| 9452 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9453 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9454 | name: FSConverter |
| 9455 | value: FSConverter |
| 9456 | / |
| 9457 | |
| 9458 | Change or add an environment variable. |
| 9459 | [clinic start generated code]*/ |
| 9460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9461 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9462 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 9463 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9464 | { |
| 9465 | PyObject *bytes = NULL; |
| 9466 | char *env; |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9467 | const char *name_string = PyBytes_AS_STRING(name); |
| 9468 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9469 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 9470 | if (strchr(name_string, '=') != NULL) { |
| 9471 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 9472 | return NULL; |
| 9473 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9474 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 9475 | if (bytes == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9476 | return NULL; |
| 9477 | } |
| 9478 | |
| 9479 | env = PyBytes_AS_STRING(bytes); |
| 9480 | if (putenv(env)) { |
| 9481 | Py_DECREF(bytes); |
| 9482 | return posix_error(); |
| 9483 | } |
| 9484 | |
| 9485 | posix_putenv_garbage_setitem(name, bytes); |
| 9486 | Py_RETURN_NONE; |
| 9487 | } |
| 9488 | #endif /* MS_WINDOWS */ |
| 9489 | #endif /* HAVE_PUTENV */ |
| 9490 | |
| 9491 | |
| 9492 | #ifdef HAVE_UNSETENV |
| 9493 | /*[clinic input] |
| 9494 | os.unsetenv |
| 9495 | name: FSConverter |
| 9496 | / |
| 9497 | |
| 9498 | Delete an environment variable. |
| 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_unsetenv_impl(PyObject *module, PyObject *name) |
| 9503 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9504 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9505 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9506 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9507 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9508 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9509 | #ifdef HAVE_BROKEN_UNSETENV |
| 9510 | unsetenv(PyBytes_AS_STRING(name)); |
| 9511 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9512 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9513 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9514 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9515 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9516 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9517 | /* Remove the key from posix_putenv_garbage; |
| 9518 | * this will cause it to be collected. This has to |
| 9519 | * happen after the real unsetenv() call because the |
| 9520 | * old value was still accessible until then. |
| 9521 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9522 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9523 | /* really not much we can do; just leak */ |
| 9524 | PyErr_Clear(); |
| 9525 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9526 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9527 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9528 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9529 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9530 | |
| 9531 | /*[clinic input] |
| 9532 | os.strerror |
| 9533 | |
| 9534 | code: int |
| 9535 | / |
| 9536 | |
| 9537 | Translate an error code to a message string. |
| 9538 | [clinic start generated code]*/ |
| 9539 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9540 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9541 | os_strerror_impl(PyObject *module, int code) |
| 9542 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9543 | { |
| 9544 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9545 | if (message == NULL) { |
| 9546 | PyErr_SetString(PyExc_ValueError, |
| 9547 | "strerror() argument out of range"); |
| 9548 | return NULL; |
| 9549 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 9550 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9551 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9552 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9553 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9554 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9555 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9556 | /*[clinic input] |
| 9557 | os.WCOREDUMP -> bool |
| 9558 | |
| 9559 | status: int |
| 9560 | / |
| 9561 | |
| 9562 | Return True if the process returning status was dumped to a core file. |
| 9563 | [clinic start generated code]*/ |
| 9564 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9565 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9566 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 9567 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9568 | { |
| 9569 | WAIT_TYPE wait_status; |
| 9570 | WAIT_STATUS_INT(wait_status) = status; |
| 9571 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9572 | } |
| 9573 | #endif /* WCOREDUMP */ |
| 9574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9575 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9576 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9577 | /*[clinic input] |
| 9578 | os.WIFCONTINUED -> bool |
| 9579 | |
| 9580 | status: int |
| 9581 | |
| 9582 | Return True if a particular process was continued from a job control stop. |
| 9583 | |
| 9584 | Return True if the process returning status was continued from a |
| 9585 | job control stop. |
| 9586 | [clinic start generated code]*/ |
| 9587 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9588 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9589 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 9590 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9591 | { |
| 9592 | WAIT_TYPE wait_status; |
| 9593 | WAIT_STATUS_INT(wait_status) = status; |
| 9594 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9595 | } |
| 9596 | #endif /* WIFCONTINUED */ |
| 9597 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9598 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9599 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9600 | /*[clinic input] |
| 9601 | os.WIFSTOPPED -> bool |
| 9602 | |
| 9603 | status: int |
| 9604 | |
| 9605 | Return True if the process returning status was stopped. |
| 9606 | [clinic start generated code]*/ |
| 9607 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9608 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9609 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 9610 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9611 | { |
| 9612 | WAIT_TYPE wait_status; |
| 9613 | WAIT_STATUS_INT(wait_status) = status; |
| 9614 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9615 | } |
| 9616 | #endif /* WIFSTOPPED */ |
| 9617 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9618 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9619 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9620 | /*[clinic input] |
| 9621 | os.WIFSIGNALED -> bool |
| 9622 | |
| 9623 | status: int |
| 9624 | |
| 9625 | Return True if the process returning status was terminated by a signal. |
| 9626 | [clinic start generated code]*/ |
| 9627 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9628 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9629 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 9630 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9631 | { |
| 9632 | WAIT_TYPE wait_status; |
| 9633 | WAIT_STATUS_INT(wait_status) = status; |
| 9634 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9635 | } |
| 9636 | #endif /* WIFSIGNALED */ |
| 9637 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9638 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9639 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9640 | /*[clinic input] |
| 9641 | os.WIFEXITED -> bool |
| 9642 | |
| 9643 | status: int |
| 9644 | |
| 9645 | Return True if the process returning status exited via the exit() system call. |
| 9646 | [clinic start generated code]*/ |
| 9647 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9648 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9649 | os_WIFEXITED_impl(PyObject *module, int status) |
| 9650 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9651 | { |
| 9652 | WAIT_TYPE wait_status; |
| 9653 | WAIT_STATUS_INT(wait_status) = status; |
| 9654 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9655 | } |
| 9656 | #endif /* WIFEXITED */ |
| 9657 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9658 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9659 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9660 | /*[clinic input] |
| 9661 | os.WEXITSTATUS -> int |
| 9662 | |
| 9663 | status: int |
| 9664 | |
| 9665 | Return the process return code from status. |
| 9666 | [clinic start generated code]*/ |
| 9667 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9668 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9669 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 9670 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9671 | { |
| 9672 | WAIT_TYPE wait_status; |
| 9673 | WAIT_STATUS_INT(wait_status) = status; |
| 9674 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9675 | } |
| 9676 | #endif /* WEXITSTATUS */ |
| 9677 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9678 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9679 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9680 | /*[clinic input] |
| 9681 | os.WTERMSIG -> int |
| 9682 | |
| 9683 | status: int |
| 9684 | |
| 9685 | Return the signal that terminated the process that provided the status value. |
| 9686 | [clinic start generated code]*/ |
| 9687 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9688 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9689 | os_WTERMSIG_impl(PyObject *module, int status) |
| 9690 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9691 | { |
| 9692 | WAIT_TYPE wait_status; |
| 9693 | WAIT_STATUS_INT(wait_status) = status; |
| 9694 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9695 | } |
| 9696 | #endif /* WTERMSIG */ |
| 9697 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9698 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9699 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9700 | /*[clinic input] |
| 9701 | os.WSTOPSIG -> int |
| 9702 | |
| 9703 | status: int |
| 9704 | |
| 9705 | Return the signal that stopped the process that provided the status value. |
| 9706 | [clinic start generated code]*/ |
| 9707 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9708 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9709 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 9710 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9711 | { |
| 9712 | WAIT_TYPE wait_status; |
| 9713 | WAIT_STATUS_INT(wait_status) = status; |
| 9714 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9715 | } |
| 9716 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9717 | #endif /* HAVE_SYS_WAIT_H */ |
| 9718 | |
| 9719 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9720 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9721 | #ifdef _SCO_DS |
| 9722 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9723 | needed definitions in sys/statvfs.h */ |
| 9724 | #define _SVID3 |
| 9725 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9726 | #include <sys/statvfs.h> |
| 9727 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9728 | static PyObject* |
| 9729 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9730 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 9731 | if (v == NULL) |
| 9732 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9733 | |
| 9734 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9735 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9736 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9737 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9738 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9739 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9740 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9741 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9742 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9743 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9744 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9745 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9746 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9747 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9748 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9749 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9750 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9751 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9752 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9753 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9754 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9755 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9756 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9757 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9758 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9759 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9760 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9761 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9762 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 9763 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 9764 | * (issue #32390). */ |
| 9765 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 9766 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 9767 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 9768 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 9769 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9770 | if (PyErr_Occurred()) { |
| 9771 | Py_DECREF(v); |
| 9772 | return NULL; |
| 9773 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9774 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9775 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9776 | } |
| 9777 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9778 | |
| 9779 | /*[clinic input] |
| 9780 | os.fstatvfs |
| 9781 | fd: int |
| 9782 | / |
| 9783 | |
| 9784 | Perform an fstatvfs system call on the given fd. |
| 9785 | |
| 9786 | Equivalent to statvfs(fd). |
| 9787 | [clinic start generated code]*/ |
| 9788 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9789 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9790 | os_fstatvfs_impl(PyObject *module, int fd) |
| 9791 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9792 | { |
| 9793 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9794 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9795 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9796 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9797 | do { |
| 9798 | Py_BEGIN_ALLOW_THREADS |
| 9799 | result = fstatvfs(fd, &st); |
| 9800 | Py_END_ALLOW_THREADS |
| 9801 | } while (result != 0 && errno == EINTR && |
| 9802 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9803 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9804 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9805 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9806 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9807 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9808 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9809 | |
| 9810 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9811 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9812 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9813 | /*[clinic input] |
| 9814 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9815 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9816 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 9817 | |
| 9818 | Perform a statvfs system call on the given path. |
| 9819 | |
| 9820 | path may always be specified as a string. |
| 9821 | On some platforms, path may also be specified as an open file descriptor. |
| 9822 | If this functionality is unavailable, using it raises an exception. |
| 9823 | [clinic start generated code]*/ |
| 9824 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9825 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9826 | os_statvfs_impl(PyObject *module, path_t *path) |
| 9827 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9828 | { |
| 9829 | int result; |
| 9830 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9831 | |
| 9832 | Py_BEGIN_ALLOW_THREADS |
| 9833 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9834 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9835 | #ifdef __APPLE__ |
| 9836 | /* handle weak-linking on Mac OS X 10.3 */ |
| 9837 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9838 | fd_specified("statvfs", path->fd); |
| 9839 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9840 | } |
| 9841 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9842 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9843 | } |
| 9844 | else |
| 9845 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9846 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9847 | Py_END_ALLOW_THREADS |
| 9848 | |
| 9849 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9850 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9851 | } |
| 9852 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9853 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9854 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9855 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 9856 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9857 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9858 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9859 | /*[clinic input] |
| 9860 | os._getdiskusage |
| 9861 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 9862 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9863 | |
| 9864 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 9865 | [clinic start generated code]*/ |
| 9866 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9867 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 9868 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 9869 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9870 | { |
| 9871 | BOOL retval; |
| 9872 | ULARGE_INTEGER _, total, free; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9873 | |
| 9874 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 9875 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9876 | Py_END_ALLOW_THREADS |
| 9877 | if (retval == 0) |
| 9878 | return PyErr_SetFromWindowsErr(0); |
| 9879 | |
| 9880 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9881 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9882 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9883 | |
| 9884 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9885 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9886 | * It maps strings representing configuration variable names to |
| 9887 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9888 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9889 | * rarely-used constants. There are three separate tables that use |
| 9890 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9891 | * |
| 9892 | * This code is always included, even if none of the interfaces that |
| 9893 | * need it are included. The #if hackery needed to avoid it would be |
| 9894 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9895 | */ |
| 9896 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 9897 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 9898 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9899 | }; |
| 9900 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9901 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9902 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9903 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9904 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9905 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 9906 | int value = _PyLong_AsInt(arg); |
| 9907 | if (value == -1 && PyErr_Occurred()) |
| 9908 | return 0; |
| 9909 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9910 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9911 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9912 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9913 | /* look up the value in the table using a binary search */ |
| 9914 | size_t lo = 0; |
| 9915 | size_t mid; |
| 9916 | size_t hi = tablesize; |
| 9917 | int cmp; |
| 9918 | const char *confname; |
| 9919 | if (!PyUnicode_Check(arg)) { |
| 9920 | PyErr_SetString(PyExc_TypeError, |
| 9921 | "configuration names must be strings or integers"); |
| 9922 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9923 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 9924 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9925 | if (confname == NULL) |
| 9926 | return 0; |
| 9927 | while (lo < hi) { |
| 9928 | mid = (lo + hi) / 2; |
| 9929 | cmp = strcmp(confname, table[mid].name); |
| 9930 | if (cmp < 0) |
| 9931 | hi = mid; |
| 9932 | else if (cmp > 0) |
| 9933 | lo = mid + 1; |
| 9934 | else { |
| 9935 | *valuep = table[mid].value; |
| 9936 | return 1; |
| 9937 | } |
| 9938 | } |
| 9939 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9940 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9941 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9942 | } |
| 9943 | |
| 9944 | |
| 9945 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9946 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9947 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9948 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9949 | #endif |
| 9950 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9951 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9952 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9953 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9954 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9955 | #endif |
| 9956 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9957 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9958 | #endif |
| 9959 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9960 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9961 | #endif |
| 9962 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9963 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9964 | #endif |
| 9965 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9966 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9967 | #endif |
| 9968 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9969 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9970 | #endif |
| 9971 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9972 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9973 | #endif |
| 9974 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9975 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9976 | #endif |
| 9977 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9978 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9979 | #endif |
| 9980 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9981 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9982 | #endif |
| 9983 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9984 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9985 | #endif |
| 9986 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9987 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9988 | #endif |
| 9989 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9990 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9991 | #endif |
| 9992 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9993 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9994 | #endif |
| 9995 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9996 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9997 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9998 | #ifdef _PC_ACL_ENABLED |
| 9999 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10000 | #endif |
| 10001 | #ifdef _PC_MIN_HOLE_SIZE |
| 10002 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10003 | #endif |
| 10004 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10005 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10006 | #endif |
| 10007 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10008 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10009 | #endif |
| 10010 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10011 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10012 | #endif |
| 10013 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10014 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10015 | #endif |
| 10016 | #ifdef _PC_REC_XFER_ALIGN |
| 10017 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10018 | #endif |
| 10019 | #ifdef _PC_SYMLINK_MAX |
| 10020 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 10021 | #endif |
| 10022 | #ifdef _PC_XATTR_ENABLED |
| 10023 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 10024 | #endif |
| 10025 | #ifdef _PC_XATTR_EXISTS |
| 10026 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 10027 | #endif |
| 10028 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 10029 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 10030 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10031 | }; |
| 10032 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10033 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10034 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10035 | { |
| 10036 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 10037 | sizeof(posix_constants_pathconf) |
| 10038 | / sizeof(struct constdef)); |
| 10039 | } |
| 10040 | #endif |
| 10041 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10042 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10043 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10044 | /*[clinic input] |
| 10045 | os.fpathconf -> long |
| 10046 | |
| 10047 | fd: int |
| 10048 | name: path_confname |
| 10049 | / |
| 10050 | |
| 10051 | Return the configuration limit name for the file descriptor fd. |
| 10052 | |
| 10053 | If there is no limit, return -1. |
| 10054 | [clinic start generated code]*/ |
| 10055 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10056 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10057 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 10058 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10059 | { |
| 10060 | long limit; |
| 10061 | |
| 10062 | errno = 0; |
| 10063 | limit = fpathconf(fd, name); |
| 10064 | if (limit == -1 && errno != 0) |
| 10065 | posix_error(); |
| 10066 | |
| 10067 | return limit; |
| 10068 | } |
| 10069 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10070 | |
| 10071 | |
| 10072 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10073 | /*[clinic input] |
| 10074 | os.pathconf -> long |
| 10075 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 10076 | name: path_confname |
| 10077 | |
| 10078 | Return the configuration limit name for the file or directory path. |
| 10079 | |
| 10080 | If there is no limit, return -1. |
| 10081 | On some platforms, path may also be specified as an open file descriptor. |
| 10082 | If this functionality is unavailable, using it raises an exception. |
| 10083 | [clinic start generated code]*/ |
| 10084 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10085 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10086 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 10087 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10088 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10089 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10090 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10091 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10092 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10093 | if (path->fd != -1) |
| 10094 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10095 | else |
| 10096 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10097 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10098 | if (limit == -1 && errno != 0) { |
| 10099 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 10100 | /* could be a path or name problem */ |
| 10101 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10102 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10103 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10104 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10105 | |
| 10106 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10107 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10108 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10109 | |
| 10110 | #ifdef HAVE_CONFSTR |
| 10111 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10112 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10113 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10114 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10115 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10116 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10117 | #endif |
| 10118 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10119 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10120 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10121 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10122 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10123 | #endif |
| 10124 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10125 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10126 | #endif |
| 10127 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10128 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10129 | #endif |
| 10130 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10131 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10132 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10133 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10134 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10135 | #endif |
| 10136 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10137 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10138 | #endif |
| 10139 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10140 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10141 | #endif |
| 10142 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10143 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10144 | #endif |
| 10145 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10146 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10147 | #endif |
| 10148 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10149 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10150 | #endif |
| 10151 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10152 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10153 | #endif |
| 10154 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10155 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10156 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10157 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10158 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10159 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10160 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10161 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10162 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10163 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10164 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10165 | #endif |
| 10166 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10167 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10168 | #endif |
| 10169 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10170 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10171 | #endif |
| 10172 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10173 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10174 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10175 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10176 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10177 | #endif |
| 10178 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10179 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10180 | #endif |
| 10181 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10182 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10183 | #endif |
| 10184 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10185 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10186 | #endif |
| 10187 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10188 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10189 | #endif |
| 10190 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10191 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10192 | #endif |
| 10193 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10194 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10195 | #endif |
| 10196 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10197 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10198 | #endif |
| 10199 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10200 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10201 | #endif |
| 10202 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10203 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10204 | #endif |
| 10205 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10206 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10207 | #endif |
| 10208 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10209 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10210 | #endif |
| 10211 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10212 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10213 | #endif |
| 10214 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10215 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10216 | #endif |
| 10217 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10218 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10219 | #endif |
| 10220 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10221 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10222 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10223 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10224 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10225 | #endif |
| 10226 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10227 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10228 | #endif |
| 10229 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10230 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10231 | #endif |
| 10232 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10233 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10234 | #endif |
| 10235 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10236 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10237 | #endif |
| 10238 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10239 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10240 | #endif |
| 10241 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10242 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10243 | #endif |
| 10244 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10245 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10246 | #endif |
| 10247 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10248 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10249 | #endif |
| 10250 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10251 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10252 | #endif |
| 10253 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10254 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10255 | #endif |
| 10256 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10257 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10258 | #endif |
| 10259 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10260 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10261 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10262 | }; |
| 10263 | |
| 10264 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10265 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10266 | { |
| 10267 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 10268 | sizeof(posix_constants_confstr) |
| 10269 | / sizeof(struct constdef)); |
| 10270 | } |
| 10271 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10272 | |
| 10273 | /*[clinic input] |
| 10274 | os.confstr |
| 10275 | |
| 10276 | name: confstr_confname |
| 10277 | / |
| 10278 | |
| 10279 | Return a string-valued system configuration variable. |
| 10280 | [clinic start generated code]*/ |
| 10281 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10282 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10283 | os_confstr_impl(PyObject *module, int name) |
| 10284 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10285 | { |
| 10286 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10287 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10288 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10289 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10290 | errno = 0; |
| 10291 | len = confstr(name, buffer, sizeof(buffer)); |
| 10292 | if (len == 0) { |
| 10293 | if (errno) { |
| 10294 | posix_error(); |
| 10295 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10296 | } |
| 10297 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10298 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10299 | } |
| 10300 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10301 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10302 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 10303 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10304 | char *buf = PyMem_Malloc(len); |
| 10305 | if (buf == NULL) |
| 10306 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 10307 | len2 = confstr(name, buf, len); |
| 10308 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 10309 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10310 | PyMem_Free(buf); |
| 10311 | } |
| 10312 | else |
| 10313 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10314 | return result; |
| 10315 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10316 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10317 | |
| 10318 | |
| 10319 | #ifdef HAVE_SYSCONF |
| 10320 | static struct constdef posix_constants_sysconf[] = { |
| 10321 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10322 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10323 | #endif |
| 10324 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10325 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10326 | #endif |
| 10327 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10328 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10329 | #endif |
| 10330 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10331 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10332 | #endif |
| 10333 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10334 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10335 | #endif |
| 10336 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10337 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10338 | #endif |
| 10339 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10340 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10341 | #endif |
| 10342 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10343 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10344 | #endif |
| 10345 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10346 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10347 | #endif |
| 10348 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10349 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10350 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10351 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10352 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10353 | #endif |
| 10354 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10355 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10356 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10357 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10358 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10359 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10360 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10361 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10362 | #endif |
| 10363 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10364 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10365 | #endif |
| 10366 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10367 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10368 | #endif |
| 10369 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10370 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10371 | #endif |
| 10372 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10373 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10374 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10375 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10376 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10377 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10378 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10379 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10380 | #endif |
| 10381 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10382 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10383 | #endif |
| 10384 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10385 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10386 | #endif |
| 10387 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10388 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10389 | #endif |
| 10390 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10391 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10392 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10393 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10394 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10395 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10396 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10397 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10398 | #endif |
| 10399 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10400 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10401 | #endif |
| 10402 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10403 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10404 | #endif |
| 10405 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10406 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10407 | #endif |
| 10408 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10409 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10410 | #endif |
| 10411 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10412 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10413 | #endif |
| 10414 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10415 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10416 | #endif |
| 10417 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10418 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10419 | #endif |
| 10420 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10421 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10422 | #endif |
| 10423 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10424 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10425 | #endif |
| 10426 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10427 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10428 | #endif |
| 10429 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10430 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10431 | #endif |
| 10432 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10433 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10434 | #endif |
| 10435 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10436 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10437 | #endif |
| 10438 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10439 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10440 | #endif |
| 10441 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10442 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10443 | #endif |
| 10444 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10445 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10446 | #endif |
| 10447 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10448 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10449 | #endif |
| 10450 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10451 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10452 | #endif |
| 10453 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10454 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10455 | #endif |
| 10456 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10457 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10458 | #endif |
| 10459 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10460 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10461 | #endif |
| 10462 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10463 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10464 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10465 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10466 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10467 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10468 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10469 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10470 | #endif |
| 10471 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10472 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10473 | #endif |
| 10474 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10475 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10476 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10477 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10478 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10479 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10480 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10481 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10482 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10483 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10484 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10485 | #endif |
| 10486 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10487 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10488 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10489 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10490 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10491 | #endif |
| 10492 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10493 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10494 | #endif |
| 10495 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10496 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10497 | #endif |
| 10498 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10499 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10500 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10501 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10502 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10503 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10504 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10505 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10506 | #endif |
| 10507 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10508 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10509 | #endif |
| 10510 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10511 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10512 | #endif |
| 10513 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10514 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10515 | #endif |
| 10516 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10517 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10518 | #endif |
| 10519 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10520 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10521 | #endif |
| 10522 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10523 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10524 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10525 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10526 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10527 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10528 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10529 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10530 | #endif |
| 10531 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10532 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10533 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10534 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10535 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10536 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10537 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10538 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10539 | #endif |
| 10540 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10541 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10542 | #endif |
| 10543 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10544 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10545 | #endif |
| 10546 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10547 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10548 | #endif |
| 10549 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10550 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10551 | #endif |
| 10552 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10553 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10554 | #endif |
| 10555 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10556 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10557 | #endif |
| 10558 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10559 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10560 | #endif |
| 10561 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10562 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10563 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10564 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10565 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10566 | #endif |
| 10567 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10568 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10569 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10570 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10571 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10572 | #endif |
| 10573 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10574 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10575 | #endif |
| 10576 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10577 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10578 | #endif |
| 10579 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10580 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10581 | #endif |
| 10582 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10583 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10584 | #endif |
| 10585 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10586 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10587 | #endif |
| 10588 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10589 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10590 | #endif |
| 10591 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10592 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10593 | #endif |
| 10594 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10595 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10596 | #endif |
| 10597 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10598 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10599 | #endif |
| 10600 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10601 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10602 | #endif |
| 10603 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10604 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10605 | #endif |
| 10606 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10607 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10608 | #endif |
| 10609 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10610 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10611 | #endif |
| 10612 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10613 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10614 | #endif |
| 10615 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10616 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10617 | #endif |
| 10618 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10619 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10620 | #endif |
| 10621 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10622 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10623 | #endif |
| 10624 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10625 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10626 | #endif |
| 10627 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10628 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10629 | #endif |
| 10630 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10631 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10632 | #endif |
| 10633 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10634 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10635 | #endif |
| 10636 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10637 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10638 | #endif |
| 10639 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10640 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10641 | #endif |
| 10642 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10643 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10644 | #endif |
| 10645 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10646 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10647 | #endif |
| 10648 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10649 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10650 | #endif |
| 10651 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10652 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10653 | #endif |
| 10654 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10655 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10656 | #endif |
| 10657 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10658 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10659 | #endif |
| 10660 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10661 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10662 | #endif |
| 10663 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10664 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10665 | #endif |
| 10666 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10667 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10668 | #endif |
| 10669 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10670 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10671 | #endif |
| 10672 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10673 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10674 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10675 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10676 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10677 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10678 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10679 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10680 | #endif |
| 10681 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10682 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10683 | #endif |
| 10684 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10685 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10686 | #endif |
| 10687 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10688 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10689 | #endif |
| 10690 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10691 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10692 | #endif |
| 10693 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10694 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10695 | #endif |
| 10696 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10697 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10698 | #endif |
| 10699 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10700 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10701 | #endif |
| 10702 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10703 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10704 | #endif |
| 10705 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10706 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10707 | #endif |
| 10708 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10709 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10710 | #endif |
| 10711 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10712 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10713 | #endif |
| 10714 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10715 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10716 | #endif |
| 10717 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10718 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10719 | #endif |
| 10720 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10721 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10722 | #endif |
| 10723 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10724 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10725 | #endif |
| 10726 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10727 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10728 | #endif |
| 10729 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10730 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10731 | #endif |
| 10732 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10733 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10734 | #endif |
| 10735 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10736 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10737 | #endif |
| 10738 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10739 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10740 | #endif |
| 10741 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10742 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10743 | #endif |
| 10744 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10745 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10746 | #endif |
| 10747 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10748 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10749 | #endif |
| 10750 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10751 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10752 | #endif |
| 10753 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10754 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10755 | #endif |
| 10756 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10757 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10758 | #endif |
| 10759 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10760 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10761 | #endif |
| 10762 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10763 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10764 | #endif |
| 10765 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10766 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10767 | #endif |
| 10768 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10769 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10770 | #endif |
| 10771 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10772 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10773 | #endif |
| 10774 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10775 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10776 | #endif |
| 10777 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10778 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10779 | #endif |
| 10780 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10781 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10782 | #endif |
| 10783 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10784 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10785 | #endif |
| 10786 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10787 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10788 | #endif |
| 10789 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10790 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10791 | #endif |
| 10792 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10793 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10794 | #endif |
| 10795 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10796 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10797 | #endif |
| 10798 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10799 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10800 | #endif |
| 10801 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10802 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10803 | #endif |
| 10804 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10805 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10806 | #endif |
| 10807 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10808 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10809 | #endif |
| 10810 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10811 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10812 | #endif |
| 10813 | }; |
| 10814 | |
| 10815 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10816 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10817 | { |
| 10818 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 10819 | sizeof(posix_constants_sysconf) |
| 10820 | / sizeof(struct constdef)); |
| 10821 | } |
| 10822 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10823 | |
| 10824 | /*[clinic input] |
| 10825 | os.sysconf -> long |
| 10826 | name: sysconf_confname |
| 10827 | / |
| 10828 | |
| 10829 | Return an integer-valued system configuration variable. |
| 10830 | [clinic start generated code]*/ |
| 10831 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10832 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10833 | os_sysconf_impl(PyObject *module, int name) |
| 10834 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10835 | { |
| 10836 | long value; |
| 10837 | |
| 10838 | errno = 0; |
| 10839 | value = sysconf(name); |
| 10840 | if (value == -1 && errno != 0) |
| 10841 | posix_error(); |
| 10842 | return value; |
| 10843 | } |
| 10844 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10845 | |
| 10846 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10847 | /* 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] | 10848 | * 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] | 10849 | * the exported dictionaries that are used to publish information about the |
| 10850 | * names available on the host platform. |
| 10851 | * |
| 10852 | * Sorting the table at runtime ensures that the table is properly ordered |
| 10853 | * when used, even for platforms we're not able to test on. It also makes |
| 10854 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10855 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10856 | |
| 10857 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10858 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10859 | { |
| 10860 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10861 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10862 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10863 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10864 | |
| 10865 | return strcmp(c1->name, c2->name); |
| 10866 | } |
| 10867 | |
| 10868 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10869 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 10870 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10871 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10872 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10873 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10874 | |
| 10875 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10876 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10877 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10878 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10879 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10880 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10881 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10882 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10883 | Py_XDECREF(o); |
| 10884 | Py_DECREF(d); |
| 10885 | return -1; |
| 10886 | } |
| 10887 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10888 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10889 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10890 | } |
| 10891 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10892 | /* Return -1 on failure, 0 on success. */ |
| 10893 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10894 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10895 | { |
| 10896 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10897 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10898 | sizeof(posix_constants_pathconf) |
| 10899 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10900 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10901 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10902 | #endif |
| 10903 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10904 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10905 | sizeof(posix_constants_confstr) |
| 10906 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10907 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10908 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10909 | #endif |
| 10910 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10911 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10912 | sizeof(posix_constants_sysconf) |
| 10913 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10914 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10915 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10916 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10917 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10918 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10919 | |
| 10920 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10921 | /*[clinic input] |
| 10922 | os.abort |
| 10923 | |
| 10924 | Abort the interpreter immediately. |
| 10925 | |
| 10926 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 10927 | on the hosting operating system. This function never returns. |
| 10928 | [clinic start generated code]*/ |
| 10929 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10930 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10931 | os_abort_impl(PyObject *module) |
| 10932 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10933 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10934 | abort(); |
| 10935 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 10936 | #ifndef __clang__ |
| 10937 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 10938 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 10939 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10940 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10941 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 10942 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10943 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10944 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10945 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10946 | /* Grab ShellExecute dynamically from shell32 */ |
| 10947 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10948 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 10949 | LPCWSTR, INT); |
| 10950 | static int |
| 10951 | check_ShellExecute() |
| 10952 | { |
| 10953 | HINSTANCE hShell32; |
| 10954 | |
| 10955 | /* only recheck */ |
| 10956 | if (-1 == has_ShellExecute) { |
| 10957 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 10958 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 10959 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 10960 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 10961 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10962 | hShell32 = LoadLibraryW(L"SHELL32"); |
| 10963 | Py_END_ALLOW_THREADS |
| 10964 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10965 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 10966 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10967 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10968 | } else { |
| 10969 | has_ShellExecute = 0; |
| 10970 | } |
| 10971 | } |
| 10972 | return has_ShellExecute; |
| 10973 | } |
| 10974 | |
| 10975 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10976 | /*[clinic input] |
| 10977 | os.startfile |
| 10978 | filepath: path_t |
| 10979 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10980 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10981 | startfile(filepath [, operation]) |
| 10982 | |
| 10983 | Start a file with its associated application. |
| 10984 | |
| 10985 | When "operation" is not specified or "open", this acts like |
| 10986 | double-clicking the file in Explorer, or giving the file name as an |
| 10987 | argument to the DOS "start" command: the file is opened with whatever |
| 10988 | application (if any) its extension is associated. |
| 10989 | When another "operation" is given, it specifies what should be done with |
| 10990 | the file. A typical operation is "print". |
| 10991 | |
| 10992 | startfile returns as soon as the associated application is launched. |
| 10993 | There is no option to wait for the application to close, and no way |
| 10994 | to retrieve the application's exit status. |
| 10995 | |
| 10996 | The filepath is relative to the current directory. If you want to use |
| 10997 | an absolute path, make sure the first character is not a slash ("/"); |
| 10998 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 10999 | [clinic start generated code]*/ |
| 11000 | |
| 11001 | static PyObject * |
| 11002 | os_startfile_impl(PyObject *module, path_t *filepath, Py_UNICODE *operation) |
| 11003 | /*[clinic end generated code: output=912ceba79acfa1c9 input=63950bf2986380d0]*/ |
| 11004 | { |
| 11005 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11006 | |
| 11007 | if(!check_ShellExecute()) { |
| 11008 | /* If the OS doesn't have ShellExecute, return a |
| 11009 | NotImplementedError. */ |
| 11010 | return PyErr_Format(PyExc_NotImplementedError, |
| 11011 | "startfile not available on this platform"); |
| 11012 | } |
| 11013 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11014 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11015 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11016 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11017 | Py_END_ALLOW_THREADS |
| 11018 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11019 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11020 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11021 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11022 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11023 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11024 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11025 | #endif /* MS_WINDOWS */ |
| 11026 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11027 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11028 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11029 | /*[clinic input] |
| 11030 | os.getloadavg |
| 11031 | |
| 11032 | Return average recent system load information. |
| 11033 | |
| 11034 | Return the number of processes in the system run queue averaged over |
| 11035 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 11036 | Raises OSError if the load average was unobtainable. |
| 11037 | [clinic start generated code]*/ |
| 11038 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11039 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11040 | os_getloadavg_impl(PyObject *module) |
| 11041 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11042 | { |
| 11043 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11044 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11045 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 11046 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11047 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11048 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11049 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11050 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11051 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11052 | |
| 11053 | /*[clinic input] |
| 11054 | os.device_encoding |
| 11055 | fd: int |
| 11056 | |
| 11057 | Return a string describing the encoding of a terminal's file descriptor. |
| 11058 | |
| 11059 | The file descriptor must be attached to a terminal. |
| 11060 | If the device is not a terminal, return None. |
| 11061 | [clinic start generated code]*/ |
| 11062 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11063 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11064 | os_device_encoding_impl(PyObject *module, int fd) |
| 11065 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11066 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 11067 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 11068 | } |
| 11069 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11070 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11071 | #ifdef HAVE_SETRESUID |
| 11072 | /*[clinic input] |
| 11073 | os.setresuid |
| 11074 | |
| 11075 | ruid: uid_t |
| 11076 | euid: uid_t |
| 11077 | suid: uid_t |
| 11078 | / |
| 11079 | |
| 11080 | Set the current process's real, effective, and saved user ids. |
| 11081 | [clinic start generated code]*/ |
| 11082 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11083 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11084 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 11085 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11086 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11087 | if (setresuid(ruid, euid, suid) < 0) |
| 11088 | return posix_error(); |
| 11089 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11090 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11091 | #endif /* HAVE_SETRESUID */ |
| 11092 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11093 | |
| 11094 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11095 | /*[clinic input] |
| 11096 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11097 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11098 | rgid: gid_t |
| 11099 | egid: gid_t |
| 11100 | sgid: gid_t |
| 11101 | / |
| 11102 | |
| 11103 | Set the current process's real, effective, and saved group ids. |
| 11104 | [clinic start generated code]*/ |
| 11105 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11106 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11107 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 11108 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11109 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11110 | if (setresgid(rgid, egid, sgid) < 0) |
| 11111 | return posix_error(); |
| 11112 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11113 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11114 | #endif /* HAVE_SETRESGID */ |
| 11115 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11116 | |
| 11117 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11118 | /*[clinic input] |
| 11119 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11120 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11121 | Return a tuple of the current process's real, effective, and saved user ids. |
| 11122 | [clinic start generated code]*/ |
| 11123 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11124 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11125 | os_getresuid_impl(PyObject *module) |
| 11126 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11127 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11128 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11129 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 11130 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11131 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 11132 | _PyLong_FromUid(euid), |
| 11133 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11134 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11135 | #endif /* HAVE_GETRESUID */ |
| 11136 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11137 | |
| 11138 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11139 | /*[clinic input] |
| 11140 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11141 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11142 | Return a tuple of the current process's real, effective, and saved group ids. |
| 11143 | [clinic start generated code]*/ |
| 11144 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11145 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11146 | os_getresgid_impl(PyObject *module) |
| 11147 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11148 | { |
| 11149 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11150 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 11151 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11152 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 11153 | _PyLong_FromGid(egid), |
| 11154 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11155 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11156 | #endif /* HAVE_GETRESGID */ |
| 11157 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11158 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11159 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11160 | /*[clinic input] |
| 11161 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11162 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11163 | path: path_t(allow_fd=True) |
| 11164 | attribute: path_t |
| 11165 | * |
| 11166 | follow_symlinks: bool = True |
| 11167 | |
| 11168 | Return the value of extended attribute attribute on path. |
| 11169 | |
| 11170 | path may be either a string or an open file descriptor. |
| 11171 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11172 | link, getxattr will examine the symbolic link itself instead of the file |
| 11173 | the link points to. |
| 11174 | |
| 11175 | [clinic start generated code]*/ |
| 11176 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11177 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11178 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11179 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11180 | /*[clinic end generated code: output=5f2f44200a43cff2 input=8c8ea3bab78d89c2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11181 | { |
| 11182 | Py_ssize_t i; |
| 11183 | PyObject *buffer = NULL; |
| 11184 | |
| 11185 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 11186 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11187 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11188 | for (i = 0; ; i++) { |
| 11189 | void *ptr; |
| 11190 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11191 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11192 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11193 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11194 | path_error(path); |
| 11195 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11196 | } |
| 11197 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 11198 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11199 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11200 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11201 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11202 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11203 | if (path->fd >= 0) |
| 11204 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11205 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11206 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11207 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11208 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11209 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11210 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11211 | if (result < 0) { |
| 11212 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11213 | if (errno == ERANGE) |
| 11214 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11215 | path_error(path); |
| 11216 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11217 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11218 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11219 | if (result != buffer_size) { |
| 11220 | /* Can only shrink. */ |
| 11221 | _PyBytes_Resize(&buffer, result); |
| 11222 | } |
| 11223 | break; |
| 11224 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11225 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11226 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11227 | } |
| 11228 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11229 | |
| 11230 | /*[clinic input] |
| 11231 | os.setxattr |
| 11232 | |
| 11233 | path: path_t(allow_fd=True) |
| 11234 | attribute: path_t |
| 11235 | value: Py_buffer |
| 11236 | flags: int = 0 |
| 11237 | * |
| 11238 | follow_symlinks: bool = True |
| 11239 | |
| 11240 | Set extended attribute attribute on path to value. |
| 11241 | |
| 11242 | path may be either a string or an open file descriptor. |
| 11243 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11244 | link, setxattr will modify the symbolic link itself instead of the file |
| 11245 | the link points to. |
| 11246 | |
| 11247 | [clinic start generated code]*/ |
| 11248 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11249 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11250 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11251 | Py_buffer *value, int flags, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11252 | /*[clinic end generated code: output=98b83f63fdde26bb input=f0d26833992015c2]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11253 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11254 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11255 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11256 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11257 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11258 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11259 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11260 | if (path->fd > -1) |
| 11261 | result = fsetxattr(path->fd, attribute->narrow, |
| 11262 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11263 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11264 | result = setxattr(path->narrow, attribute->narrow, |
| 11265 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11266 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11267 | result = lsetxattr(path->narrow, attribute->narrow, |
| 11268 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11269 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11270 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11271 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11272 | path_error(path); |
| 11273 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11274 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11275 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11276 | Py_RETURN_NONE; |
| 11277 | } |
| 11278 | |
| 11279 | |
| 11280 | /*[clinic input] |
| 11281 | os.removexattr |
| 11282 | |
| 11283 | path: path_t(allow_fd=True) |
| 11284 | attribute: path_t |
| 11285 | * |
| 11286 | follow_symlinks: bool = True |
| 11287 | |
| 11288 | Remove extended attribute attribute on path. |
| 11289 | |
| 11290 | path may be either a string or an open file descriptor. |
| 11291 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11292 | link, removexattr will modify the symbolic link itself instead of the file |
| 11293 | the link points to. |
| 11294 | |
| 11295 | [clinic start generated code]*/ |
| 11296 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11297 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11298 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11299 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11300 | /*[clinic end generated code: output=521a51817980cda6 input=cdb54834161e3329]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11301 | { |
| 11302 | ssize_t result; |
| 11303 | |
| 11304 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 11305 | return NULL; |
| 11306 | |
| 11307 | Py_BEGIN_ALLOW_THREADS; |
| 11308 | if (path->fd > -1) |
| 11309 | result = fremovexattr(path->fd, attribute->narrow); |
| 11310 | else if (follow_symlinks) |
| 11311 | result = removexattr(path->narrow, attribute->narrow); |
| 11312 | else |
| 11313 | result = lremovexattr(path->narrow, attribute->narrow); |
| 11314 | Py_END_ALLOW_THREADS; |
| 11315 | |
| 11316 | if (result) { |
| 11317 | return path_error(path); |
| 11318 | } |
| 11319 | |
| 11320 | Py_RETURN_NONE; |
| 11321 | } |
| 11322 | |
| 11323 | |
| 11324 | /*[clinic input] |
| 11325 | os.listxattr |
| 11326 | |
| 11327 | path: path_t(allow_fd=True, nullable=True) = None |
| 11328 | * |
| 11329 | follow_symlinks: bool = True |
| 11330 | |
| 11331 | Return a list of extended attributes on path. |
| 11332 | |
| 11333 | path may be either None, a string, or an open file descriptor. |
| 11334 | if path is None, listxattr will examine the current directory. |
| 11335 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11336 | link, listxattr will examine the symbolic link itself instead of the file |
| 11337 | the link points to. |
| 11338 | [clinic start generated code]*/ |
| 11339 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11340 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11341 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
| 11342 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=08cca53ac0b07c13]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11343 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11344 | Py_ssize_t i; |
| 11345 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11346 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11347 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11348 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11349 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11350 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11351 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11352 | name = path->narrow ? path->narrow : "."; |
| 11353 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11354 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11355 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11356 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11357 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11358 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11359 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 11360 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11361 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11362 | break; |
| 11363 | } |
| 11364 | buffer = PyMem_MALLOC(buffer_size); |
| 11365 | if (!buffer) { |
| 11366 | PyErr_NoMemory(); |
| 11367 | break; |
| 11368 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11369 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11370 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11371 | if (path->fd > -1) |
| 11372 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11373 | else if (follow_symlinks) |
| 11374 | length = listxattr(name, buffer, buffer_size); |
| 11375 | else |
| 11376 | length = llistxattr(name, buffer, buffer_size); |
| 11377 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11378 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11379 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11380 | if (errno == ERANGE) { |
| 11381 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 11382 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11383 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11384 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11385 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11386 | break; |
| 11387 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11388 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11389 | result = PyList_New(0); |
| 11390 | if (!result) { |
| 11391 | goto exit; |
| 11392 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11393 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11394 | end = buffer + length; |
| 11395 | for (trace = start = buffer; trace != end; trace++) { |
| 11396 | if (!*trace) { |
| 11397 | int error; |
| 11398 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 11399 | trace - start); |
| 11400 | if (!attribute) { |
| 11401 | Py_DECREF(result); |
| 11402 | result = NULL; |
| 11403 | goto exit; |
| 11404 | } |
| 11405 | error = PyList_Append(result, attribute); |
| 11406 | Py_DECREF(attribute); |
| 11407 | if (error) { |
| 11408 | Py_DECREF(result); |
| 11409 | result = NULL; |
| 11410 | goto exit; |
| 11411 | } |
| 11412 | start = trace + 1; |
| 11413 | } |
| 11414 | } |
| 11415 | break; |
| 11416 | } |
| 11417 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11418 | if (buffer) |
| 11419 | PyMem_FREE(buffer); |
| 11420 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11421 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11422 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11423 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11424 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11425 | /*[clinic input] |
| 11426 | os.urandom |
| 11427 | |
| 11428 | size: Py_ssize_t |
| 11429 | / |
| 11430 | |
| 11431 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 11432 | [clinic start generated code]*/ |
| 11433 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11434 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11435 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 11436 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11437 | { |
| 11438 | PyObject *bytes; |
| 11439 | int result; |
| 11440 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11441 | if (size < 0) |
| 11442 | return PyErr_Format(PyExc_ValueError, |
| 11443 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11444 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 11445 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11446 | return NULL; |
| 11447 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 11448 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11449 | if (result == -1) { |
| 11450 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11451 | return NULL; |
| 11452 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11453 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11454 | } |
| 11455 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11456 | /* Terminal size querying */ |
| 11457 | |
| 11458 | static PyTypeObject TerminalSizeType; |
| 11459 | |
| 11460 | PyDoc_STRVAR(TerminalSize_docstring, |
| 11461 | "A tuple of (columns, lines) for holding terminal window size"); |
| 11462 | |
| 11463 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 11464 | {"columns", "width of the terminal window in characters"}, |
| 11465 | {"lines", "height of the terminal window in characters"}, |
| 11466 | {NULL, NULL} |
| 11467 | }; |
| 11468 | |
| 11469 | static PyStructSequence_Desc TerminalSize_desc = { |
| 11470 | "os.terminal_size", |
| 11471 | TerminalSize_docstring, |
| 11472 | TerminalSize_fields, |
| 11473 | 2, |
| 11474 | }; |
| 11475 | |
| 11476 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11477 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11478 | PyDoc_STRVAR(termsize__doc__, |
| 11479 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 11480 | "\n" \ |
| 11481 | "The optional argument fd (default standard output) specifies\n" \ |
| 11482 | "which file descriptor should be queried.\n" \ |
| 11483 | "\n" \ |
| 11484 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 11485 | "is thrown.\n" \ |
| 11486 | "\n" \ |
| 11487 | "This function will only be defined if an implementation is\n" \ |
| 11488 | "available for this system.\n" \ |
| 11489 | "\n" \ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 11490 | "shutil.get_terminal_size is the high-level function which should\n" \ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11491 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 11492 | |
| 11493 | static PyObject* |
| 11494 | get_terminal_size(PyObject *self, PyObject *args) |
| 11495 | { |
| 11496 | int columns, lines; |
| 11497 | PyObject *termsize; |
| 11498 | |
| 11499 | int fd = fileno(stdout); |
| 11500 | /* Under some conditions stdout may not be connected and |
| 11501 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 11502 | * GUI apps don't have valid standard streams by default. |
| 11503 | * |
| 11504 | * If this happens, and the optional fd argument is not present, |
| 11505 | * the ioctl below will fail returning EBADF. This is what we want. |
| 11506 | */ |
| 11507 | |
| 11508 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 11509 | return NULL; |
| 11510 | |
| 11511 | #ifdef TERMSIZE_USE_IOCTL |
| 11512 | { |
| 11513 | struct winsize w; |
| 11514 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 11515 | return PyErr_SetFromErrno(PyExc_OSError); |
| 11516 | columns = w.ws_col; |
| 11517 | lines = w.ws_row; |
| 11518 | } |
| 11519 | #endif /* TERMSIZE_USE_IOCTL */ |
| 11520 | |
| 11521 | #ifdef TERMSIZE_USE_CONIO |
| 11522 | { |
| 11523 | DWORD nhandle; |
| 11524 | HANDLE handle; |
| 11525 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 11526 | switch (fd) { |
| 11527 | case 0: nhandle = STD_INPUT_HANDLE; |
| 11528 | break; |
| 11529 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 11530 | break; |
| 11531 | case 2: nhandle = STD_ERROR_HANDLE; |
| 11532 | break; |
| 11533 | default: |
| 11534 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 11535 | } |
| 11536 | handle = GetStdHandle(nhandle); |
| 11537 | if (handle == NULL) |
| 11538 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 11539 | if (handle == INVALID_HANDLE_VALUE) |
| 11540 | return PyErr_SetFromWindowsErr(0); |
| 11541 | |
| 11542 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 11543 | return PyErr_SetFromWindowsErr(0); |
| 11544 | |
| 11545 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 11546 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 11547 | } |
| 11548 | #endif /* TERMSIZE_USE_CONIO */ |
| 11549 | |
| 11550 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 11551 | if (termsize == NULL) |
| 11552 | return NULL; |
| 11553 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 11554 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 11555 | if (PyErr_Occurred()) { |
| 11556 | Py_DECREF(termsize); |
| 11557 | return NULL; |
| 11558 | } |
| 11559 | return termsize; |
| 11560 | } |
| 11561 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 11562 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11563 | |
| 11564 | /*[clinic input] |
| 11565 | os.cpu_count |
| 11566 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 11567 | Return the number of CPUs in the system; return None if indeterminable. |
| 11568 | |
| 11569 | This number is not equivalent to the number of CPUs the current process can |
| 11570 | use. The number of usable CPUs can be obtained with |
| 11571 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11572 | [clinic start generated code]*/ |
| 11573 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11574 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11575 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 11576 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11577 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11578 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11579 | #ifdef MS_WINDOWS |
Christopher Wilcox | c67bae0 | 2017-08-30 05:01:08 -0400 | [diff] [blame] | 11580 | /* Vista is supported and the GetMaximumProcessorCount API is Win7+ |
| 11581 | Need to fallback to Vista behavior if this call isn't present */ |
| 11582 | HINSTANCE hKernel32; |
| 11583 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 11584 | |
| 11585 | static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL; |
| 11586 | *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32, |
| 11587 | "GetMaximumProcessorCount"); |
| 11588 | if (_GetMaximumProcessorCount != NULL) { |
| 11589 | ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS); |
| 11590 | } |
| 11591 | else { |
| 11592 | SYSTEM_INFO sysinfo; |
| 11593 | GetSystemInfo(&sysinfo); |
| 11594 | ncpu = sysinfo.dwNumberOfProcessors; |
| 11595 | } |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11596 | #elif defined(__hpux) |
| 11597 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 11598 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 11599 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11600 | #elif defined(__DragonFly__) || \ |
| 11601 | defined(__OpenBSD__) || \ |
| 11602 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11603 | defined(__NetBSD__) || \ |
| 11604 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 11605 | int mib[2]; |
| 11606 | size_t len = sizeof(ncpu); |
| 11607 | mib[0] = CTL_HW; |
| 11608 | mib[1] = HW_NCPU; |
| 11609 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 11610 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11611 | #endif |
| 11612 | if (ncpu >= 1) |
| 11613 | return PyLong_FromLong(ncpu); |
| 11614 | else |
| 11615 | Py_RETURN_NONE; |
| 11616 | } |
| 11617 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11618 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11619 | /*[clinic input] |
| 11620 | os.get_inheritable -> bool |
| 11621 | |
| 11622 | fd: int |
| 11623 | / |
| 11624 | |
| 11625 | Get the close-on-exe flag of the specified file descriptor. |
| 11626 | [clinic start generated code]*/ |
| 11627 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11628 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11629 | os_get_inheritable_impl(PyObject *module, int fd) |
| 11630 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11631 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11632 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11633 | _Py_BEGIN_SUPPRESS_IPH |
| 11634 | return_value = _Py_get_inheritable(fd); |
| 11635 | _Py_END_SUPPRESS_IPH |
| 11636 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11637 | } |
| 11638 | |
| 11639 | |
| 11640 | /*[clinic input] |
| 11641 | os.set_inheritable |
| 11642 | fd: int |
| 11643 | inheritable: int |
| 11644 | / |
| 11645 | |
| 11646 | Set the inheritable flag of the specified file descriptor. |
| 11647 | [clinic start generated code]*/ |
| 11648 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11649 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11650 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 11651 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11652 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11653 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11654 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11655 | _Py_BEGIN_SUPPRESS_IPH |
| 11656 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 11657 | _Py_END_SUPPRESS_IPH |
| 11658 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11659 | return NULL; |
| 11660 | Py_RETURN_NONE; |
| 11661 | } |
| 11662 | |
| 11663 | |
| 11664 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11665 | /*[clinic input] |
| 11666 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11667 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11668 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11669 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11670 | Get the close-on-exe flag of the specified file descriptor. |
| 11671 | [clinic start generated code]*/ |
| 11672 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11673 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11674 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 11675 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11676 | { |
| 11677 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11678 | |
| 11679 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 11680 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11681 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11682 | } |
| 11683 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11684 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11685 | } |
| 11686 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11687 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11688 | /*[clinic input] |
| 11689 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11690 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11691 | inheritable: bool |
| 11692 | / |
| 11693 | |
| 11694 | Set the inheritable flag of the specified handle. |
| 11695 | [clinic start generated code]*/ |
| 11696 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11697 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11698 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11699 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 11700 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11701 | { |
| 11702 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11703 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 11704 | PyErr_SetFromWindowsErr(0); |
| 11705 | return NULL; |
| 11706 | } |
| 11707 | Py_RETURN_NONE; |
| 11708 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11709 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11710 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11711 | #ifndef MS_WINDOWS |
| 11712 | PyDoc_STRVAR(get_blocking__doc__, |
| 11713 | "get_blocking(fd) -> bool\n" \ |
| 11714 | "\n" \ |
| 11715 | "Get the blocking mode of the file descriptor:\n" \ |
| 11716 | "False if the O_NONBLOCK flag is set, True if the flag is cleared."); |
| 11717 | |
| 11718 | static PyObject* |
| 11719 | posix_get_blocking(PyObject *self, PyObject *args) |
| 11720 | { |
| 11721 | int fd; |
| 11722 | int blocking; |
| 11723 | |
| 11724 | if (!PyArg_ParseTuple(args, "i:get_blocking", &fd)) |
| 11725 | return NULL; |
| 11726 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11727 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11728 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11729 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11730 | if (blocking < 0) |
| 11731 | return NULL; |
| 11732 | return PyBool_FromLong(blocking); |
| 11733 | } |
| 11734 | |
| 11735 | PyDoc_STRVAR(set_blocking__doc__, |
| 11736 | "set_blocking(fd, blocking)\n" \ |
| 11737 | "\n" \ |
| 11738 | "Set the blocking mode of the specified file descriptor.\n" \ |
| 11739 | "Set the O_NONBLOCK flag if blocking is False,\n" \ |
| 11740 | "clear the O_NONBLOCK flag otherwise."); |
| 11741 | |
| 11742 | static PyObject* |
| 11743 | posix_set_blocking(PyObject *self, PyObject *args) |
| 11744 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11745 | int fd, blocking, result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11746 | |
| 11747 | if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking)) |
| 11748 | return NULL; |
| 11749 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11750 | _Py_BEGIN_SUPPRESS_IPH |
| 11751 | result = _Py_set_blocking(fd, blocking); |
| 11752 | _Py_END_SUPPRESS_IPH |
| 11753 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11754 | return NULL; |
| 11755 | Py_RETURN_NONE; |
| 11756 | } |
| 11757 | #endif /* !MS_WINDOWS */ |
| 11758 | |
| 11759 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 11760 | /*[clinic input] |
| 11761 | class os.DirEntry "DirEntry *" "&DirEntryType" |
| 11762 | [clinic start generated code]*/ |
| 11763 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11764 | |
| 11765 | typedef struct { |
| 11766 | PyObject_HEAD |
| 11767 | PyObject *name; |
| 11768 | PyObject *path; |
| 11769 | PyObject *stat; |
| 11770 | PyObject *lstat; |
| 11771 | #ifdef MS_WINDOWS |
| 11772 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 11773 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11774 | int got_file_index; |
| 11775 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11776 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11777 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11778 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11779 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 11780 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11781 | #endif |
| 11782 | } DirEntry; |
| 11783 | |
| 11784 | static void |
| 11785 | DirEntry_dealloc(DirEntry *entry) |
| 11786 | { |
| 11787 | Py_XDECREF(entry->name); |
| 11788 | Py_XDECREF(entry->path); |
| 11789 | Py_XDECREF(entry->stat); |
| 11790 | Py_XDECREF(entry->lstat); |
| 11791 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 11792 | } |
| 11793 | |
| 11794 | /* Forward reference */ |
| 11795 | static int |
| 11796 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 11797 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 11798 | /*[clinic input] |
| 11799 | os.DirEntry.is_symlink -> bool |
| 11800 | |
| 11801 | Return True if the entry is a symbolic link; cached per entry. |
| 11802 | [clinic start generated code]*/ |
| 11803 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11804 | static int |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 11805 | os_DirEntry_is_symlink_impl(DirEntry *self) |
| 11806 | /*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11807 | { |
| 11808 | #ifdef MS_WINDOWS |
| 11809 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11810 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 11811 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11812 | if (self->d_type != DT_UNKNOWN) |
| 11813 | return self->d_type == DT_LNK; |
| 11814 | else |
| 11815 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11816 | #else |
| 11817 | /* POSIX without d_type */ |
| 11818 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11819 | #endif |
| 11820 | } |
| 11821 | |
| 11822 | static PyObject * |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11823 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 11824 | { |
| 11825 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 11826 | STRUCT_STAT st; |
| 11827 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11828 | |
| 11829 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 11830 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 11831 | return NULL; |
| 11832 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11833 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 11834 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 11835 | return NULL; |
| 11836 | const char *path = PyBytes_AS_STRING(ub); |
| 11837 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 11838 | #ifdef HAVE_FSTATAT |
| 11839 | result = fstatat(self->dir_fd, path, &st, |
| 11840 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 11841 | #else |
| 11842 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 11843 | return NULL; |
| 11844 | #endif /* HAVE_FSTATAT */ |
| 11845 | } |
| 11846 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 11847 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 11848 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 11849 | if (follow_symlinks) |
| 11850 | result = STAT(path, &st); |
| 11851 | else |
| 11852 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 11853 | } |
| 11854 | Py_DECREF(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11855 | |
| 11856 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 11857 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11858 | |
| 11859 | return _pystat_fromstructstat(&st); |
| 11860 | } |
| 11861 | |
| 11862 | static PyObject * |
| 11863 | DirEntry_get_lstat(DirEntry *self) |
| 11864 | { |
| 11865 | if (!self->lstat) { |
| 11866 | #ifdef MS_WINDOWS |
| 11867 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 11868 | #else /* POSIX */ |
| 11869 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 11870 | #endif |
| 11871 | } |
| 11872 | Py_XINCREF(self->lstat); |
| 11873 | return self->lstat; |
| 11874 | } |
| 11875 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 11876 | /*[clinic input] |
| 11877 | os.DirEntry.stat |
| 11878 | * |
| 11879 | follow_symlinks: bool = True |
| 11880 | |
| 11881 | Return stat_result object for the entry; cached per entry. |
| 11882 | [clinic start generated code]*/ |
| 11883 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11884 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 11885 | os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks) |
| 11886 | /*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11887 | { |
| 11888 | if (!follow_symlinks) |
| 11889 | return DirEntry_get_lstat(self); |
| 11890 | |
| 11891 | if (!self->stat) { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 11892 | int result = os_DirEntry_is_symlink_impl(self); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11893 | if (result == -1) |
| 11894 | return NULL; |
| 11895 | else if (result) |
| 11896 | self->stat = DirEntry_fetch_stat(self, 1); |
| 11897 | else |
| 11898 | self->stat = DirEntry_get_lstat(self); |
| 11899 | } |
| 11900 | |
| 11901 | Py_XINCREF(self->stat); |
| 11902 | return self->stat; |
| 11903 | } |
| 11904 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11905 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11906 | static int |
| 11907 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11908 | { |
| 11909 | PyObject *stat = NULL; |
| 11910 | PyObject *st_mode = NULL; |
| 11911 | long mode; |
| 11912 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11913 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11914 | int is_symlink; |
| 11915 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11916 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11917 | #ifdef MS_WINDOWS |
| 11918 | unsigned long dir_bits; |
| 11919 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11920 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11921 | |
| 11922 | #ifdef MS_WINDOWS |
| 11923 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 11924 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11925 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11926 | is_symlink = self->d_type == DT_LNK; |
| 11927 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 11928 | #endif |
| 11929 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11930 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11931 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11932 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 11933 | stat = os_DirEntry_stat_impl(self, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11934 | if (!stat) { |
| 11935 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 11936 | /* If file doesn't exist (anymore), then return False |
| 11937 | (i.e., say it's not a file/directory) */ |
| 11938 | PyErr_Clear(); |
| 11939 | return 0; |
| 11940 | } |
| 11941 | goto error; |
| 11942 | } |
| 11943 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 11944 | if (!st_mode) |
| 11945 | goto error; |
| 11946 | |
| 11947 | mode = PyLong_AsLong(st_mode); |
| 11948 | if (mode == -1 && PyErr_Occurred()) |
| 11949 | goto error; |
| 11950 | Py_CLEAR(st_mode); |
| 11951 | Py_CLEAR(stat); |
| 11952 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11953 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11954 | } |
| 11955 | else if (is_symlink) { |
| 11956 | assert(mode_bits != S_IFLNK); |
| 11957 | result = 0; |
| 11958 | } |
| 11959 | else { |
| 11960 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 11961 | #ifdef MS_WINDOWS |
| 11962 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 11963 | if (mode_bits == S_IFDIR) |
| 11964 | result = dir_bits != 0; |
| 11965 | else |
| 11966 | result = dir_bits == 0; |
| 11967 | #else /* POSIX */ |
| 11968 | if (mode_bits == S_IFDIR) |
| 11969 | result = self->d_type == DT_DIR; |
| 11970 | else |
| 11971 | result = self->d_type == DT_REG; |
| 11972 | #endif |
| 11973 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11974 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11975 | |
| 11976 | return result; |
| 11977 | |
| 11978 | error: |
| 11979 | Py_XDECREF(st_mode); |
| 11980 | Py_XDECREF(stat); |
| 11981 | return -1; |
| 11982 | } |
| 11983 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 11984 | /*[clinic input] |
| 11985 | os.DirEntry.is_dir -> bool |
| 11986 | * |
| 11987 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11988 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 11989 | Return True if the entry is a directory; cached per entry. |
| 11990 | [clinic start generated code]*/ |
| 11991 | |
| 11992 | static int |
| 11993 | os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks) |
| 11994 | /*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/ |
| 11995 | { |
| 11996 | return DirEntry_test_mode(self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11997 | } |
| 11998 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 11999 | /*[clinic input] |
| 12000 | os.DirEntry.is_file -> bool |
| 12001 | * |
| 12002 | follow_symlinks: bool = True |
| 12003 | |
| 12004 | Return True if the entry is a file; cached per entry. |
| 12005 | [clinic start generated code]*/ |
| 12006 | |
| 12007 | static int |
| 12008 | os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks) |
| 12009 | /*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12010 | { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12011 | return DirEntry_test_mode(self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12012 | } |
| 12013 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12014 | /*[clinic input] |
| 12015 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12016 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12017 | Return inode of the entry; cached per entry. |
| 12018 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12019 | |
| 12020 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12021 | os_DirEntry_inode_impl(DirEntry *self) |
| 12022 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12023 | { |
| 12024 | #ifdef MS_WINDOWS |
| 12025 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12026 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12027 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12028 | STRUCT_STAT stat; |
| 12029 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12030 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12031 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12032 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12033 | path = PyUnicode_AsUnicode(unicode); |
| 12034 | result = LSTAT(path, &stat); |
| 12035 | Py_DECREF(unicode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12036 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12037 | if (result != 0) |
| 12038 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12039 | |
| 12040 | self->win32_file_index = stat.st_ino; |
| 12041 | self->got_file_index = 1; |
| 12042 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12043 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 12044 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12045 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 12046 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 12047 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12048 | #endif |
| 12049 | } |
| 12050 | |
| 12051 | static PyObject * |
| 12052 | DirEntry_repr(DirEntry *self) |
| 12053 | { |
| 12054 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 12055 | } |
| 12056 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12057 | /*[clinic input] |
| 12058 | os.DirEntry.__fspath__ |
| 12059 | |
| 12060 | Returns the path for the entry. |
| 12061 | [clinic start generated code]*/ |
| 12062 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12063 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12064 | os_DirEntry___fspath___impl(DirEntry *self) |
| 12065 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12066 | { |
| 12067 | Py_INCREF(self->path); |
| 12068 | return self->path; |
| 12069 | } |
| 12070 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12071 | static PyMemberDef DirEntry_members[] = { |
| 12072 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 12073 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 12074 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 12075 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 12076 | {NULL} |
| 12077 | }; |
| 12078 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12079 | #include "clinic/posixmodule.c.h" |
| 12080 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12081 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12082 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 12083 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 12084 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 12085 | OS_DIRENTRY_STAT_METHODDEF |
| 12086 | OS_DIRENTRY_INODE_METHODDEF |
| 12087 | OS_DIRENTRY___FSPATH___METHODDEF |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12088 | {NULL} |
| 12089 | }; |
| 12090 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12091 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12092 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12093 | MODNAME ".DirEntry", /* tp_name */ |
| 12094 | sizeof(DirEntry), /* tp_basicsize */ |
| 12095 | 0, /* tp_itemsize */ |
| 12096 | /* methods */ |
| 12097 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
| 12098 | 0, /* tp_print */ |
| 12099 | 0, /* tp_getattr */ |
| 12100 | 0, /* tp_setattr */ |
| 12101 | 0, /* tp_compare */ |
| 12102 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 12103 | 0, /* tp_as_number */ |
| 12104 | 0, /* tp_as_sequence */ |
| 12105 | 0, /* tp_as_mapping */ |
| 12106 | 0, /* tp_hash */ |
| 12107 | 0, /* tp_call */ |
| 12108 | 0, /* tp_str */ |
| 12109 | 0, /* tp_getattro */ |
| 12110 | 0, /* tp_setattro */ |
| 12111 | 0, /* tp_as_buffer */ |
| 12112 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 12113 | 0, /* tp_doc */ |
| 12114 | 0, /* tp_traverse */ |
| 12115 | 0, /* tp_clear */ |
| 12116 | 0, /* tp_richcompare */ |
| 12117 | 0, /* tp_weaklistoffset */ |
| 12118 | 0, /* tp_iter */ |
| 12119 | 0, /* tp_iternext */ |
| 12120 | DirEntry_methods, /* tp_methods */ |
| 12121 | DirEntry_members, /* tp_members */ |
| 12122 | }; |
| 12123 | |
| 12124 | #ifdef MS_WINDOWS |
| 12125 | |
| 12126 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12127 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12128 | { |
| 12129 | Py_ssize_t path_len; |
| 12130 | Py_ssize_t size; |
| 12131 | wchar_t *result; |
| 12132 | wchar_t ch; |
| 12133 | |
| 12134 | if (!path_wide) { /* Default arg: "." */ |
| 12135 | path_wide = L"."; |
| 12136 | path_len = 1; |
| 12137 | } |
| 12138 | else { |
| 12139 | path_len = wcslen(path_wide); |
| 12140 | } |
| 12141 | |
| 12142 | /* The +1's are for the path separator and the NUL */ |
| 12143 | size = path_len + 1 + wcslen(filename) + 1; |
| 12144 | result = PyMem_New(wchar_t, size); |
| 12145 | if (!result) { |
| 12146 | PyErr_NoMemory(); |
| 12147 | return NULL; |
| 12148 | } |
| 12149 | wcscpy(result, path_wide); |
| 12150 | if (path_len > 0) { |
| 12151 | ch = result[path_len - 1]; |
| 12152 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 12153 | result[path_len++] = SEP; |
| 12154 | wcscpy(result + path_len, filename); |
| 12155 | } |
| 12156 | return result; |
| 12157 | } |
| 12158 | |
| 12159 | static PyObject * |
| 12160 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 12161 | { |
| 12162 | DirEntry *entry; |
| 12163 | BY_HANDLE_FILE_INFORMATION file_info; |
| 12164 | ULONG reparse_tag; |
| 12165 | wchar_t *joined_path; |
| 12166 | |
| 12167 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 12168 | if (!entry) |
| 12169 | return NULL; |
| 12170 | entry->name = NULL; |
| 12171 | entry->path = NULL; |
| 12172 | entry->stat = NULL; |
| 12173 | entry->lstat = NULL; |
| 12174 | entry->got_file_index = 0; |
| 12175 | |
| 12176 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 12177 | if (!entry->name) |
| 12178 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12179 | if (path->narrow) { |
| 12180 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 12181 | if (!entry->name) |
| 12182 | goto error; |
| 12183 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12184 | |
| 12185 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 12186 | if (!joined_path) |
| 12187 | goto error; |
| 12188 | |
| 12189 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 12190 | PyMem_Free(joined_path); |
| 12191 | if (!entry->path) |
| 12192 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12193 | if (path->narrow) { |
| 12194 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 12195 | if (!entry->path) |
| 12196 | goto error; |
| 12197 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12198 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12199 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12200 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 12201 | |
| 12202 | return (PyObject *)entry; |
| 12203 | |
| 12204 | error: |
| 12205 | Py_DECREF(entry); |
| 12206 | return NULL; |
| 12207 | } |
| 12208 | |
| 12209 | #else /* POSIX */ |
| 12210 | |
| 12211 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12212 | 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] | 12213 | { |
| 12214 | Py_ssize_t path_len; |
| 12215 | Py_ssize_t size; |
| 12216 | char *result; |
| 12217 | |
| 12218 | if (!path_narrow) { /* Default arg: "." */ |
| 12219 | path_narrow = "."; |
| 12220 | path_len = 1; |
| 12221 | } |
| 12222 | else { |
| 12223 | path_len = strlen(path_narrow); |
| 12224 | } |
| 12225 | |
| 12226 | if (filename_len == -1) |
| 12227 | filename_len = strlen(filename); |
| 12228 | |
| 12229 | /* The +1's are for the path separator and the NUL */ |
| 12230 | size = path_len + 1 + filename_len + 1; |
| 12231 | result = PyMem_New(char, size); |
| 12232 | if (!result) { |
| 12233 | PyErr_NoMemory(); |
| 12234 | return NULL; |
| 12235 | } |
| 12236 | strcpy(result, path_narrow); |
| 12237 | if (path_len > 0 && result[path_len - 1] != '/') |
| 12238 | result[path_len++] = '/'; |
| 12239 | strcpy(result + path_len, filename); |
| 12240 | return result; |
| 12241 | } |
| 12242 | |
| 12243 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12244 | 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] | 12245 | ino_t d_ino |
| 12246 | #ifdef HAVE_DIRENT_D_TYPE |
| 12247 | , unsigned char d_type |
| 12248 | #endif |
| 12249 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12250 | { |
| 12251 | DirEntry *entry; |
| 12252 | char *joined_path; |
| 12253 | |
| 12254 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 12255 | if (!entry) |
| 12256 | return NULL; |
| 12257 | entry->name = NULL; |
| 12258 | entry->path = NULL; |
| 12259 | entry->stat = NULL; |
| 12260 | entry->lstat = NULL; |
| 12261 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12262 | if (path->fd != -1) { |
| 12263 | entry->dir_fd = path->fd; |
| 12264 | joined_path = NULL; |
| 12265 | } |
| 12266 | else { |
| 12267 | entry->dir_fd = DEFAULT_DIR_FD; |
| 12268 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 12269 | if (!joined_path) |
| 12270 | goto error; |
| 12271 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12272 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 12273 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12274 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12275 | if (joined_path) |
| 12276 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12277 | } |
| 12278 | else { |
| 12279 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12280 | if (joined_path) |
| 12281 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12282 | } |
| 12283 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12284 | if (!entry->name) |
| 12285 | goto error; |
| 12286 | |
| 12287 | if (path->fd != -1) { |
| 12288 | entry->path = entry->name; |
| 12289 | Py_INCREF(entry->path); |
| 12290 | } |
| 12291 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12292 | goto error; |
| 12293 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12294 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12295 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12296 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12297 | entry->d_ino = d_ino; |
| 12298 | |
| 12299 | return (PyObject *)entry; |
| 12300 | |
| 12301 | error: |
| 12302 | Py_XDECREF(entry); |
| 12303 | return NULL; |
| 12304 | } |
| 12305 | |
| 12306 | #endif |
| 12307 | |
| 12308 | |
| 12309 | typedef struct { |
| 12310 | PyObject_HEAD |
| 12311 | path_t path; |
| 12312 | #ifdef MS_WINDOWS |
| 12313 | HANDLE handle; |
| 12314 | WIN32_FIND_DATAW file_data; |
| 12315 | int first_time; |
| 12316 | #else /* POSIX */ |
| 12317 | DIR *dirp; |
| 12318 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12319 | #ifdef HAVE_FDOPENDIR |
| 12320 | int fd; |
| 12321 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12322 | } ScandirIterator; |
| 12323 | |
| 12324 | #ifdef MS_WINDOWS |
| 12325 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12326 | static int |
| 12327 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 12328 | { |
| 12329 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 12330 | } |
| 12331 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12332 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12333 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12334 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12335 | HANDLE handle = iterator->handle; |
| 12336 | |
| 12337 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12338 | return; |
| 12339 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12340 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12341 | Py_BEGIN_ALLOW_THREADS |
| 12342 | FindClose(handle); |
| 12343 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12344 | } |
| 12345 | |
| 12346 | static PyObject * |
| 12347 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12348 | { |
| 12349 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 12350 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12351 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12352 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12353 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12354 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12355 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12356 | |
| 12357 | while (1) { |
| 12358 | if (!iterator->first_time) { |
| 12359 | Py_BEGIN_ALLOW_THREADS |
| 12360 | success = FindNextFileW(iterator->handle, file_data); |
| 12361 | Py_END_ALLOW_THREADS |
| 12362 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12363 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12364 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12365 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12366 | break; |
| 12367 | } |
| 12368 | } |
| 12369 | iterator->first_time = 0; |
| 12370 | |
| 12371 | /* Skip over . and .. */ |
| 12372 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12373 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 12374 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 12375 | if (!entry) |
| 12376 | break; |
| 12377 | return entry; |
| 12378 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12379 | |
| 12380 | /* Loop till we get a non-dot directory or finish iterating */ |
| 12381 | } |
| 12382 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12383 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12384 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12385 | return NULL; |
| 12386 | } |
| 12387 | |
| 12388 | #else /* POSIX */ |
| 12389 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12390 | static int |
| 12391 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 12392 | { |
| 12393 | return !iterator->dirp; |
| 12394 | } |
| 12395 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12396 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12397 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12398 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12399 | DIR *dirp = iterator->dirp; |
| 12400 | |
| 12401 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12402 | return; |
| 12403 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12404 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12405 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12406 | #ifdef HAVE_FDOPENDIR |
| 12407 | if (iterator->path.fd != -1) |
| 12408 | rewinddir(dirp); |
| 12409 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 12410 | closedir(dirp); |
| 12411 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12412 | return; |
| 12413 | } |
| 12414 | |
| 12415 | static PyObject * |
| 12416 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12417 | { |
| 12418 | struct dirent *direntp; |
| 12419 | Py_ssize_t name_len; |
| 12420 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12421 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12422 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12423 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12424 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12425 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12426 | |
| 12427 | while (1) { |
| 12428 | errno = 0; |
| 12429 | Py_BEGIN_ALLOW_THREADS |
| 12430 | direntp = readdir(iterator->dirp); |
| 12431 | Py_END_ALLOW_THREADS |
| 12432 | |
| 12433 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12434 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12435 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12436 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12437 | break; |
| 12438 | } |
| 12439 | |
| 12440 | /* Skip over . and .. */ |
| 12441 | name_len = NAMLEN(direntp); |
| 12442 | is_dot = direntp->d_name[0] == '.' && |
| 12443 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 12444 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12445 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12446 | name_len, direntp->d_ino |
| 12447 | #ifdef HAVE_DIRENT_D_TYPE |
| 12448 | , direntp->d_type |
| 12449 | #endif |
| 12450 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12451 | if (!entry) |
| 12452 | break; |
| 12453 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12454 | } |
| 12455 | |
| 12456 | /* Loop till we get a non-dot directory or finish iterating */ |
| 12457 | } |
| 12458 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12459 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12460 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12461 | return NULL; |
| 12462 | } |
| 12463 | |
| 12464 | #endif |
| 12465 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12466 | static PyObject * |
| 12467 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 12468 | { |
| 12469 | ScandirIterator_closedir(self); |
| 12470 | Py_RETURN_NONE; |
| 12471 | } |
| 12472 | |
| 12473 | static PyObject * |
| 12474 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 12475 | { |
| 12476 | Py_INCREF(self); |
| 12477 | return self; |
| 12478 | } |
| 12479 | |
| 12480 | static PyObject * |
| 12481 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 12482 | { |
| 12483 | ScandirIterator_closedir(self); |
| 12484 | Py_RETURN_NONE; |
| 12485 | } |
| 12486 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12487 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12488 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 12489 | { |
| 12490 | PyObject *error_type, *error_value, *error_traceback; |
| 12491 | |
| 12492 | /* Save the current exception, if any. */ |
| 12493 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 12494 | |
| 12495 | if (!ScandirIterator_is_closed(iterator)) { |
| 12496 | ScandirIterator_closedir(iterator); |
| 12497 | |
| 12498 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 12499 | "unclosed scandir iterator %R", iterator)) { |
| 12500 | /* Spurious errors can appear at shutdown */ |
| 12501 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 12502 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 12503 | } |
| 12504 | } |
| 12505 | } |
| 12506 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12507 | path_cleanup(&iterator->path); |
| 12508 | |
| 12509 | /* Restore the saved exception. */ |
| 12510 | PyErr_Restore(error_type, error_value, error_traceback); |
| 12511 | } |
| 12512 | |
| 12513 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12514 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 12515 | { |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12516 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 12517 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12518 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12519 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 12520 | } |
| 12521 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12522 | static PyMethodDef ScandirIterator_methods[] = { |
| 12523 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 12524 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 12525 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 12526 | {NULL} |
| 12527 | }; |
| 12528 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12529 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12530 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12531 | MODNAME ".ScandirIterator", /* tp_name */ |
| 12532 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 12533 | 0, /* tp_itemsize */ |
| 12534 | /* methods */ |
| 12535 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
| 12536 | 0, /* tp_print */ |
| 12537 | 0, /* tp_getattr */ |
| 12538 | 0, /* tp_setattr */ |
| 12539 | 0, /* tp_compare */ |
| 12540 | 0, /* tp_repr */ |
| 12541 | 0, /* tp_as_number */ |
| 12542 | 0, /* tp_as_sequence */ |
| 12543 | 0, /* tp_as_mapping */ |
| 12544 | 0, /* tp_hash */ |
| 12545 | 0, /* tp_call */ |
| 12546 | 0, /* tp_str */ |
| 12547 | 0, /* tp_getattro */ |
| 12548 | 0, /* tp_setattro */ |
| 12549 | 0, /* tp_as_buffer */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12550 | Py_TPFLAGS_DEFAULT |
| 12551 | | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12552 | 0, /* tp_doc */ |
| 12553 | 0, /* tp_traverse */ |
| 12554 | 0, /* tp_clear */ |
| 12555 | 0, /* tp_richcompare */ |
| 12556 | 0, /* tp_weaklistoffset */ |
| 12557 | PyObject_SelfIter, /* tp_iter */ |
| 12558 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 12559 | ScandirIterator_methods, /* tp_methods */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 12560 | 0, /* tp_members */ |
| 12561 | 0, /* tp_getset */ |
| 12562 | 0, /* tp_base */ |
| 12563 | 0, /* tp_dict */ |
| 12564 | 0, /* tp_descr_get */ |
| 12565 | 0, /* tp_descr_set */ |
| 12566 | 0, /* tp_dictoffset */ |
| 12567 | 0, /* tp_init */ |
| 12568 | 0, /* tp_alloc */ |
| 12569 | 0, /* tp_new */ |
| 12570 | 0, /* tp_free */ |
| 12571 | 0, /* tp_is_gc */ |
| 12572 | 0, /* tp_bases */ |
| 12573 | 0, /* tp_mro */ |
| 12574 | 0, /* tp_cache */ |
| 12575 | 0, /* tp_subclasses */ |
| 12576 | 0, /* tp_weaklist */ |
| 12577 | 0, /* tp_del */ |
| 12578 | 0, /* tp_version_tag */ |
| 12579 | (destructor)ScandirIterator_finalize, /* tp_finalize */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12580 | }; |
| 12581 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12582 | /*[clinic input] |
| 12583 | os.scandir |
| 12584 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12585 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12586 | |
| 12587 | Return an iterator of DirEntry objects for given path. |
| 12588 | |
| 12589 | path can be specified as either str, bytes or path-like object. If path |
| 12590 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 12591 | all other circumstances they will be str. |
| 12592 | |
| 12593 | If path is None, uses the path='.'. |
| 12594 | [clinic start generated code]*/ |
| 12595 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12596 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12597 | os_scandir_impl(PyObject *module, path_t *path) |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12598 | /*[clinic end generated code: output=6eb2668b675ca89e input=b139dc1c57f60846]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12599 | { |
| 12600 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12601 | #ifdef MS_WINDOWS |
| 12602 | wchar_t *path_strW; |
| 12603 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12604 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12605 | #ifdef HAVE_FDOPENDIR |
| 12606 | int fd = -1; |
| 12607 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12608 | #endif |
| 12609 | |
| 12610 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 12611 | if (!iterator) |
| 12612 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12613 | |
| 12614 | #ifdef MS_WINDOWS |
| 12615 | iterator->handle = INVALID_HANDLE_VALUE; |
| 12616 | #else |
| 12617 | iterator->dirp = NULL; |
| 12618 | #endif |
| 12619 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12620 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 12621 | /* Move the ownership to iterator->path */ |
| 12622 | path->object = NULL; |
| 12623 | path->cleanup = NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12624 | |
| 12625 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12626 | iterator->first_time = 1; |
| 12627 | |
| 12628 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 12629 | if (!path_strW) |
| 12630 | goto error; |
| 12631 | |
| 12632 | Py_BEGIN_ALLOW_THREADS |
| 12633 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 12634 | Py_END_ALLOW_THREADS |
| 12635 | |
| 12636 | PyMem_Free(path_strW); |
| 12637 | |
| 12638 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 12639 | path_error(&iterator->path); |
| 12640 | goto error; |
| 12641 | } |
| 12642 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12643 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12644 | #ifdef HAVE_FDOPENDIR |
| 12645 | if (path->fd != -1) { |
| 12646 | /* closedir() closes the FD, so we duplicate it */ |
| 12647 | fd = _Py_dup(path->fd); |
| 12648 | if (fd == -1) |
| 12649 | goto error; |
| 12650 | |
| 12651 | Py_BEGIN_ALLOW_THREADS |
| 12652 | iterator->dirp = fdopendir(fd); |
| 12653 | Py_END_ALLOW_THREADS |
| 12654 | } |
| 12655 | else |
| 12656 | #endif |
| 12657 | { |
| 12658 | if (iterator->path.narrow) |
| 12659 | path_str = iterator->path.narrow; |
| 12660 | else |
| 12661 | path_str = "."; |
| 12662 | |
| 12663 | Py_BEGIN_ALLOW_THREADS |
| 12664 | iterator->dirp = opendir(path_str); |
| 12665 | Py_END_ALLOW_THREADS |
| 12666 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12667 | |
| 12668 | if (!iterator->dirp) { |
| 12669 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12670 | #ifdef HAVE_FDOPENDIR |
| 12671 | if (fd != -1) { |
| 12672 | Py_BEGIN_ALLOW_THREADS |
| 12673 | close(fd); |
| 12674 | Py_END_ALLOW_THREADS |
| 12675 | } |
| 12676 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12677 | goto error; |
| 12678 | } |
| 12679 | #endif |
| 12680 | |
| 12681 | return (PyObject *)iterator; |
| 12682 | |
| 12683 | error: |
| 12684 | Py_DECREF(iterator); |
| 12685 | return NULL; |
| 12686 | } |
| 12687 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12688 | /* |
| 12689 | Return the file system path representation of the object. |
| 12690 | |
| 12691 | If the object is str or bytes, then allow it to pass through with |
| 12692 | an incremented refcount. If the object defines __fspath__(), then |
| 12693 | return the result of that method. All other types raise a TypeError. |
| 12694 | */ |
| 12695 | PyObject * |
| 12696 | PyOS_FSPath(PyObject *path) |
| 12697 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 12698 | /* For error message reasons, this function is manually inlined in |
| 12699 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12700 | _Py_IDENTIFIER(__fspath__); |
| 12701 | PyObject *func = NULL; |
| 12702 | PyObject *path_repr = NULL; |
| 12703 | |
| 12704 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 12705 | Py_INCREF(path); |
| 12706 | return path; |
| 12707 | } |
| 12708 | |
| 12709 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 12710 | if (NULL == func) { |
| 12711 | return PyErr_Format(PyExc_TypeError, |
| 12712 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 12713 | "not %.200s", |
| 12714 | Py_TYPE(path)->tp_name); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12715 | } |
| 12716 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 12717 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12718 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 12719 | if (NULL == path_repr) { |
| 12720 | return NULL; |
| 12721 | } |
| 12722 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 12723 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 12724 | PyErr_Format(PyExc_TypeError, |
| 12725 | "expected %.200s.__fspath__() to return str or bytes, " |
| 12726 | "not %.200s", Py_TYPE(path)->tp_name, |
| 12727 | Py_TYPE(path_repr)->tp_name); |
| 12728 | Py_DECREF(path_repr); |
| 12729 | return NULL; |
| 12730 | } |
| 12731 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12732 | return path_repr; |
| 12733 | } |
| 12734 | |
| 12735 | /*[clinic input] |
| 12736 | os.fspath |
| 12737 | |
| 12738 | path: object |
| 12739 | |
| 12740 | Return the file system path representation of the object. |
| 12741 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 12742 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 12743 | object defines __fspath__(), then return the result of that method. All other |
| 12744 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12745 | [clinic start generated code]*/ |
| 12746 | |
| 12747 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12748 | os_fspath_impl(PyObject *module, PyObject *path) |
| 12749 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12750 | { |
| 12751 | return PyOS_FSPath(path); |
| 12752 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12753 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12754 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 12755 | /*[clinic input] |
| 12756 | os.getrandom |
| 12757 | |
| 12758 | size: Py_ssize_t |
| 12759 | flags: int=0 |
| 12760 | |
| 12761 | Obtain a series of random bytes. |
| 12762 | [clinic start generated code]*/ |
| 12763 | |
| 12764 | static PyObject * |
| 12765 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 12766 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 12767 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12768 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 12769 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12770 | |
| 12771 | if (size < 0) { |
| 12772 | errno = EINVAL; |
| 12773 | return posix_error(); |
| 12774 | } |
| 12775 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 12776 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 12777 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12778 | PyErr_NoMemory(); |
| 12779 | return NULL; |
| 12780 | } |
| 12781 | |
| 12782 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 12783 | n = syscall(SYS_getrandom, |
| 12784 | PyBytes_AS_STRING(bytes), |
| 12785 | PyBytes_GET_SIZE(bytes), |
| 12786 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12787 | if (n < 0 && errno == EINTR) { |
| 12788 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 12789 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12790 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 12791 | |
| 12792 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12793 | continue; |
| 12794 | } |
| 12795 | break; |
| 12796 | } |
| 12797 | |
| 12798 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12799 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 12800 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12801 | } |
| 12802 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 12803 | if (n != size) { |
| 12804 | _PyBytes_Resize(&bytes, n); |
| 12805 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12806 | |
| 12807 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 12808 | |
| 12809 | error: |
| 12810 | Py_DECREF(bytes); |
| 12811 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12812 | } |
| 12813 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 12814 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12815 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12816 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12817 | |
| 12818 | OS_STAT_METHODDEF |
| 12819 | OS_ACCESS_METHODDEF |
| 12820 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12821 | OS_CHDIR_METHODDEF |
| 12822 | OS_CHFLAGS_METHODDEF |
| 12823 | OS_CHMOD_METHODDEF |
| 12824 | OS_FCHMOD_METHODDEF |
| 12825 | OS_LCHMOD_METHODDEF |
| 12826 | OS_CHOWN_METHODDEF |
| 12827 | OS_FCHOWN_METHODDEF |
| 12828 | OS_LCHOWN_METHODDEF |
| 12829 | OS_LCHFLAGS_METHODDEF |
| 12830 | OS_CHROOT_METHODDEF |
| 12831 | OS_CTERMID_METHODDEF |
| 12832 | OS_GETCWD_METHODDEF |
| 12833 | OS_GETCWDB_METHODDEF |
| 12834 | OS_LINK_METHODDEF |
| 12835 | OS_LISTDIR_METHODDEF |
| 12836 | OS_LSTAT_METHODDEF |
| 12837 | OS_MKDIR_METHODDEF |
| 12838 | OS_NICE_METHODDEF |
| 12839 | OS_GETPRIORITY_METHODDEF |
| 12840 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 12841 | OS_POSIX_SPAWN_METHODDEF |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12842 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12843 | {"readlink", (PyCFunction)posix_readlink, |
| 12844 | METH_VARARGS | METH_KEYWORDS, |
| 12845 | readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 12846 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12847 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12848 | {"readlink", (PyCFunction)win_readlink, |
| 12849 | METH_VARARGS | METH_KEYWORDS, |
| 12850 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12851 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12852 | OS_RENAME_METHODDEF |
| 12853 | OS_REPLACE_METHODDEF |
| 12854 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12855 | OS_SYMLINK_METHODDEF |
| 12856 | OS_SYSTEM_METHODDEF |
| 12857 | OS_UMASK_METHODDEF |
| 12858 | OS_UNAME_METHODDEF |
| 12859 | OS_UNLINK_METHODDEF |
| 12860 | OS_REMOVE_METHODDEF |
| 12861 | OS_UTIME_METHODDEF |
| 12862 | OS_TIMES_METHODDEF |
| 12863 | OS__EXIT_METHODDEF |
| 12864 | OS_EXECV_METHODDEF |
| 12865 | OS_EXECVE_METHODDEF |
| 12866 | OS_SPAWNV_METHODDEF |
| 12867 | OS_SPAWNVE_METHODDEF |
| 12868 | OS_FORK1_METHODDEF |
| 12869 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 12870 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12871 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 12872 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 12873 | OS_SCHED_GETPARAM_METHODDEF |
| 12874 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 12875 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 12876 | OS_SCHED_SETPARAM_METHODDEF |
| 12877 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 12878 | OS_SCHED_YIELD_METHODDEF |
| 12879 | OS_SCHED_SETAFFINITY_METHODDEF |
| 12880 | OS_SCHED_GETAFFINITY_METHODDEF |
| 12881 | OS_OPENPTY_METHODDEF |
| 12882 | OS_FORKPTY_METHODDEF |
| 12883 | OS_GETEGID_METHODDEF |
| 12884 | OS_GETEUID_METHODDEF |
| 12885 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 12886 | #ifdef HAVE_GETGROUPLIST |
| 12887 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 12888 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12889 | OS_GETGROUPS_METHODDEF |
| 12890 | OS_GETPID_METHODDEF |
| 12891 | OS_GETPGRP_METHODDEF |
| 12892 | OS_GETPPID_METHODDEF |
| 12893 | OS_GETUID_METHODDEF |
| 12894 | OS_GETLOGIN_METHODDEF |
| 12895 | OS_KILL_METHODDEF |
| 12896 | OS_KILLPG_METHODDEF |
| 12897 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12898 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12899 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12900 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12901 | OS_SETUID_METHODDEF |
| 12902 | OS_SETEUID_METHODDEF |
| 12903 | OS_SETREUID_METHODDEF |
| 12904 | OS_SETGID_METHODDEF |
| 12905 | OS_SETEGID_METHODDEF |
| 12906 | OS_SETREGID_METHODDEF |
| 12907 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12908 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12909 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12910 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12911 | OS_GETPGID_METHODDEF |
| 12912 | OS_SETPGRP_METHODDEF |
| 12913 | OS_WAIT_METHODDEF |
| 12914 | OS_WAIT3_METHODDEF |
| 12915 | OS_WAIT4_METHODDEF |
| 12916 | OS_WAITID_METHODDEF |
| 12917 | OS_WAITPID_METHODDEF |
| 12918 | OS_GETSID_METHODDEF |
| 12919 | OS_SETSID_METHODDEF |
| 12920 | OS_SETPGID_METHODDEF |
| 12921 | OS_TCGETPGRP_METHODDEF |
| 12922 | OS_TCSETPGRP_METHODDEF |
| 12923 | OS_OPEN_METHODDEF |
| 12924 | OS_CLOSE_METHODDEF |
| 12925 | OS_CLOSERANGE_METHODDEF |
| 12926 | OS_DEVICE_ENCODING_METHODDEF |
| 12927 | OS_DUP_METHODDEF |
| 12928 | OS_DUP2_METHODDEF |
| 12929 | OS_LOCKF_METHODDEF |
| 12930 | OS_LSEEK_METHODDEF |
| 12931 | OS_READ_METHODDEF |
| 12932 | OS_READV_METHODDEF |
| 12933 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 12934 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12935 | OS_WRITE_METHODDEF |
| 12936 | OS_WRITEV_METHODDEF |
| 12937 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 12938 | OS_PWRITEV_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12939 | #ifdef HAVE_SENDFILE |
| 12940 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 12941 | posix_sendfile__doc__}, |
| 12942 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12943 | OS_FSTAT_METHODDEF |
| 12944 | OS_ISATTY_METHODDEF |
| 12945 | OS_PIPE_METHODDEF |
| 12946 | OS_PIPE2_METHODDEF |
| 12947 | OS_MKFIFO_METHODDEF |
| 12948 | OS_MKNOD_METHODDEF |
| 12949 | OS_MAJOR_METHODDEF |
| 12950 | OS_MINOR_METHODDEF |
| 12951 | OS_MAKEDEV_METHODDEF |
| 12952 | OS_FTRUNCATE_METHODDEF |
| 12953 | OS_TRUNCATE_METHODDEF |
| 12954 | OS_POSIX_FALLOCATE_METHODDEF |
| 12955 | OS_POSIX_FADVISE_METHODDEF |
| 12956 | OS_PUTENV_METHODDEF |
| 12957 | OS_UNSETENV_METHODDEF |
| 12958 | OS_STRERROR_METHODDEF |
| 12959 | OS_FCHDIR_METHODDEF |
| 12960 | OS_FSYNC_METHODDEF |
| 12961 | OS_SYNC_METHODDEF |
| 12962 | OS_FDATASYNC_METHODDEF |
| 12963 | OS_WCOREDUMP_METHODDEF |
| 12964 | OS_WIFCONTINUED_METHODDEF |
| 12965 | OS_WIFSTOPPED_METHODDEF |
| 12966 | OS_WIFSIGNALED_METHODDEF |
| 12967 | OS_WIFEXITED_METHODDEF |
| 12968 | OS_WEXITSTATUS_METHODDEF |
| 12969 | OS_WTERMSIG_METHODDEF |
| 12970 | OS_WSTOPSIG_METHODDEF |
| 12971 | OS_FSTATVFS_METHODDEF |
| 12972 | OS_STATVFS_METHODDEF |
| 12973 | OS_CONFSTR_METHODDEF |
| 12974 | OS_SYSCONF_METHODDEF |
| 12975 | OS_FPATHCONF_METHODDEF |
| 12976 | OS_PATHCONF_METHODDEF |
| 12977 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 12978 | OS__GETFULLPATHNAME_METHODDEF |
| 12979 | OS__ISDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12980 | OS__GETDISKUSAGE_METHODDEF |
| 12981 | OS__GETFINALPATHNAME_METHODDEF |
| 12982 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 12983 | OS_GETLOADAVG_METHODDEF |
| 12984 | OS_URANDOM_METHODDEF |
| 12985 | OS_SETRESUID_METHODDEF |
| 12986 | OS_SETRESGID_METHODDEF |
| 12987 | OS_GETRESUID_METHODDEF |
| 12988 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12989 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12990 | OS_GETXATTR_METHODDEF |
| 12991 | OS_SETXATTR_METHODDEF |
| 12992 | OS_REMOVEXATTR_METHODDEF |
| 12993 | OS_LISTXATTR_METHODDEF |
| 12994 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12995 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 12996 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 12997 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12998 | OS_CPU_COUNT_METHODDEF |
| 12999 | OS_GET_INHERITABLE_METHODDEF |
| 13000 | OS_SET_INHERITABLE_METHODDEF |
| 13001 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 13002 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13003 | #ifndef MS_WINDOWS |
| 13004 | {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__}, |
| 13005 | {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__}, |
| 13006 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13007 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13008 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13009 | OS_GETRANDOM_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13010 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13011 | }; |
| 13012 | |
| 13013 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13014 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13015 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13016 | enable_symlink() |
| 13017 | { |
| 13018 | HANDLE tok; |
| 13019 | TOKEN_PRIVILEGES tok_priv; |
| 13020 | LUID luid; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13021 | |
| 13022 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13023 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13024 | |
| 13025 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13026 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13027 | |
| 13028 | tok_priv.PrivilegeCount = 1; |
| 13029 | tok_priv.Privileges[0].Luid = luid; |
| 13030 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 13031 | |
| 13032 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 13033 | sizeof(TOKEN_PRIVILEGES), |
| 13034 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13035 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13036 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13037 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 13038 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13039 | } |
| 13040 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 13041 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13042 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13043 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13044 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13045 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13046 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13047 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13048 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13049 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13050 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13051 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13052 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13053 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13054 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13055 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13056 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13057 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13058 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13059 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13060 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13061 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13062 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13063 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13064 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13065 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13066 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13067 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13068 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13069 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13070 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13071 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13072 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13073 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13074 | #endif |
| 13075 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13076 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13077 | #endif |
| 13078 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13079 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13080 | #endif |
| 13081 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13082 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13083 | #endif |
| 13084 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13085 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13086 | #endif |
| 13087 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13088 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13089 | #endif |
| 13090 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13091 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13092 | #endif |
| 13093 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13094 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13095 | #endif |
| 13096 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13097 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13098 | #endif |
| 13099 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13100 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13101 | #endif |
| 13102 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13103 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13104 | #endif |
| 13105 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13106 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13107 | #endif |
| 13108 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13109 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13110 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13111 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13112 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13113 | #endif |
| 13114 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13115 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13116 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13117 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13118 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13119 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13120 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13121 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13122 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13123 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13124 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13125 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13126 | #endif |
| 13127 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13128 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13129 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13130 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13131 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13132 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13133 | #endif |
| 13134 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13135 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13136 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13137 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13138 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13139 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13140 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13141 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13142 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 13143 | #ifdef O_TMPFILE |
| 13144 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 13145 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13146 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13147 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13148 | #endif |
| 13149 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13150 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13151 | #endif |
| 13152 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13153 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13154 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13155 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13156 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13157 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13158 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13159 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13160 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13161 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13162 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13163 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13164 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13165 | #endif |
| 13166 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13167 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13168 | #endif |
| 13169 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13170 | /* MS Windows */ |
| 13171 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13172 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13173 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13174 | #endif |
| 13175 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13176 | /* Optimize for short life (keep in memory). */ |
| 13177 | /* 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] | 13178 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13179 | #endif |
| 13180 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13181 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13182 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13183 | #endif |
| 13184 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13185 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13186 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13187 | #endif |
| 13188 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13189 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13190 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13191 | #endif |
| 13192 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13193 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13194 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13195 | /* Send a SIGIO signal whenever input or output |
| 13196 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13197 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13198 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13199 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13200 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13201 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13202 | #endif |
| 13203 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13204 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13205 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13206 | #endif |
| 13207 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13208 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13209 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13210 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13211 | #ifdef O_NOLINKS |
| 13212 | /* 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] | 13213 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13214 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13215 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13216 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13217 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13218 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 13219 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13220 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13221 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13222 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13223 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13224 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13225 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13226 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13227 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13228 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13229 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13230 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13231 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13232 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13233 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13234 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13235 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13236 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13237 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13238 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13239 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13240 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13241 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13242 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13243 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13244 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13245 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13246 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13247 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13248 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13249 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13250 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13251 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13252 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13253 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13254 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13255 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13256 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13257 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13258 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13259 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13260 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13261 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13262 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13263 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13264 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13265 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13266 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13267 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13268 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13269 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13270 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13271 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13272 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 13273 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13274 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13275 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13276 | #endif /* ST_RDONLY */ |
| 13277 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13278 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 13279 | #endif /* ST_NOSUID */ |
| 13280 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 13281 | /* GNU extensions */ |
| 13282 | #ifdef ST_NODEV |
| 13283 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 13284 | #endif /* ST_NODEV */ |
| 13285 | #ifdef ST_NOEXEC |
| 13286 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 13287 | #endif /* ST_NOEXEC */ |
| 13288 | #ifdef ST_SYNCHRONOUS |
| 13289 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 13290 | #endif /* ST_SYNCHRONOUS */ |
| 13291 | #ifdef ST_MANDLOCK |
| 13292 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 13293 | #endif /* ST_MANDLOCK */ |
| 13294 | #ifdef ST_WRITE |
| 13295 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 13296 | #endif /* ST_WRITE */ |
| 13297 | #ifdef ST_APPEND |
| 13298 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 13299 | #endif /* ST_APPEND */ |
| 13300 | #ifdef ST_NOATIME |
| 13301 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 13302 | #endif /* ST_NOATIME */ |
| 13303 | #ifdef ST_NODIRATIME |
| 13304 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 13305 | #endif /* ST_NODIRATIME */ |
| 13306 | #ifdef ST_RELATIME |
| 13307 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 13308 | #endif /* ST_RELATIME */ |
| 13309 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13310 | /* FreeBSD sendfile() constants */ |
| 13311 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13312 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13313 | #endif |
| 13314 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13315 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13316 | #endif |
| 13317 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13318 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13319 | #endif |
| 13320 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13321 | /* constants for posix_fadvise */ |
| 13322 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13323 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13324 | #endif |
| 13325 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13326 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13327 | #endif |
| 13328 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13329 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13330 | #endif |
| 13331 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13332 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13333 | #endif |
| 13334 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13335 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13336 | #endif |
| 13337 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13338 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13339 | #endif |
| 13340 | |
| 13341 | /* constants for waitid */ |
| 13342 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13343 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 13344 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 13345 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13346 | #endif |
| 13347 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13348 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13349 | #endif |
| 13350 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13351 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13352 | #endif |
| 13353 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13354 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13355 | #endif |
| 13356 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13357 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13358 | #endif |
| 13359 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13360 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13361 | #endif |
| 13362 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13363 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13364 | #endif |
| 13365 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13366 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13367 | #endif |
| 13368 | |
| 13369 | /* constants for lockf */ |
| 13370 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13371 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13372 | #endif |
| 13373 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13374 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13375 | #endif |
| 13376 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13377 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13378 | #endif |
| 13379 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13380 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13381 | #endif |
| 13382 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13383 | #ifdef RWF_DSYNC |
| 13384 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 13385 | #endif |
| 13386 | #ifdef RWF_HIPRI |
| 13387 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 13388 | #endif |
| 13389 | #ifdef RWF_SYNC |
| 13390 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 13391 | #endif |
| 13392 | #ifdef RWF_NOWAIT |
| 13393 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 13394 | #endif |
| 13395 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13396 | /* constants for posix_spawn */ |
| 13397 | #ifdef HAVE_POSIX_SPAWN |
| 13398 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 13399 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 13400 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 13401 | #endif |
| 13402 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 13403 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13404 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 13405 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 13406 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 13407 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 13408 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 13409 | #endif |
| 13410 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13411 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13412 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13413 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13414 | #endif |
| 13415 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13416 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13417 | #endif |
| 13418 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13419 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 13420 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13421 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 13422 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13423 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13424 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13425 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13426 | #endif |
| 13427 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13428 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13429 | #endif |
| 13430 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13431 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13432 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13433 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13434 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13435 | #endif |
| 13436 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13437 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13438 | #endif |
| 13439 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13440 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13441 | #endif |
| 13442 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13443 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 13444 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13445 | #endif |
| 13446 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 13447 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13448 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 13449 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 13450 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 13451 | #endif |
| 13452 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13453 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13454 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13455 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13456 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13457 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13458 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13459 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13460 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13461 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13462 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13463 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13464 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13465 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13466 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13467 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13468 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13469 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13470 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 13471 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13472 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13473 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 13474 | #if HAVE_DECL_RTLD_MEMBER |
| 13475 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 13476 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 13477 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13478 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13479 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 13480 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 13481 | #endif |
| 13482 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13483 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13484 | } |
| 13485 | |
| 13486 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 13487 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13488 | PyModuleDef_HEAD_INIT, |
| 13489 | MODNAME, |
| 13490 | posix__doc__, |
| 13491 | -1, |
| 13492 | posix_methods, |
| 13493 | NULL, |
| 13494 | NULL, |
| 13495 | NULL, |
| 13496 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 13497 | }; |
| 13498 | |
| 13499 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 13500 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13501 | |
| 13502 | #ifdef HAVE_FACCESSAT |
| 13503 | "HAVE_FACCESSAT", |
| 13504 | #endif |
| 13505 | |
| 13506 | #ifdef HAVE_FCHDIR |
| 13507 | "HAVE_FCHDIR", |
| 13508 | #endif |
| 13509 | |
| 13510 | #ifdef HAVE_FCHMOD |
| 13511 | "HAVE_FCHMOD", |
| 13512 | #endif |
| 13513 | |
| 13514 | #ifdef HAVE_FCHMODAT |
| 13515 | "HAVE_FCHMODAT", |
| 13516 | #endif |
| 13517 | |
| 13518 | #ifdef HAVE_FCHOWN |
| 13519 | "HAVE_FCHOWN", |
| 13520 | #endif |
| 13521 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 13522 | #ifdef HAVE_FCHOWNAT |
| 13523 | "HAVE_FCHOWNAT", |
| 13524 | #endif |
| 13525 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13526 | #ifdef HAVE_FEXECVE |
| 13527 | "HAVE_FEXECVE", |
| 13528 | #endif |
| 13529 | |
| 13530 | #ifdef HAVE_FDOPENDIR |
| 13531 | "HAVE_FDOPENDIR", |
| 13532 | #endif |
| 13533 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 13534 | #ifdef HAVE_FPATHCONF |
| 13535 | "HAVE_FPATHCONF", |
| 13536 | #endif |
| 13537 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13538 | #ifdef HAVE_FSTATAT |
| 13539 | "HAVE_FSTATAT", |
| 13540 | #endif |
| 13541 | |
| 13542 | #ifdef HAVE_FSTATVFS |
| 13543 | "HAVE_FSTATVFS", |
| 13544 | #endif |
| 13545 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 13546 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 13547 | "HAVE_FTRUNCATE", |
| 13548 | #endif |
| 13549 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13550 | #ifdef HAVE_FUTIMENS |
| 13551 | "HAVE_FUTIMENS", |
| 13552 | #endif |
| 13553 | |
| 13554 | #ifdef HAVE_FUTIMES |
| 13555 | "HAVE_FUTIMES", |
| 13556 | #endif |
| 13557 | |
| 13558 | #ifdef HAVE_FUTIMESAT |
| 13559 | "HAVE_FUTIMESAT", |
| 13560 | #endif |
| 13561 | |
| 13562 | #ifdef HAVE_LINKAT |
| 13563 | "HAVE_LINKAT", |
| 13564 | #endif |
| 13565 | |
| 13566 | #ifdef HAVE_LCHFLAGS |
| 13567 | "HAVE_LCHFLAGS", |
| 13568 | #endif |
| 13569 | |
| 13570 | #ifdef HAVE_LCHMOD |
| 13571 | "HAVE_LCHMOD", |
| 13572 | #endif |
| 13573 | |
| 13574 | #ifdef HAVE_LCHOWN |
| 13575 | "HAVE_LCHOWN", |
| 13576 | #endif |
| 13577 | |
| 13578 | #ifdef HAVE_LSTAT |
| 13579 | "HAVE_LSTAT", |
| 13580 | #endif |
| 13581 | |
| 13582 | #ifdef HAVE_LUTIMES |
| 13583 | "HAVE_LUTIMES", |
| 13584 | #endif |
| 13585 | |
| 13586 | #ifdef HAVE_MKDIRAT |
| 13587 | "HAVE_MKDIRAT", |
| 13588 | #endif |
| 13589 | |
| 13590 | #ifdef HAVE_MKFIFOAT |
| 13591 | "HAVE_MKFIFOAT", |
| 13592 | #endif |
| 13593 | |
| 13594 | #ifdef HAVE_MKNODAT |
| 13595 | "HAVE_MKNODAT", |
| 13596 | #endif |
| 13597 | |
| 13598 | #ifdef HAVE_OPENAT |
| 13599 | "HAVE_OPENAT", |
| 13600 | #endif |
| 13601 | |
| 13602 | #ifdef HAVE_READLINKAT |
| 13603 | "HAVE_READLINKAT", |
| 13604 | #endif |
| 13605 | |
| 13606 | #ifdef HAVE_RENAMEAT |
| 13607 | "HAVE_RENAMEAT", |
| 13608 | #endif |
| 13609 | |
| 13610 | #ifdef HAVE_SYMLINKAT |
| 13611 | "HAVE_SYMLINKAT", |
| 13612 | #endif |
| 13613 | |
| 13614 | #ifdef HAVE_UNLINKAT |
| 13615 | "HAVE_UNLINKAT", |
| 13616 | #endif |
| 13617 | |
| 13618 | #ifdef HAVE_UTIMENSAT |
| 13619 | "HAVE_UTIMENSAT", |
| 13620 | #endif |
| 13621 | |
| 13622 | #ifdef MS_WINDOWS |
| 13623 | "MS_WINDOWS", |
| 13624 | #endif |
| 13625 | |
| 13626 | NULL |
| 13627 | }; |
| 13628 | |
| 13629 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 13630 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 13631 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13632 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13633 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13634 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 13635 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13636 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13637 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 13638 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 13639 | #endif |
| 13640 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13641 | m = PyModule_Create(&posixmodule); |
| 13642 | if (m == NULL) |
| 13643 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13644 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13645 | /* Initialize environ dictionary */ |
| 13646 | v = convertenviron(); |
| 13647 | Py_XINCREF(v); |
| 13648 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 13649 | return NULL; |
| 13650 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13651 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13652 | if (all_ins(m)) |
| 13653 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13654 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13655 | if (setup_confname_tables(m)) |
| 13656 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 13657 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13658 | Py_INCREF(PyExc_OSError); |
| 13659 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 13660 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 13661 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13662 | if (posix_putenv_garbage == NULL) |
| 13663 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 13664 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 13665 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13666 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13667 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 13668 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13669 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 13670 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13671 | #endif |
| 13672 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 13673 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13674 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 13675 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 13676 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13677 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 13678 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13679 | structseq_new = StatResultType.tp_new; |
| 13680 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13681 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 13682 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13683 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 13684 | &statvfs_result_desc) < 0) |
| 13685 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13686 | #ifdef NEED_TICKS_PER_SECOND |
| 13687 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13688 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13689 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13690 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13691 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13692 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13693 | # endif |
| 13694 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13695 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 13696 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13697 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13698 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 13699 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13700 | SchedParamType.tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13701 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13702 | |
| 13703 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13704 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 13705 | &TerminalSize_desc) < 0) |
| 13706 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13707 | |
| 13708 | /* initialize scandir types */ |
| 13709 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 13710 | return NULL; |
| 13711 | if (PyType_Ready(&DirEntryType) < 0) |
| 13712 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13713 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13714 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 13715 | Py_INCREF((PyObject*) &WaitidResultType); |
| 13716 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 13717 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13718 | Py_INCREF((PyObject*) &StatResultType); |
| 13719 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 13720 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 13721 | PyModule_AddObject(m, "statvfs_result", |
| 13722 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 13723 | |
| 13724 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13725 | Py_INCREF(&SchedParamType); |
| 13726 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 13727 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13728 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13729 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13730 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 13731 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13732 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 13733 | |
| 13734 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13735 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 13736 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13737 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 13738 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13739 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13740 | /* |
| 13741 | * Step 2 of weak-linking support on Mac OS X. |
| 13742 | * |
| 13743 | * The code below removes functions that are not available on the |
| 13744 | * currently active platform. |
| 13745 | * |
| 13746 | * 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] | 13747 | * 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] | 13748 | * OSX 10.4. |
| 13749 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13750 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13751 | if (fstatvfs == NULL) { |
| 13752 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 13753 | return NULL; |
| 13754 | } |
| 13755 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13756 | #endif /* HAVE_FSTATVFS */ |
| 13757 | |
| 13758 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13759 | if (statvfs == NULL) { |
| 13760 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 13761 | return NULL; |
| 13762 | } |
| 13763 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13764 | #endif /* HAVE_STATVFS */ |
| 13765 | |
| 13766 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13767 | if (lchown == NULL) { |
| 13768 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 13769 | return NULL; |
| 13770 | } |
| 13771 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13772 | #endif /* HAVE_LCHOWN */ |
| 13773 | |
| 13774 | |
| 13775 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13776 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 13777 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13778 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 13779 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 13780 | billion = PyLong_FromLong(1000000000); |
| 13781 | if (!billion) |
| 13782 | return NULL; |
| 13783 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13784 | /* suppress "function not used" warnings */ |
| 13785 | { |
| 13786 | int ignored; |
| 13787 | fd_specified("", -1); |
| 13788 | follow_symlinks_specified("", 1); |
| 13789 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 13790 | dir_fd_converter(Py_None, &ignored); |
| 13791 | dir_fd_unavailable(Py_None, &ignored); |
| 13792 | } |
| 13793 | |
| 13794 | /* |
| 13795 | * provide list of locally available functions |
| 13796 | * so os.py can populate support_* lists |
| 13797 | */ |
| 13798 | list = PyList_New(0); |
| 13799 | if (!list) |
| 13800 | return NULL; |
| 13801 | for (trace = have_functions; *trace; trace++) { |
| 13802 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 13803 | if (!unicode) |
| 13804 | return NULL; |
| 13805 | if (PyList_Append(list, unicode)) |
| 13806 | return NULL; |
| 13807 | Py_DECREF(unicode); |
| 13808 | } |
| 13809 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 13810 | |
| 13811 | Py_INCREF((PyObject *) &DirEntryType); |
Brett Cannon | a32c4d0 | 2016-06-24 14:14:44 -0700 | [diff] [blame] | 13812 | PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13813 | |
| 13814 | initialized = 1; |
| 13815 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13816 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13817 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13818 | |
| 13819 | #ifdef __cplusplus |
| 13820 | } |
| 13821 | #endif |