Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win. In that case the |
| 5 | module actually calls itself 'nt', not 'posix', and a few |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Victor Stinner | f427a14 | 2014-10-22 12:33:23 +0200 | [diff] [blame] | 9 | test macro, e.g. '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11 | |
| 12 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13 | #ifdef __APPLE__ |
| 14 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 17 | * at the end of this file for more information. |
| 18 | */ |
| 19 | # pragma weak lchown |
| 20 | # pragma weak statvfs |
| 21 | # pragma weak fstatvfs |
| 22 | |
| 23 | #endif /* __APPLE__ */ |
| 24 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 25 | #define PY_SSIZE_T_CLEAN |
| 26 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 27 | #include "Python.h" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 28 | #include "structmember.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 29 | #ifndef MS_WINDOWS |
| 30 | #include "posixmodule.h" |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 31 | #else |
| 32 | #include "winreparse.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 33 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 34 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 35 | /* On android API level 21, 'AT_EACCESS' is not declared although |
| 36 | * HAVE_FACCESSAT is defined. */ |
| 37 | #ifdef __ANDROID__ |
| 38 | #undef HAVE_FACCESSAT |
| 39 | #endif |
| 40 | |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | fa76eee | 2016-05-28 21:03:48 +0000 | [diff] [blame] | 41 | #include <stdio.h> /* needed for ctermid() */ |
| 42 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 43 | #ifdef __cplusplus |
| 44 | extern "C" { |
| 45 | #endif |
| 46 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 47 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 48 | "This module provides access to operating system functionality that is\n\ |
| 49 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 50 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 51 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 53 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 54 | #ifdef HAVE_SYS_UIO_H |
| 55 | #include <sys/uio.h> |
| 56 | #endif |
| 57 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 58 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 59 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 60 | #endif /* HAVE_SYS_TYPES_H */ |
| 61 | |
| 62 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 63 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 66 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 67 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 68 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 69 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 70 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 71 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 72 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 | #ifdef HAVE_FCNTL_H |
| 75 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 76 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 78 | #ifdef HAVE_GRP_H |
| 79 | #include <grp.h> |
| 80 | #endif |
| 81 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 82 | #ifdef HAVE_SYSEXITS_H |
| 83 | #include <sysexits.h> |
| 84 | #endif /* HAVE_SYSEXITS_H */ |
| 85 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 86 | #ifdef HAVE_SYS_LOADAVG_H |
| 87 | #include <sys/loadavg.h> |
| 88 | #endif |
| 89 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 90 | #ifdef HAVE_LANGINFO_H |
| 91 | #include <langinfo.h> |
| 92 | #endif |
| 93 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 94 | #ifdef HAVE_SYS_SENDFILE_H |
| 95 | #include <sys/sendfile.h> |
| 96 | #endif |
| 97 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 98 | #ifdef HAVE_SCHED_H |
| 99 | #include <sched.h> |
| 100 | #endif |
| 101 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 102 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 103 | #undef HAVE_SCHED_SETAFFINITY |
| 104 | #endif |
| 105 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 106 | #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] | 107 | #define USE_XATTRS |
| 108 | #endif |
| 109 | |
| 110 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 111 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 112 | #endif |
| 113 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 114 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 115 | #ifdef HAVE_SYS_SOCKET_H |
| 116 | #include <sys/socket.h> |
| 117 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 118 | #endif |
| 119 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 120 | #ifdef HAVE_DLFCN_H |
| 121 | #include <dlfcn.h> |
| 122 | #endif |
| 123 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 124 | #ifdef __hpux |
| 125 | #include <sys/mpctl.h> |
| 126 | #endif |
| 127 | |
| 128 | #if defined(__DragonFly__) || \ |
| 129 | defined(__OpenBSD__) || \ |
| 130 | defined(__FreeBSD__) || \ |
| 131 | defined(__NetBSD__) || \ |
| 132 | defined(__APPLE__) |
| 133 | #include <sys/sysctl.h> |
| 134 | #endif |
| 135 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 136 | #ifdef HAVE_LINUX_RANDOM_H |
| 137 | # include <linux/random.h> |
| 138 | #endif |
| 139 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 140 | # include <sys/syscall.h> |
| 141 | #endif |
| 142 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 143 | #if defined(MS_WINDOWS) |
| 144 | # define TERMSIZE_USE_CONIO |
| 145 | #elif defined(HAVE_SYS_IOCTL_H) |
| 146 | # include <sys/ioctl.h> |
| 147 | # if defined(HAVE_TERMIOS_H) |
| 148 | # include <termios.h> |
| 149 | # endif |
| 150 | # if defined(TIOCGWINSZ) |
| 151 | # define TERMSIZE_USE_IOCTL |
| 152 | # endif |
| 153 | #endif /* MS_WINDOWS */ |
| 154 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 155 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 156 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 157 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 158 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 159 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 160 | #include <process.h> |
| 161 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 162 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 163 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 164 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 165 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 166 | #define HAVE_EXECV 1 |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 167 | #define HAVE_WSPAWNV 1 |
| 168 | #define HAVE_WEXECV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 169 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 170 | #define HAVE_SYSTEM 1 |
| 171 | #define HAVE_CWAIT 1 |
| 172 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 173 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 174 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | /* Unix functions that the configure script doesn't check for */ |
| 176 | #define HAVE_EXECV 1 |
| 177 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 178 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 179 | #define HAVE_FORK1 1 |
| 180 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 181 | #define HAVE_GETEGID 1 |
| 182 | #define HAVE_GETEUID 1 |
| 183 | #define HAVE_GETGID 1 |
| 184 | #define HAVE_GETPPID 1 |
| 185 | #define HAVE_GETUID 1 |
| 186 | #define HAVE_KILL 1 |
| 187 | #define HAVE_OPENDIR 1 |
| 188 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 189 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 190 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 191 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 192 | #endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 193 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 194 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 195 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 196 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 197 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 198 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 199 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 200 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 201 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 202 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 203 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 204 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 205 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 206 | (default) */ |
| 207 | extern char *ctermid_r(char *); |
| 208 | #endif |
| 209 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 210 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 211 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 212 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 213 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 214 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 215 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 216 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 217 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 218 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 219 | #endif |
| 220 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 221 | extern int chdir(char *); |
| 222 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 223 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 224 | extern int chdir(const char *); |
| 225 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 226 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 227 | extern int chmod(const char *, mode_t); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 228 | /*#ifdef HAVE_FCHMOD |
| 229 | extern int fchmod(int, mode_t); |
| 230 | #endif*/ |
| 231 | /*#ifdef HAVE_LCHMOD |
| 232 | extern int lchmod(const char *, mode_t); |
| 233 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 234 | extern int chown(const char *, uid_t, gid_t); |
| 235 | extern char *getcwd(char *, int); |
| 236 | extern char *strerror(int); |
| 237 | extern int link(const char *, const char *); |
| 238 | extern int rename(const char *, const char *); |
| 239 | extern int stat(const char *, struct stat *); |
| 240 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 242 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 243 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 244 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 245 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 246 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 249 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #ifdef HAVE_UTIME_H |
| 252 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 253 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 255 | #ifdef HAVE_SYS_UTIME_H |
| 256 | #include <sys/utime.h> |
| 257 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 258 | #endif /* HAVE_SYS_UTIME_H */ |
| 259 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #ifdef HAVE_SYS_TIMES_H |
| 261 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 262 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | |
| 264 | #ifdef HAVE_SYS_PARAM_H |
| 265 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 266 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | |
| 268 | #ifdef HAVE_SYS_UTSNAME_H |
| 269 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 270 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 272 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 274 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 275 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 276 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 277 | #include <direct.h> |
| 278 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 279 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 280 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 281 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 282 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 283 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 284 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 285 | #endif |
| 286 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 287 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 288 | #endif |
| 289 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 290 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 291 | #endif |
| 292 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 293 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 294 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 295 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 296 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 297 | #endif |
| 298 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 299 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 300 | #endif |
| 301 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 302 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 303 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 304 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 305 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 306 | #endif |
| 307 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 308 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 309 | #endif |
| 310 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 311 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 312 | #endif |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 313 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 314 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 315 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 316 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 317 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 318 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 319 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 320 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 321 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 322 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 323 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 324 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 325 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 326 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 327 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 328 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 329 | #define MAXPATHLEN PATH_MAX |
| 330 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 331 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 332 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 333 | #endif /* MAXPATHLEN */ |
| 334 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 335 | #ifdef UNION_WAIT |
| 336 | /* Emulate some macros on systems that have a union instead of macros */ |
| 337 | |
| 338 | #ifndef WIFEXITED |
| 339 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 340 | #endif |
| 341 | |
| 342 | #ifndef WEXITSTATUS |
| 343 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 344 | #endif |
| 345 | |
| 346 | #ifndef WTERMSIG |
| 347 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 348 | #endif |
| 349 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 350 | #define WAIT_TYPE union wait |
| 351 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 352 | |
| 353 | #else /* !UNION_WAIT */ |
| 354 | #define WAIT_TYPE int |
| 355 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 356 | #endif /* UNION_WAIT */ |
| 357 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 358 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 359 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 360 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 361 | #define USE_CTERMID_R |
| 362 | #endif |
| 363 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 364 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 365 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 366 | #undef FSTAT |
| 367 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 368 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 369 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 370 | # define LSTAT win32_lstat |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 371 | # define FSTAT _Py_fstat_noraise |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 372 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 373 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 374 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 375 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 376 | # define FSTAT fstat |
| 377 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 378 | #endif |
| 379 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 380 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 381 | #include <sys/mkdev.h> |
| 382 | #else |
| 383 | #if defined(MAJOR_IN_SYSMACROS) |
| 384 | #include <sys/sysmacros.h> |
| 385 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 386 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 387 | #include <sys/mkdev.h> |
| 388 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 389 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 390 | |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 391 | #define DWORD_MAX 4294967295U |
| 392 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 393 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 394 | #define INITFUNC PyInit_nt |
| 395 | #define MODNAME "nt" |
| 396 | #else |
| 397 | #define INITFUNC PyInit_posix |
| 398 | #define MODNAME "posix" |
| 399 | #endif |
| 400 | |
| 401 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 402 | /* defined in fileutils.c */ |
| 403 | PyAPI_FUNC(void) _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 404 | PyAPI_FUNC(void) _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
| 405 | ULONG, struct _Py_stat_struct *); |
| 406 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 407 | |
| 408 | #ifdef MS_WINDOWS |
| 409 | static int |
| 410 | win32_warn_bytes_api() |
| 411 | { |
| 412 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 413 | "The Windows bytes API has been deprecated, " |
| 414 | "use Unicode filenames instead", |
| 415 | 1); |
| 416 | } |
| 417 | #endif |
| 418 | |
| 419 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 420 | #ifndef MS_WINDOWS |
| 421 | PyObject * |
| 422 | _PyLong_FromUid(uid_t uid) |
| 423 | { |
| 424 | if (uid == (uid_t)-1) |
| 425 | return PyLong_FromLong(-1); |
| 426 | return PyLong_FromUnsignedLong(uid); |
| 427 | } |
| 428 | |
| 429 | PyObject * |
| 430 | _PyLong_FromGid(gid_t gid) |
| 431 | { |
| 432 | if (gid == (gid_t)-1) |
| 433 | return PyLong_FromLong(-1); |
| 434 | return PyLong_FromUnsignedLong(gid); |
| 435 | } |
| 436 | |
| 437 | int |
| 438 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 439 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 440 | uid_t uid; |
| 441 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 442 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 443 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 444 | unsigned long uresult; |
| 445 | |
| 446 | index = PyNumber_Index(obj); |
| 447 | if (index == NULL) { |
| 448 | PyErr_Format(PyExc_TypeError, |
| 449 | "uid should be integer, not %.200s", |
| 450 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 451 | return 0; |
| 452 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 453 | |
| 454 | /* |
| 455 | * Handling uid_t is complicated for two reasons: |
| 456 | * * Although uid_t is (always?) unsigned, it still |
| 457 | * accepts -1. |
| 458 | * * We don't know its size in advance--it may be |
| 459 | * bigger than an int, or it may be smaller than |
| 460 | * a long. |
| 461 | * |
| 462 | * So a bit of defensive programming is in order. |
| 463 | * Start with interpreting the value passed |
| 464 | * in as a signed long and see if it works. |
| 465 | */ |
| 466 | |
| 467 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 468 | |
| 469 | if (!overflow) { |
| 470 | uid = (uid_t)result; |
| 471 | |
| 472 | if (result == -1) { |
| 473 | if (PyErr_Occurred()) |
| 474 | goto fail; |
| 475 | /* It's a legitimate -1, we're done. */ |
| 476 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 477 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 478 | |
| 479 | /* Any other negative number is disallowed. */ |
| 480 | if (result < 0) |
| 481 | goto underflow; |
| 482 | |
| 483 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 484 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 485 | (long)uid != result) |
| 486 | goto underflow; |
| 487 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 488 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 489 | |
| 490 | if (overflow < 0) |
| 491 | goto underflow; |
| 492 | |
| 493 | /* |
| 494 | * Okay, the value overflowed a signed long. If it |
| 495 | * fits in an *unsigned* long, it may still be okay, |
| 496 | * as uid_t may be unsigned long on this platform. |
| 497 | */ |
| 498 | uresult = PyLong_AsUnsignedLong(index); |
| 499 | if (PyErr_Occurred()) { |
| 500 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 501 | goto overflow; |
| 502 | goto fail; |
| 503 | } |
| 504 | |
| 505 | uid = (uid_t)uresult; |
| 506 | |
| 507 | /* |
| 508 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 509 | * but this value would get interpreted as (uid_t)-1 by chown |
| 510 | * and its siblings. That's not what the user meant! So we |
| 511 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 512 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 513 | */ |
| 514 | if (uid == (uid_t)-1) |
| 515 | goto overflow; |
| 516 | |
| 517 | /* Ensure the value wasn't truncated. */ |
| 518 | if (sizeof(uid_t) < sizeof(long) && |
| 519 | (unsigned long)uid != uresult) |
| 520 | goto overflow; |
| 521 | /* fallthrough */ |
| 522 | |
| 523 | success: |
| 524 | Py_DECREF(index); |
| 525 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 526 | return 1; |
| 527 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 528 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 529 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 530 | "uid is less than minimum"); |
| 531 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 532 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 533 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 534 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 535 | "uid is greater than maximum"); |
| 536 | /* fallthrough */ |
| 537 | |
| 538 | fail: |
| 539 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | int |
| 544 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 545 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 546 | gid_t gid; |
| 547 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 548 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 549 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 550 | unsigned long uresult; |
| 551 | |
| 552 | index = PyNumber_Index(obj); |
| 553 | if (index == NULL) { |
| 554 | PyErr_Format(PyExc_TypeError, |
| 555 | "gid should be integer, not %.200s", |
| 556 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 557 | return 0; |
| 558 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 559 | |
| 560 | /* |
| 561 | * Handling gid_t is complicated for two reasons: |
| 562 | * * Although gid_t is (always?) unsigned, it still |
| 563 | * accepts -1. |
| 564 | * * We don't know its size in advance--it may be |
| 565 | * bigger than an int, or it may be smaller than |
| 566 | * a long. |
| 567 | * |
| 568 | * So a bit of defensive programming is in order. |
| 569 | * Start with interpreting the value passed |
| 570 | * in as a signed long and see if it works. |
| 571 | */ |
| 572 | |
| 573 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 574 | |
| 575 | if (!overflow) { |
| 576 | gid = (gid_t)result; |
| 577 | |
| 578 | if (result == -1) { |
| 579 | if (PyErr_Occurred()) |
| 580 | goto fail; |
| 581 | /* It's a legitimate -1, we're done. */ |
| 582 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 583 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 584 | |
| 585 | /* Any other negative number is disallowed. */ |
| 586 | if (result < 0) { |
| 587 | goto underflow; |
| 588 | } |
| 589 | |
| 590 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 591 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 592 | (long)gid != result) |
| 593 | goto underflow; |
| 594 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 595 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 596 | |
| 597 | if (overflow < 0) |
| 598 | goto underflow; |
| 599 | |
| 600 | /* |
| 601 | * Okay, the value overflowed a signed long. If it |
| 602 | * fits in an *unsigned* long, it may still be okay, |
| 603 | * as gid_t may be unsigned long on this platform. |
| 604 | */ |
| 605 | uresult = PyLong_AsUnsignedLong(index); |
| 606 | if (PyErr_Occurred()) { |
| 607 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 608 | goto overflow; |
| 609 | goto fail; |
| 610 | } |
| 611 | |
| 612 | gid = (gid_t)uresult; |
| 613 | |
| 614 | /* |
| 615 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 616 | * but this value would get interpreted as (gid_t)-1 by chown |
| 617 | * and its siblings. That's not what the user meant! So we |
| 618 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 619 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 620 | */ |
| 621 | if (gid == (gid_t)-1) |
| 622 | goto overflow; |
| 623 | |
| 624 | /* Ensure the value wasn't truncated. */ |
| 625 | if (sizeof(gid_t) < sizeof(long) && |
| 626 | (unsigned long)gid != uresult) |
| 627 | goto overflow; |
| 628 | /* fallthrough */ |
| 629 | |
| 630 | success: |
| 631 | Py_DECREF(index); |
| 632 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 633 | return 1; |
| 634 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 635 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 636 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 637 | "gid is less than minimum"); |
| 638 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 639 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 640 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 641 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 642 | "gid is greater than maximum"); |
| 643 | /* fallthrough */ |
| 644 | |
| 645 | fail: |
| 646 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 647 | return 0; |
| 648 | } |
| 649 | #endif /* MS_WINDOWS */ |
| 650 | |
| 651 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 652 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 653 | |
| 654 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 655 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 656 | static int |
| 657 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 658 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 659 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 660 | if (PyErr_Occurred()) |
| 661 | return 0; |
| 662 | return 1; |
| 663 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 664 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 665 | |
| 666 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 667 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 668 | /* |
| 669 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 670 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 671 | * which doesn't play nicely with all the initializer lines in this file that |
| 672 | * look like this: |
| 673 | * int dir_fd = DEFAULT_DIR_FD; |
| 674 | */ |
| 675 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 676 | #else |
| 677 | #define DEFAULT_DIR_FD (-100) |
| 678 | #endif |
| 679 | |
| 680 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 681 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 682 | { |
| 683 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 684 | long long_value; |
| 685 | |
| 686 | PyObject *index = PyNumber_Index(o); |
| 687 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 688 | return 0; |
| 689 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 690 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 691 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 692 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 693 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 694 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 695 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 696 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 697 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 698 | return 0; |
| 699 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 700 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 701 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 702 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 703 | return 0; |
| 704 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 705 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 706 | *p = (int)long_value; |
| 707 | return 1; |
| 708 | } |
| 709 | |
| 710 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 711 | dir_fd_converter(PyObject *o, void *p) |
| 712 | { |
| 713 | if (o == Py_None) { |
| 714 | *(int *)p = DEFAULT_DIR_FD; |
| 715 | return 1; |
| 716 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 717 | else if (PyIndex_Check(o)) { |
| 718 | return _fd_converter(o, (int *)p); |
| 719 | } |
| 720 | else { |
| 721 | PyErr_Format(PyExc_TypeError, |
| 722 | "argument should be integer or None, not %.200s", |
| 723 | Py_TYPE(o)->tp_name); |
| 724 | return 0; |
| 725 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 729 | /* |
| 730 | * A PyArg_ParseTuple "converter" function |
| 731 | * that handles filesystem paths in the manner |
| 732 | * preferred by the os module. |
| 733 | * |
| 734 | * path_converter accepts (Unicode) strings and their |
| 735 | * subclasses, and bytes and their subclasses. What |
| 736 | * it does with the argument depends on the platform: |
| 737 | * |
| 738 | * * On Windows, if we get a (Unicode) string we |
| 739 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 740 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 741 | * |
| 742 | * * On all other platforms, strings are encoded |
| 743 | * to bytes using PyUnicode_FSConverter, then we |
| 744 | * extract the char * from the bytes object and |
| 745 | * return that. |
| 746 | * |
| 747 | * path_converter also optionally accepts signed |
| 748 | * integers (representing open file descriptors) instead |
| 749 | * of path strings. |
| 750 | * |
| 751 | * Input fields: |
| 752 | * path.nullable |
| 753 | * If nonzero, the path is permitted to be None. |
| 754 | * path.allow_fd |
| 755 | * If nonzero, the path is permitted to be a file handle |
| 756 | * (a signed int) instead of a string. |
| 757 | * path.function_name |
| 758 | * If non-NULL, path_converter will use that as the name |
| 759 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 760 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 761 | * path.argument_name |
| 762 | * If non-NULL, path_converter will use that as the name |
| 763 | * of the parameter in error messages. |
| 764 | * (If path.argument_name is NULL it uses "path".) |
| 765 | * |
| 766 | * Output fields: |
| 767 | * path.wide |
| 768 | * Points to the path if it was expressed as Unicode |
| 769 | * and was not encoded. (Only used on Windows.) |
| 770 | * path.narrow |
| 771 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 772 | * or it was Unicode and was encoded to bytes. (On Windows, |
| 773 | * is an non-zero integer if the path was expressed as bytes. |
| 774 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 775 | * path.fd |
| 776 | * Contains a file descriptor if path.accept_fd was true |
| 777 | * and the caller provided a signed integer instead of any |
| 778 | * sort of string. |
| 779 | * |
| 780 | * WARNING: if your "path" parameter is optional, and is |
| 781 | * unspecified, path_converter will never get called. |
| 782 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 783 | * yourself! |
| 784 | * path.length |
| 785 | * The length of the path in characters, if specified as |
| 786 | * a string. |
| 787 | * path.object |
| 788 | * The original object passed in. |
| 789 | * path.cleanup |
| 790 | * For internal use only. May point to a temporary object. |
| 791 | * (Pay no attention to the man behind the curtain.) |
| 792 | * |
| 793 | * At most one of path.wide or path.narrow will be non-NULL. |
| 794 | * If path was None and path.nullable was set, |
| 795 | * or if path was an integer and path.allow_fd was set, |
| 796 | * both path.wide and path.narrow will be NULL |
| 797 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 798 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 799 | * path_converter takes care to not write to the path_t |
| 800 | * unless it's successful. However it must reset the |
| 801 | * "cleanup" field each time it's called. |
| 802 | * |
| 803 | * Use as follows: |
| 804 | * path_t path; |
| 805 | * memset(&path, 0, sizeof(path)); |
| 806 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 807 | * // ... use values from path ... |
| 808 | * path_cleanup(&path); |
| 809 | * |
| 810 | * (Note that if PyArg_Parse fails you don't need to call |
| 811 | * path_cleanup(). However it is safe to do so.) |
| 812 | */ |
| 813 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 814 | const char *function_name; |
| 815 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 816 | int nullable; |
| 817 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 818 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 819 | #ifdef MS_WINDOWS |
| 820 | BOOL narrow; |
| 821 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 822 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 823 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 824 | int fd; |
| 825 | Py_ssize_t length; |
| 826 | PyObject *object; |
| 827 | PyObject *cleanup; |
| 828 | } path_t; |
| 829 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 830 | #ifdef MS_WINDOWS |
| 831 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 832 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 833 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 834 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 835 | {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] | 836 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 837 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 838 | static void |
| 839 | path_cleanup(path_t *path) { |
| 840 | if (path->cleanup) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 841 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
| 845 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 846 | path_converter(PyObject *o, void *p) |
| 847 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 848 | path_t *path = (path_t *)p; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 849 | PyObject *bytes, *to_cleanup = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 850 | Py_ssize_t length; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 851 | int is_index, is_buffer, is_bytes, is_unicode; |
| 852 | /* Default to failure, forcing explicit signaling of succcess. */ |
| 853 | int ret = 0; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 854 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 855 | #ifdef MS_WINDOWS |
| 856 | PyObject *wo; |
| 857 | const wchar_t *wide; |
| 858 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 859 | |
| 860 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 861 | PyErr_Format(exc, "%s%s" fmt, \ |
| 862 | path->function_name ? path->function_name : "", \ |
| 863 | path->function_name ? ": " : "", \ |
| 864 | path->argument_name ? path->argument_name : "path") |
| 865 | |
| 866 | /* Py_CLEANUP_SUPPORTED support */ |
| 867 | if (o == NULL) { |
| 868 | path_cleanup(path); |
| 869 | return 1; |
| 870 | } |
| 871 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 872 | /* Ensure it's always safe to call path_cleanup(). */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 873 | path->cleanup = NULL; |
| 874 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 875 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 876 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 877 | #ifdef MS_WINDOWS |
| 878 | path->narrow = FALSE; |
| 879 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 880 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 881 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 882 | path->length = 0; |
| 883 | path->object = o; |
| 884 | path->fd = -1; |
| 885 | return 1; |
| 886 | } |
| 887 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 888 | /* Only call this here so that we don't treat the return value of |
| 889 | os.fspath() as an fd or buffer. */ |
| 890 | is_index = path->allow_fd && PyIndex_Check(o); |
| 891 | is_buffer = PyObject_CheckBuffer(o); |
| 892 | is_bytes = PyBytes_Check(o); |
| 893 | is_unicode = PyUnicode_Check(o); |
| 894 | |
| 895 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 896 | /* Inline PyOS_FSPath() for better error messages. */ |
| 897 | _Py_IDENTIFIER(__fspath__); |
| 898 | PyObject *func = NULL; |
| 899 | |
| 900 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 901 | if (NULL == func) { |
| 902 | goto error_exit; |
| 903 | } |
| 904 | |
| 905 | o = to_cleanup = PyObject_CallFunctionObjArgs(func, NULL); |
| 906 | Py_DECREF(func); |
| 907 | if (NULL == o) { |
| 908 | goto error_exit; |
| 909 | } |
| 910 | else if (PyUnicode_Check(o)) { |
| 911 | is_unicode = 1; |
| 912 | } |
| 913 | else if (PyBytes_Check(o)) { |
| 914 | is_bytes = 1; |
| 915 | } |
| 916 | else { |
| 917 | goto error_exit; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 922 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 923 | wide = PyUnicode_AsWideCharString(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 924 | if (!wide) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 925 | goto exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 926 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 927 | if (length > 32767) { |
| 928 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 929 | goto exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 930 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 931 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 932 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 933 | goto exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 934 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 935 | |
| 936 | path->wide = wide; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 937 | path->length = length; |
| 938 | path->object = o; |
| 939 | path->fd = -1; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 940 | ret = 1; |
| 941 | goto exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 942 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 943 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 944 | goto exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 945 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 946 | #endif |
| 947 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 948 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 949 | bytes = o; |
| 950 | Py_INCREF(bytes); |
| 951 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 952 | else if (is_buffer) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 953 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 954 | "%s%s%s should be %s, not %.200s", |
| 955 | path->function_name ? path->function_name : "", |
| 956 | path->function_name ? ": " : "", |
| 957 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 958 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 959 | "integer or None" : |
| 960 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 961 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 962 | "string, bytes or os.PathLike", |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 963 | Py_TYPE(o)->tp_name)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 964 | goto exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 965 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 966 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 967 | if (!bytes) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 968 | goto exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 969 | } |
| 970 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 971 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 972 | if (!_fd_converter(o, &path->fd)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 973 | goto exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 974 | } |
| 975 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 976 | #ifdef MS_WINDOWS |
| 977 | path->narrow = FALSE; |
| 978 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 979 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 980 | #endif |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 981 | path->length = 0; |
| 982 | path->object = o; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 983 | ret = 1; |
| 984 | goto exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 985 | } |
| 986 | else { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 987 | error_exit: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 988 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 989 | path->function_name ? path->function_name : "", |
| 990 | path->function_name ? ": " : "", |
| 991 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 992 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 993 | "integer or None" : |
| 994 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 995 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 996 | "string, bytes or os.PathLike", |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 997 | Py_TYPE(o)->tp_name); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 998 | goto exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1001 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1002 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1003 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1004 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1005 | Py_DECREF(bytes); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1006 | goto exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1009 | #ifdef MS_WINDOWS |
| 1010 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1011 | narrow, |
| 1012 | length |
| 1013 | ); |
| 1014 | if (!wo) { |
| 1015 | goto exit; |
| 1016 | } |
| 1017 | |
| 1018 | wide = PyUnicode_AsWideCharString(wo, &length); |
| 1019 | Py_DECREF(wo); |
| 1020 | |
| 1021 | if (!wide) { |
| 1022 | goto exit; |
| 1023 | } |
| 1024 | if (length > 32767) { |
| 1025 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 1026 | goto exit; |
| 1027 | } |
| 1028 | if (wcslen(wide) != length) { |
| 1029 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
| 1030 | goto exit; |
| 1031 | } |
| 1032 | path->wide = wide; |
| 1033 | path->narrow = TRUE; |
| 1034 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1035 | path->wide = NULL; |
| 1036 | path->narrow = narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1037 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1038 | path->length = length; |
| 1039 | path->object = o; |
| 1040 | path->fd = -1; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1041 | if (bytes == o) { |
| 1042 | Py_DECREF(bytes); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1043 | ret = 1; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1044 | } |
| 1045 | else { |
| 1046 | path->cleanup = bytes; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1047 | ret = Py_CLEANUP_SUPPORTED; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1048 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1049 | exit: |
| 1050 | Py_XDECREF(to_cleanup); |
| 1051 | return ret; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1055 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1056 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1057 | PyErr_Format(PyExc_NotImplementedError, |
| 1058 | "%s%s%s unavailable on this platform", |
| 1059 | (function_name != NULL) ? function_name : "", |
| 1060 | (function_name != NULL) ? ": ": "", |
| 1061 | argument_name); |
| 1062 | } |
| 1063 | |
| 1064 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1065 | dir_fd_unavailable(PyObject *o, void *p) |
| 1066 | { |
| 1067 | int dir_fd; |
| 1068 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1069 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1070 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1071 | argument_unavailable_error(NULL, "dir_fd"); |
| 1072 | return 0; |
| 1073 | } |
| 1074 | *(int *)p = dir_fd; |
| 1075 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1079 | fd_specified(const char *function_name, int fd) |
| 1080 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1081 | if (fd == -1) |
| 1082 | return 0; |
| 1083 | |
| 1084 | argument_unavailable_error(function_name, "fd"); |
| 1085 | return 1; |
| 1086 | } |
| 1087 | |
| 1088 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1089 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1090 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1091 | if (follow_symlinks) |
| 1092 | return 0; |
| 1093 | |
| 1094 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1095 | return 1; |
| 1096 | } |
| 1097 | |
| 1098 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1099 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1100 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1101 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1102 | #ifndef MS_WINDOWS |
| 1103 | && !path->narrow |
| 1104 | #endif |
| 1105 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1106 | PyErr_Format(PyExc_ValueError, |
| 1107 | "%s: can't specify dir_fd without matching path", |
| 1108 | function_name); |
| 1109 | return 1; |
| 1110 | } |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1115 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1116 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1117 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1118 | PyErr_Format(PyExc_ValueError, |
| 1119 | "%s: can't specify both dir_fd and fd", |
| 1120 | function_name); |
| 1121 | return 1; |
| 1122 | } |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1127 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1128 | int follow_symlinks) |
| 1129 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1130 | if ((fd > 0) && (!follow_symlinks)) { |
| 1131 | PyErr_Format(PyExc_ValueError, |
| 1132 | "%s: cannot use fd and follow_symlinks together", |
| 1133 | function_name); |
| 1134 | return 1; |
| 1135 | } |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1140 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1141 | int follow_symlinks) |
| 1142 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1143 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1144 | PyErr_Format(PyExc_ValueError, |
| 1145 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1146 | function_name); |
| 1147 | return 1; |
| 1148 | } |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1152 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1153 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1154 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1155 | typedef off_t Py_off_t; |
| 1156 | #endif |
| 1157 | |
| 1158 | static int |
| 1159 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1160 | { |
| 1161 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1162 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1163 | #else |
| 1164 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1165 | #endif |
| 1166 | if (PyErr_Occurred()) |
| 1167 | return 0; |
| 1168 | return 1; |
| 1169 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1170 | |
| 1171 | static PyObject * |
| 1172 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1173 | { |
| 1174 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1175 | return PyLong_FromLongLong(offset); |
| 1176 | #else |
| 1177 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1178 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1179 | } |
| 1180 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1181 | |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 1182 | #if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900 |
| 1183 | /* Legacy implementation of _PyVerify_fd_dup2 while transitioning to |
| 1184 | * MSVC 14.0. This should eventually be removed. (issue23524) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1185 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1186 | #define IOINFO_L2E 5 |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1187 | #define IOINFO_ARRAYS 64 |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 1188 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1189 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1190 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 1191 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1192 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 1193 | static int |
| 1194 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 1195 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1196 | if (!_PyVerify_fd(fd1)) |
| 1197 | return 0; |
| 1198 | if (fd2 == _NO_CONSOLE_FILENO) |
| 1199 | return 0; |
| 1200 | if ((unsigned)fd2 < _NHANDLE_) |
| 1201 | return 1; |
| 1202 | else |
| 1203 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1204 | } |
| 1205 | #else |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 1206 | #define _PyVerify_fd_dup2(fd1, fd2) (_PyVerify_fd(fd1) && (fd2) >= 0) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1207 | #endif |
| 1208 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1209 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1210 | |
| 1211 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1212 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1213 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1214 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1215 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1216 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1217 | |
| 1218 | if (0 == DeviceIoControl( |
| 1219 | reparse_point_handle, |
| 1220 | FSCTL_GET_REPARSE_POINT, |
| 1221 | NULL, 0, /* in buffer */ |
| 1222 | target_buffer, sizeof(target_buffer), |
| 1223 | &n_bytes_returned, |
| 1224 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1225 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1226 | |
| 1227 | if (reparse_tag) |
| 1228 | *reparse_tag = rdb->ReparseTag; |
| 1229 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1230 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1231 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1232 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1233 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1234 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1235 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1236 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1237 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1238 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1239 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1240 | */ |
| 1241 | #include <crt_externs.h> |
| 1242 | static char **environ; |
| 1243 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1244 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1245 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1246 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1247 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1248 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1249 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1250 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1251 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1252 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1253 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1254 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1255 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1256 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1257 | d = PyDict_New(); |
| 1258 | if (d == NULL) |
| 1259 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1260 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1261 | if (environ == NULL) |
| 1262 | environ = *_NSGetEnviron(); |
| 1263 | #endif |
| 1264 | #ifdef MS_WINDOWS |
| 1265 | /* _wenviron must be initialized in this way if the program is started |
| 1266 | through main() instead of wmain(). */ |
| 1267 | _wgetenv(L""); |
| 1268 | if (_wenviron == NULL) |
| 1269 | return d; |
| 1270 | /* This part ignores errors */ |
| 1271 | for (e = _wenviron; *e != NULL; e++) { |
| 1272 | PyObject *k; |
| 1273 | PyObject *v; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1274 | const wchar_t *p = wcschr(*e, L'='); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1275 | if (p == NULL) |
| 1276 | continue; |
| 1277 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1278 | if (k == NULL) { |
| 1279 | PyErr_Clear(); |
| 1280 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1281 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1282 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1283 | if (v == NULL) { |
| 1284 | PyErr_Clear(); |
| 1285 | Py_DECREF(k); |
| 1286 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1287 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1288 | if (PyDict_GetItem(d, k) == NULL) { |
| 1289 | if (PyDict_SetItem(d, k, v) != 0) |
| 1290 | PyErr_Clear(); |
| 1291 | } |
| 1292 | Py_DECREF(k); |
| 1293 | Py_DECREF(v); |
| 1294 | } |
| 1295 | #else |
| 1296 | if (environ == NULL) |
| 1297 | return d; |
| 1298 | /* This part ignores errors */ |
| 1299 | for (e = environ; *e != NULL; e++) { |
| 1300 | PyObject *k; |
| 1301 | PyObject *v; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1302 | const char *p = strchr(*e, '='); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1303 | if (p == NULL) |
| 1304 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1305 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1306 | if (k == NULL) { |
| 1307 | PyErr_Clear(); |
| 1308 | continue; |
| 1309 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1310 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1311 | if (v == NULL) { |
| 1312 | PyErr_Clear(); |
| 1313 | Py_DECREF(k); |
| 1314 | continue; |
| 1315 | } |
| 1316 | if (PyDict_GetItem(d, k) == NULL) { |
| 1317 | if (PyDict_SetItem(d, k, v) != 0) |
| 1318 | PyErr_Clear(); |
| 1319 | } |
| 1320 | Py_DECREF(k); |
| 1321 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1322 | } |
| 1323 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1324 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1327 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1328 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1329 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1330 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1331 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1332 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1333 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1334 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1335 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1336 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1337 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1338 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1339 | /* XXX We should pass the function name along in the future. |
| 1340 | (winreg.c also wants to pass the function name.) |
| 1341 | This would however require an additional param to the |
| 1342 | Windows error object, which is non-trivial. |
| 1343 | */ |
| 1344 | errno = GetLastError(); |
| 1345 | if (filename) |
| 1346 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1347 | else |
| 1348 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1349 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1350 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1351 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1352 | win32_error_object(const char* function, PyObject* filename) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1353 | { |
| 1354 | /* XXX - see win32_error for comments on 'function' */ |
| 1355 | errno = GetLastError(); |
| 1356 | if (filename) |
| 1357 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1358 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1359 | errno, |
| 1360 | filename); |
| 1361 | else |
| 1362 | return PyErr_SetFromWindowsErr(errno); |
| 1363 | } |
| 1364 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1365 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1366 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1367 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1368 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1369 | { |
| 1370 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1371 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1372 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1373 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1374 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1375 | #endif |
| 1376 | } |
| 1377 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1378 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1379 | static PyObject * |
| 1380 | path_error2(path_t *path, path_t *path2) |
| 1381 | { |
| 1382 | #ifdef MS_WINDOWS |
| 1383 | return PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError, |
| 1384 | 0, path->object, path2->object); |
| 1385 | #else |
| 1386 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, |
| 1387 | path->object, path2->object); |
| 1388 | #endif |
| 1389 | } |
| 1390 | |
| 1391 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1392 | /* POSIX generic methods */ |
| 1393 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1394 | static int |
| 1395 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1396 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1397 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1398 | int *pointer = (int *)p; |
| 1399 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1400 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1401 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1402 | *pointer = fd; |
| 1403 | return 1; |
| 1404 | } |
| 1405 | |
| 1406 | static PyObject * |
| 1407 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1408 | { |
| 1409 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1410 | int async_err = 0; |
| 1411 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1412 | if (!_PyVerify_fd(fd)) |
| 1413 | return posix_error(); |
| 1414 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1415 | do { |
| 1416 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1417 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1418 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1419 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1420 | Py_END_ALLOW_THREADS |
| 1421 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1422 | if (res != 0) |
| 1423 | return (!async_err) ? posix_error() : NULL; |
| 1424 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1425 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1426 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1427 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1428 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1429 | /* This is a reimplementation of the C library's chdir function, |
| 1430 | but one that produces Win32 errors instead of DOS error codes. |
| 1431 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1432 | it also needs to set "magic" environment variables indicating |
| 1433 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1434 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1435 | win32_wchdir(LPCWSTR path) |
| 1436 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1437 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1438 | int result; |
| 1439 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1440 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1441 | if(!SetCurrentDirectoryW(path)) |
| 1442 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1443 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1444 | if (!result) |
| 1445 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1446 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1447 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1448 | if (!new_path) { |
| 1449 | SetLastError(ERROR_OUTOFMEMORY); |
| 1450 | return FALSE; |
| 1451 | } |
| 1452 | result = GetCurrentDirectoryW(result, new_path); |
| 1453 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1454 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1455 | return FALSE; |
| 1456 | } |
| 1457 | } |
| 1458 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1459 | wcsncmp(new_path, L"//", 2) == 0) |
| 1460 | /* UNC path, nothing to do. */ |
| 1461 | return TRUE; |
| 1462 | env[1] = new_path[0]; |
| 1463 | result = SetEnvironmentVariableW(env, new_path); |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1464 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1465 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1466 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1467 | } |
| 1468 | #endif |
| 1469 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1470 | #ifdef MS_WINDOWS |
| 1471 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1472 | - time stamps are restricted to second resolution |
| 1473 | - file modification times suffer from forth-and-back conversions between |
| 1474 | UTC and local time |
| 1475 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1476 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1477 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1478 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1479 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1480 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1481 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1482 | BY_HANDLE_FILE_INFORMATION *info, |
| 1483 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1484 | { |
| 1485 | memset(info, 0, sizeof(*info)); |
| 1486 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1487 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1488 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1489 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1490 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1491 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1492 | /* info->nNumberOfLinks = 1; */ |
| 1493 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1494 | *reparse_tag = pFileData->dwReserved0; |
| 1495 | else |
| 1496 | *reparse_tag = 0; |
| 1497 | } |
| 1498 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1499 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1500 | 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] | 1501 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1502 | HANDLE hFindFile; |
| 1503 | WIN32_FIND_DATAW FileData; |
| 1504 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1505 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1506 | return FALSE; |
| 1507 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1508 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1509 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1512 | static BOOL |
| 1513 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1514 | { |
| 1515 | int buf_size, result_length; |
| 1516 | wchar_t *buf; |
| 1517 | |
| 1518 | /* We have a good handle to the target, use it to determine |
| 1519 | the target path name (then we'll call lstat on it). */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1520 | buf_size = GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1521 | VOLUME_NAME_DOS); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1522 | if(!buf_size) |
| 1523 | return FALSE; |
| 1524 | |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1525 | buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1526 | if (!buf) { |
| 1527 | SetLastError(ERROR_OUTOFMEMORY); |
| 1528 | return FALSE; |
| 1529 | } |
| 1530 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1531 | result_length = GetFinalPathNameByHandleW(hdl, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1532 | buf, buf_size, VOLUME_NAME_DOS); |
| 1533 | |
| 1534 | if(!result_length) { |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1535 | PyMem_RawFree(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1536 | return FALSE; |
| 1537 | } |
| 1538 | |
| 1539 | if(!CloseHandle(hdl)) { |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1540 | PyMem_RawFree(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1541 | return FALSE; |
| 1542 | } |
| 1543 | |
| 1544 | buf[result_length] = 0; |
| 1545 | |
| 1546 | *target_path = buf; |
| 1547 | return TRUE; |
| 1548 | } |
| 1549 | |
| 1550 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1551 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1552 | BOOL traverse) |
| 1553 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1554 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1555 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1556 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1557 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1558 | wchar_t *target_path; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1559 | const wchar_t *dot; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1560 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1561 | hFile = CreateFileW( |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1562 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1563 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1564 | 0, /* share mode */ |
| 1565 | NULL, /* security attributes */ |
| 1566 | OPEN_EXISTING, |
| 1567 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1568 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1569 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1570 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1571 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1572 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1573 | NULL); |
| 1574 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1575 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1576 | /* Either the target doesn't exist, or we don't have access to |
| 1577 | get a handle to it. If the former, we need to return an error. |
| 1578 | If the latter, we can use attributes_from_dir. */ |
| 1579 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1580 | return -1; |
| 1581 | /* Could not get attributes on open file. Fall back to |
| 1582 | reading the directory. */ |
| 1583 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1584 | /* Very strange. This should not fail now */ |
| 1585 | return -1; |
| 1586 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1587 | if (traverse) { |
| 1588 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1589 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1590 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1591 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1592 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1593 | } else { |
| 1594 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1595 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1596 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1597 | } |
| 1598 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1599 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1600 | return -1; |
| 1601 | |
| 1602 | /* Close the outer open file handle now that we're about to |
| 1603 | reopen it with different flags. */ |
| 1604 | if (!CloseHandle(hFile)) |
| 1605 | return -1; |
| 1606 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1607 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1608 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1609 | the file without the reparse handling flag set. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1610 | hFile2 = CreateFileW( |
| 1611 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1612 | NULL, OPEN_EXISTING, |
| 1613 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1614 | NULL); |
| 1615 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1616 | return -1; |
| 1617 | |
| 1618 | if (!get_target_path(hFile2, &target_path)) |
| 1619 | return -1; |
| 1620 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1621 | code = win32_xstat_impl(target_path, result, FALSE); |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1622 | PyMem_RawFree(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1623 | return code; |
| 1624 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1625 | } else |
| 1626 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1627 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1628 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1629 | |
| 1630 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1631 | dot = wcsrchr(path, '.'); |
| 1632 | if (dot) { |
| 1633 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1634 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1635 | result->st_mode |= 0111; |
| 1636 | } |
| 1637 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1641 | 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] | 1642 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1643 | /* Protocol violation: we explicitly clear errno, instead of |
| 1644 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1645 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1646 | errno = 0; |
| 1647 | return code; |
| 1648 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1649 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1650 | |
| 1651 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1652 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1653 | default does not traverse symlinks and instead returns attributes for |
| 1654 | the symlink. |
| 1655 | |
| 1656 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1657 | win32_stat will first explicitly resolve the symlink target and then will |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1658 | call win32_lstat on that result. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1659 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1660 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1661 | 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] | 1662 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1663 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1666 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1667 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1668 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1669 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1672 | #endif /* MS_WINDOWS */ |
| 1673 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1674 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1675 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1676 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1677 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1678 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1679 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1680 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1681 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1682 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1683 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1684 | |
| 1685 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1686 | {"st_mode", "protection bits"}, |
| 1687 | {"st_ino", "inode"}, |
| 1688 | {"st_dev", "device"}, |
| 1689 | {"st_nlink", "number of hard links"}, |
| 1690 | {"st_uid", "user ID of owner"}, |
| 1691 | {"st_gid", "group ID of owner"}, |
| 1692 | {"st_size", "total size, in bytes"}, |
| 1693 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1694 | {NULL, "integer time of last access"}, |
| 1695 | {NULL, "integer time of last modification"}, |
| 1696 | {NULL, "integer time of last change"}, |
| 1697 | {"st_atime", "time of last access"}, |
| 1698 | {"st_mtime", "time of last modification"}, |
| 1699 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1700 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1701 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1702 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1703 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1704 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1705 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1706 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1707 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1708 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1709 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1710 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1711 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1712 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1713 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1714 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1715 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1716 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1717 | #endif |
| 1718 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1719 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1720 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1721 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1722 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1723 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1724 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1725 | }; |
| 1726 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1727 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1728 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1729 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1730 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1731 | #endif |
| 1732 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1733 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1734 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1735 | #else |
| 1736 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1737 | #endif |
| 1738 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1739 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1740 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1741 | #else |
| 1742 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1743 | #endif |
| 1744 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1745 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1746 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1747 | #else |
| 1748 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1749 | #endif |
| 1750 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1751 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1752 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1753 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1754 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1755 | #endif |
| 1756 | |
| 1757 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1758 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1759 | #else |
| 1760 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1761 | #endif |
| 1762 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1763 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1764 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1765 | #else |
| 1766 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 1767 | #endif |
| 1768 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1769 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1770 | "stat_result", /* name */ |
| 1771 | stat_result__doc__, /* doc */ |
| 1772 | stat_result_fields, |
| 1773 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1774 | }; |
| 1775 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1776 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1777 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1778 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1779 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1780 | 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] | 1781 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1782 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1783 | |
| 1784 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1785 | {"f_bsize", }, |
| 1786 | {"f_frsize", }, |
| 1787 | {"f_blocks", }, |
| 1788 | {"f_bfree", }, |
| 1789 | {"f_bavail", }, |
| 1790 | {"f_files", }, |
| 1791 | {"f_ffree", }, |
| 1792 | {"f_favail", }, |
| 1793 | {"f_flag", }, |
| 1794 | {"f_namemax",}, |
| 1795 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1796 | }; |
| 1797 | |
| 1798 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1799 | "statvfs_result", /* name */ |
| 1800 | statvfs_result__doc__, /* doc */ |
| 1801 | statvfs_result_fields, |
| 1802 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1803 | }; |
| 1804 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1805 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1806 | PyDoc_STRVAR(waitid_result__doc__, |
| 1807 | "waitid_result: Result from waitid.\n\n\ |
| 1808 | This object may be accessed either as a tuple of\n\ |
| 1809 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1810 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1811 | \n\ |
| 1812 | See os.waitid for more information."); |
| 1813 | |
| 1814 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1815 | {"si_pid", }, |
| 1816 | {"si_uid", }, |
| 1817 | {"si_signo", }, |
| 1818 | {"si_status", }, |
| 1819 | {"si_code", }, |
| 1820 | {0} |
| 1821 | }; |
| 1822 | |
| 1823 | static PyStructSequence_Desc waitid_result_desc = { |
| 1824 | "waitid_result", /* name */ |
| 1825 | waitid_result__doc__, /* doc */ |
| 1826 | waitid_result_fields, |
| 1827 | 5 |
| 1828 | }; |
| 1829 | static PyTypeObject WaitidResultType; |
| 1830 | #endif |
| 1831 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1832 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1833 | static PyTypeObject StatResultType; |
| 1834 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1835 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1836 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1837 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1838 | static newfunc structseq_new; |
| 1839 | |
| 1840 | static PyObject * |
| 1841 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1842 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1843 | PyStructSequence *result; |
| 1844 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1845 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1846 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1847 | if (!result) |
| 1848 | return NULL; |
| 1849 | /* If we have been initialized from a tuple, |
| 1850 | st_?time might be set to None. Initialize it |
| 1851 | from the int slots. */ |
| 1852 | for (i = 7; i <= 9; i++) { |
| 1853 | if (result->ob_item[i+3] == Py_None) { |
| 1854 | Py_DECREF(Py_None); |
| 1855 | Py_INCREF(result->ob_item[i]); |
| 1856 | result->ob_item[i+3] = result->ob_item[i]; |
| 1857 | } |
| 1858 | } |
| 1859 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | |
| 1863 | |
| 1864 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1865 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1866 | |
| 1867 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1868 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1869 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1870 | \n\ |
| 1871 | If value is True, future calls to stat() return floats; if it is False,\n\ |
| 1872 | future calls return ints.\n\ |
| 1873 | If value is omitted, return the current setting.\n"); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1874 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1875 | /* AC 3.5: the public default value should be None, not ready for that yet */ |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1876 | static PyObject* |
| 1877 | stat_float_times(PyObject* self, PyObject *args) |
| 1878 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1879 | int newval = -1; |
| 1880 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1881 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 1882 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1883 | "stat_float_times() is deprecated", |
| 1884 | 1)) |
| 1885 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1886 | if (newval == -1) |
| 1887 | /* Return old value */ |
| 1888 | return PyBool_FromLong(_stat_float_times); |
| 1889 | _stat_float_times = newval; |
| 1890 | Py_INCREF(Py_None); |
| 1891 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1892 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1893 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1894 | static PyObject *billion = NULL; |
| 1895 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1896 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1897 | 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] | 1898 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1899 | PyObject *s = _PyLong_FromTime_t(sec); |
| 1900 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 1901 | PyObject *s_in_ns = NULL; |
| 1902 | PyObject *ns_total = NULL; |
| 1903 | PyObject *float_s = NULL; |
| 1904 | |
| 1905 | if (!(s && ns_fractional)) |
| 1906 | goto exit; |
| 1907 | |
| 1908 | s_in_ns = PyNumber_Multiply(s, billion); |
| 1909 | if (!s_in_ns) |
| 1910 | goto exit; |
| 1911 | |
| 1912 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 1913 | if (!ns_total) |
| 1914 | goto exit; |
| 1915 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1916 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1917 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1918 | if (!float_s) |
| 1919 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1920 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1921 | else { |
| 1922 | float_s = s; |
| 1923 | Py_INCREF(float_s); |
| 1924 | } |
| 1925 | |
| 1926 | PyStructSequence_SET_ITEM(v, index, s); |
| 1927 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 1928 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 1929 | s = NULL; |
| 1930 | float_s = NULL; |
| 1931 | ns_total = NULL; |
| 1932 | exit: |
| 1933 | Py_XDECREF(s); |
| 1934 | Py_XDECREF(ns_fractional); |
| 1935 | Py_XDECREF(s_in_ns); |
| 1936 | Py_XDECREF(ns_total); |
| 1937 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1940 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1941 | (used by posix_stat() and posix_fstat()) */ |
| 1942 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1943 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1944 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1945 | unsigned long ansec, mnsec, cnsec; |
| 1946 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1947 | if (v == NULL) |
| 1948 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1949 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1950 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1951 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1952 | PyStructSequence_SET_ITEM(v, 1, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1953 | PyLong_FromLongLong((long long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1954 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1955 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1956 | #endif |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 1957 | #ifdef MS_WINDOWS |
| 1958 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1959 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 1960 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1961 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1962 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 1963 | #if defined(MS_WINDOWS) |
| 1964 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 1965 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 1966 | #else |
| 1967 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 1968 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 1969 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1970 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1971 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1972 | PyLong_FromLongLong((long long)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1973 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1974 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1975 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1976 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1977 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1978 | ansec = st->st_atim.tv_nsec; |
| 1979 | mnsec = st->st_mtim.tv_nsec; |
| 1980 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1981 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1982 | ansec = st->st_atimespec.tv_nsec; |
| 1983 | mnsec = st->st_mtimespec.tv_nsec; |
| 1984 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1985 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1986 | ansec = st->st_atime_nsec; |
| 1987 | mnsec = st->st_mtime_nsec; |
| 1988 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1989 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1990 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1991 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1992 | fill_time(v, 7, st->st_atime, ansec); |
| 1993 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1994 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1995 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1996 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1997 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1998 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1999 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2000 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2001 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2002 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2003 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2004 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2005 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2006 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2007 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2008 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2009 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2010 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2011 | #endif |
| 2012 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2013 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2014 | PyObject *val; |
| 2015 | unsigned long bsec,bnsec; |
| 2016 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2017 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2018 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2019 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2020 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2021 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2022 | if (_stat_float_times) { |
| 2023 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2024 | } else { |
| 2025 | val = PyLong_FromLong((long)bsec); |
| 2026 | } |
| 2027 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2028 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2029 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2030 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2031 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2032 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2033 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2034 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2035 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2036 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2037 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2038 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2039 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2040 | if (PyErr_Occurred()) { |
| 2041 | Py_DECREF(v); |
| 2042 | return NULL; |
| 2043 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2044 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2045 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2048 | /* POSIX methods */ |
| 2049 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2050 | |
| 2051 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2052 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2053 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2054 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2055 | STRUCT_STAT st; |
| 2056 | int result; |
| 2057 | |
| 2058 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2059 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2060 | return NULL; |
| 2061 | #endif |
| 2062 | |
| 2063 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2064 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2065 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2066 | return NULL; |
| 2067 | |
| 2068 | Py_BEGIN_ALLOW_THREADS |
| 2069 | if (path->fd != -1) |
| 2070 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2071 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame^] | 2072 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2073 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2074 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2075 | result = win32_lstat(path->wide, &st); |
| 2076 | #else |
| 2077 | else |
| 2078 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2079 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2080 | result = LSTAT(path->narrow, &st); |
| 2081 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2082 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2083 | #ifdef HAVE_FSTATAT |
| 2084 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2085 | result = fstatat(dir_fd, path->narrow, &st, |
| 2086 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2087 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2088 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2089 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2090 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2091 | Py_END_ALLOW_THREADS |
| 2092 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2093 | if (result != 0) { |
| 2094 | return path_error(path); |
| 2095 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2096 | |
| 2097 | return _pystat_fromstructstat(&st); |
| 2098 | } |
| 2099 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2100 | /*[python input] |
| 2101 | |
| 2102 | for s in """ |
| 2103 | |
| 2104 | FACCESSAT |
| 2105 | FCHMODAT |
| 2106 | FCHOWNAT |
| 2107 | FSTATAT |
| 2108 | LINKAT |
| 2109 | MKDIRAT |
| 2110 | MKFIFOAT |
| 2111 | MKNODAT |
| 2112 | OPENAT |
| 2113 | READLINKAT |
| 2114 | SYMLINKAT |
| 2115 | UNLINKAT |
| 2116 | |
| 2117 | """.strip().split(): |
| 2118 | s = s.strip() |
| 2119 | print(""" |
| 2120 | #ifdef HAVE_{s} |
| 2121 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2122 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2123 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2124 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2125 | """.rstrip().format(s=s)) |
| 2126 | |
| 2127 | for s in """ |
| 2128 | |
| 2129 | FCHDIR |
| 2130 | FCHMOD |
| 2131 | FCHOWN |
| 2132 | FDOPENDIR |
| 2133 | FEXECVE |
| 2134 | FPATHCONF |
| 2135 | FSTATVFS |
| 2136 | FTRUNCATE |
| 2137 | |
| 2138 | """.strip().split(): |
| 2139 | s = s.strip() |
| 2140 | print(""" |
| 2141 | #ifdef HAVE_{s} |
| 2142 | #define PATH_HAVE_{s} 1 |
| 2143 | #else |
| 2144 | #define PATH_HAVE_{s} 0 |
| 2145 | #endif |
| 2146 | |
| 2147 | """.rstrip().format(s=s)) |
| 2148 | [python start generated code]*/ |
| 2149 | |
| 2150 | #ifdef HAVE_FACCESSAT |
| 2151 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2152 | #else |
| 2153 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2154 | #endif |
| 2155 | |
| 2156 | #ifdef HAVE_FCHMODAT |
| 2157 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2158 | #else |
| 2159 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2160 | #endif |
| 2161 | |
| 2162 | #ifdef HAVE_FCHOWNAT |
| 2163 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2164 | #else |
| 2165 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2166 | #endif |
| 2167 | |
| 2168 | #ifdef HAVE_FSTATAT |
| 2169 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2170 | #else |
| 2171 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2172 | #endif |
| 2173 | |
| 2174 | #ifdef HAVE_LINKAT |
| 2175 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2176 | #else |
| 2177 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2178 | #endif |
| 2179 | |
| 2180 | #ifdef HAVE_MKDIRAT |
| 2181 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2182 | #else |
| 2183 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2184 | #endif |
| 2185 | |
| 2186 | #ifdef HAVE_MKFIFOAT |
| 2187 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2188 | #else |
| 2189 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2190 | #endif |
| 2191 | |
| 2192 | #ifdef HAVE_MKNODAT |
| 2193 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2194 | #else |
| 2195 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2196 | #endif |
| 2197 | |
| 2198 | #ifdef HAVE_OPENAT |
| 2199 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2200 | #else |
| 2201 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2202 | #endif |
| 2203 | |
| 2204 | #ifdef HAVE_READLINKAT |
| 2205 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2206 | #else |
| 2207 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2208 | #endif |
| 2209 | |
| 2210 | #ifdef HAVE_SYMLINKAT |
| 2211 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2212 | #else |
| 2213 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2214 | #endif |
| 2215 | |
| 2216 | #ifdef HAVE_UNLINKAT |
| 2217 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2218 | #else |
| 2219 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2220 | #endif |
| 2221 | |
| 2222 | #ifdef HAVE_FCHDIR |
| 2223 | #define PATH_HAVE_FCHDIR 1 |
| 2224 | #else |
| 2225 | #define PATH_HAVE_FCHDIR 0 |
| 2226 | #endif |
| 2227 | |
| 2228 | #ifdef HAVE_FCHMOD |
| 2229 | #define PATH_HAVE_FCHMOD 1 |
| 2230 | #else |
| 2231 | #define PATH_HAVE_FCHMOD 0 |
| 2232 | #endif |
| 2233 | |
| 2234 | #ifdef HAVE_FCHOWN |
| 2235 | #define PATH_HAVE_FCHOWN 1 |
| 2236 | #else |
| 2237 | #define PATH_HAVE_FCHOWN 0 |
| 2238 | #endif |
| 2239 | |
| 2240 | #ifdef HAVE_FDOPENDIR |
| 2241 | #define PATH_HAVE_FDOPENDIR 1 |
| 2242 | #else |
| 2243 | #define PATH_HAVE_FDOPENDIR 0 |
| 2244 | #endif |
| 2245 | |
| 2246 | #ifdef HAVE_FEXECVE |
| 2247 | #define PATH_HAVE_FEXECVE 1 |
| 2248 | #else |
| 2249 | #define PATH_HAVE_FEXECVE 0 |
| 2250 | #endif |
| 2251 | |
| 2252 | #ifdef HAVE_FPATHCONF |
| 2253 | #define PATH_HAVE_FPATHCONF 1 |
| 2254 | #else |
| 2255 | #define PATH_HAVE_FPATHCONF 0 |
| 2256 | #endif |
| 2257 | |
| 2258 | #ifdef HAVE_FSTATVFS |
| 2259 | #define PATH_HAVE_FSTATVFS 1 |
| 2260 | #else |
| 2261 | #define PATH_HAVE_FSTATVFS 0 |
| 2262 | #endif |
| 2263 | |
| 2264 | #ifdef HAVE_FTRUNCATE |
| 2265 | #define PATH_HAVE_FTRUNCATE 1 |
| 2266 | #else |
| 2267 | #define PATH_HAVE_FTRUNCATE 0 |
| 2268 | #endif |
| 2269 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2270 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2271 | #ifdef MS_WINDOWS |
| 2272 | #undef PATH_HAVE_FTRUNCATE |
| 2273 | #define PATH_HAVE_FTRUNCATE 1 |
| 2274 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2275 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2276 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2277 | |
| 2278 | class path_t_converter(CConverter): |
| 2279 | |
| 2280 | type = "path_t" |
| 2281 | impl_by_reference = True |
| 2282 | parse_by_reference = True |
| 2283 | |
| 2284 | converter = 'path_converter' |
| 2285 | |
| 2286 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2287 | # right now path_t doesn't support default values. |
| 2288 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2289 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2290 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2291 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2292 | if self.c_default not in (None, 'Py_None'): |
| 2293 | 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] | 2294 | |
| 2295 | self.nullable = nullable |
| 2296 | self.allow_fd = allow_fd |
| 2297 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2298 | def pre_render(self): |
| 2299 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2300 | if isinstance(value, str): |
| 2301 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2302 | return str(int(bool(value))) |
| 2303 | |
| 2304 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2305 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2306 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2307 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2308 | strify(self.nullable), |
| 2309 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2310 | ) |
| 2311 | |
| 2312 | def cleanup(self): |
| 2313 | return "path_cleanup(&" + self.name + ");\n" |
| 2314 | |
| 2315 | |
| 2316 | class dir_fd_converter(CConverter): |
| 2317 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2318 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2319 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2320 | if self.default in (unspecified, None): |
| 2321 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2322 | if isinstance(requires, str): |
| 2323 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2324 | else: |
| 2325 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2326 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2327 | class fildes_converter(CConverter): |
| 2328 | type = 'int' |
| 2329 | converter = 'fildes_converter' |
| 2330 | |
| 2331 | class uid_t_converter(CConverter): |
| 2332 | type = "uid_t" |
| 2333 | converter = '_Py_Uid_Converter' |
| 2334 | |
| 2335 | class gid_t_converter(CConverter): |
| 2336 | type = "gid_t" |
| 2337 | converter = '_Py_Gid_Converter' |
| 2338 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2339 | class dev_t_converter(CConverter): |
| 2340 | type = 'dev_t' |
| 2341 | converter = '_Py_Dev_Converter' |
| 2342 | |
| 2343 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2344 | type = 'dev_t' |
| 2345 | conversion_fn = '_PyLong_FromDev' |
| 2346 | unsigned_cast = '(dev_t)' |
| 2347 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2348 | class FSConverter_converter(CConverter): |
| 2349 | type = 'PyObject *' |
| 2350 | converter = 'PyUnicode_FSConverter' |
| 2351 | def converter_init(self): |
| 2352 | if self.default is not unspecified: |
| 2353 | fail("FSConverter_converter does not support default values") |
| 2354 | self.c_default = 'NULL' |
| 2355 | |
| 2356 | def cleanup(self): |
| 2357 | return "Py_XDECREF(" + self.name + ");\n" |
| 2358 | |
| 2359 | class pid_t_converter(CConverter): |
| 2360 | type = 'pid_t' |
| 2361 | format_unit = '" _Py_PARSE_PID "' |
| 2362 | |
| 2363 | class idtype_t_converter(int_converter): |
| 2364 | type = 'idtype_t' |
| 2365 | |
| 2366 | class id_t_converter(CConverter): |
| 2367 | type = 'id_t' |
| 2368 | format_unit = '" _Py_PARSE_PID "' |
| 2369 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2370 | class intptr_t_converter(CConverter): |
| 2371 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2372 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2373 | |
| 2374 | class Py_off_t_converter(CConverter): |
| 2375 | type = 'Py_off_t' |
| 2376 | converter = 'Py_off_t_converter' |
| 2377 | |
| 2378 | class Py_off_t_return_converter(long_return_converter): |
| 2379 | type = 'Py_off_t' |
| 2380 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2381 | |
| 2382 | class path_confname_converter(CConverter): |
| 2383 | type="int" |
| 2384 | converter="conv_path_confname" |
| 2385 | |
| 2386 | class confstr_confname_converter(path_confname_converter): |
| 2387 | converter='conv_confstr_confname' |
| 2388 | |
| 2389 | class sysconf_confname_converter(path_confname_converter): |
| 2390 | converter="conv_sysconf_confname" |
| 2391 | |
| 2392 | class sched_param_converter(CConverter): |
| 2393 | type = 'struct sched_param' |
| 2394 | converter = 'convert_sched_param' |
| 2395 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2396 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2397 | [python start generated code]*/ |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 2398 | /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2399 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2400 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2401 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2402 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2403 | |
| 2404 | path : path_t(allow_fd=True) |
| 2405 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 2406 | |
| 2407 | * |
| 2408 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2409 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2410 | If not None, it should be a file descriptor open to a directory, |
| 2411 | and path should be a relative string; path will then be relative to |
| 2412 | that directory. |
| 2413 | |
| 2414 | follow_symlinks: bool = True |
| 2415 | If False, and the last element of the path is a symbolic link, |
| 2416 | stat will examine the symbolic link itself instead of the file |
| 2417 | the link points to. |
| 2418 | |
| 2419 | Perform a stat system call on the given path. |
| 2420 | |
| 2421 | dir_fd and follow_symlinks may not be implemented |
| 2422 | on your platform. If they are unavailable, using them will raise a |
| 2423 | NotImplementedError. |
| 2424 | |
| 2425 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2426 | an open file descriptor. |
| 2427 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2428 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2429 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2430 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2431 | os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks) |
| 2432 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=099d356c306fa24a]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2433 | { |
| 2434 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2435 | } |
| 2436 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2437 | |
| 2438 | /*[clinic input] |
| 2439 | os.lstat |
| 2440 | |
| 2441 | path : path_t |
| 2442 | |
| 2443 | * |
| 2444 | |
| 2445 | dir_fd : dir_fd(requires='fstatat') = None |
| 2446 | |
| 2447 | Perform a stat system call on the given path, without following symbolic links. |
| 2448 | |
| 2449 | Like stat(), but do not follow symbolic links. |
| 2450 | Equivalent to stat(path, follow_symlinks=False). |
| 2451 | [clinic start generated code]*/ |
| 2452 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2453 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2454 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2455 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2456 | { |
| 2457 | int follow_symlinks = 0; |
| 2458 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2459 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2461 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2462 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2463 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2464 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2465 | path: path_t |
| 2466 | Path to be tested; can be string or bytes |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2467 | |
| 2468 | mode: int |
| 2469 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2470 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2471 | |
| 2472 | * |
| 2473 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2474 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2475 | If not None, it should be a file descriptor open to a directory, |
| 2476 | and path should be relative; path will then be relative to that |
| 2477 | directory. |
| 2478 | |
| 2479 | effective_ids: bool = False |
| 2480 | If True, access will use the effective uid/gid instead of |
| 2481 | the real uid/gid. |
| 2482 | |
| 2483 | follow_symlinks: bool = True |
| 2484 | If False, and the last element of the path is a symbolic link, |
| 2485 | access will examine the symbolic link itself instead of the file |
| 2486 | the link points to. |
| 2487 | |
| 2488 | Use the real uid/gid to test for access to a path. |
| 2489 | |
| 2490 | {parameters} |
| 2491 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2492 | on your platform. If they are unavailable, using them will raise a |
| 2493 | NotImplementedError. |
| 2494 | |
| 2495 | Note that most operations will use the effective uid/gid, therefore this |
| 2496 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2497 | has the specified access to the path. |
| 2498 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2499 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2500 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2501 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2502 | 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] | 2503 | int effective_ids, int follow_symlinks) |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2504 | /*[clinic end generated code: output=cf84158bc90b1a77 input=8e8c3a6ba791fee3]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2505 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2506 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2507 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2508 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2509 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2510 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2511 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2512 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2513 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2514 | #ifndef HAVE_FACCESSAT |
| 2515 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2516 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2517 | |
| 2518 | if (effective_ids) { |
| 2519 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2520 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2521 | } |
| 2522 | #endif |
| 2523 | |
| 2524 | #ifdef MS_WINDOWS |
| 2525 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2526 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2527 | Py_END_ALLOW_THREADS |
| 2528 | |
| 2529 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2530 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2531 | * * we didn't get a -1, and |
| 2532 | * * write access wasn't requested, |
| 2533 | * * or the file isn't read-only, |
| 2534 | * * or it's a directory. |
| 2535 | * (Directories cannot be read-only on Windows.) |
| 2536 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2537 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2538 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2539 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2540 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2541 | #else |
| 2542 | |
| 2543 | Py_BEGIN_ALLOW_THREADS |
| 2544 | #ifdef HAVE_FACCESSAT |
| 2545 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2546 | effective_ids || |
| 2547 | !follow_symlinks) { |
| 2548 | int flags = 0; |
| 2549 | if (!follow_symlinks) |
| 2550 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2551 | if (effective_ids) |
| 2552 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2553 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2554 | } |
| 2555 | else |
| 2556 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2557 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2558 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2559 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2560 | #endif |
| 2561 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2562 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2565 | #ifndef F_OK |
| 2566 | #define F_OK 0 |
| 2567 | #endif |
| 2568 | #ifndef R_OK |
| 2569 | #define R_OK 4 |
| 2570 | #endif |
| 2571 | #ifndef W_OK |
| 2572 | #define W_OK 2 |
| 2573 | #endif |
| 2574 | #ifndef X_OK |
| 2575 | #define X_OK 1 |
| 2576 | #endif |
| 2577 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2578 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2579 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2580 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2581 | os.ttyname -> DecodeFSDefault |
| 2582 | |
| 2583 | fd: int |
| 2584 | Integer file descriptor handle. |
| 2585 | |
| 2586 | / |
| 2587 | |
| 2588 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2589 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2590 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2591 | static char * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2592 | os_ttyname_impl(PyObject *module, int fd) |
| 2593 | /*[clinic end generated code: output=ed16ad216d813591 input=5f72ca83e76b3b45]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2594 | { |
| 2595 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2596 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2597 | ret = ttyname(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2598 | if (ret == NULL) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2599 | posix_error(); |
| 2600 | return ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2601 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2602 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2603 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2604 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2605 | /*[clinic input] |
| 2606 | os.ctermid |
| 2607 | |
| 2608 | Return the name of the controlling terminal for this process. |
| 2609 | [clinic start generated code]*/ |
| 2610 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2611 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2612 | os_ctermid_impl(PyObject *module) |
| 2613 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2614 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2615 | char *ret; |
| 2616 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2617 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2618 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2619 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2620 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2621 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2622 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2623 | if (ret == NULL) |
| 2624 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2625 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2626 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2627 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2628 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2629 | |
| 2630 | /*[clinic input] |
| 2631 | os.chdir |
| 2632 | |
| 2633 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2634 | |
| 2635 | Change the current working directory to the specified path. |
| 2636 | |
| 2637 | path may always be specified as a string. |
| 2638 | On some platforms, path may also be specified as an open file descriptor. |
| 2639 | If this functionality is unavailable, using it raises an exception. |
| 2640 | [clinic start generated code]*/ |
| 2641 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2642 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2643 | os_chdir_impl(PyObject *module, path_t *path) |
| 2644 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2645 | { |
| 2646 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2647 | |
| 2648 | Py_BEGIN_ALLOW_THREADS |
| 2649 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2650 | /* on unix, success = 0, on windows, success = !0 */ |
| 2651 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2652 | #else |
| 2653 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2654 | if (path->fd != -1) |
| 2655 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2656 | else |
| 2657 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2658 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2659 | #endif |
| 2660 | Py_END_ALLOW_THREADS |
| 2661 | |
| 2662 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2663 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2664 | } |
| 2665 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2666 | Py_RETURN_NONE; |
| 2667 | } |
| 2668 | |
| 2669 | |
| 2670 | #ifdef HAVE_FCHDIR |
| 2671 | /*[clinic input] |
| 2672 | os.fchdir |
| 2673 | |
| 2674 | fd: fildes |
| 2675 | |
| 2676 | Change to the directory of the given file descriptor. |
| 2677 | |
| 2678 | fd must be opened on a directory, not a file. |
| 2679 | Equivalent to os.chdir(fd). |
| 2680 | |
| 2681 | [clinic start generated code]*/ |
| 2682 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2683 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2684 | os_fchdir_impl(PyObject *module, int fd) |
| 2685 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2686 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2687 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2688 | } |
| 2689 | #endif /* HAVE_FCHDIR */ |
| 2690 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2691 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2692 | /*[clinic input] |
| 2693 | os.chmod |
| 2694 | |
| 2695 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
| 2696 | Path to be modified. May always be specified as a str or bytes. |
| 2697 | On some platforms, path may also be specified as an open file descriptor. |
| 2698 | If this functionality is unavailable, using it raises an exception. |
| 2699 | |
| 2700 | mode: int |
| 2701 | Operating-system mode bitfield. |
| 2702 | |
| 2703 | * |
| 2704 | |
| 2705 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2706 | If not None, it should be a file descriptor open to a directory, |
| 2707 | and path should be relative; path will then be relative to that |
| 2708 | directory. |
| 2709 | |
| 2710 | follow_symlinks: bool = True |
| 2711 | If False, and the last element of the path is a symbolic link, |
| 2712 | chmod will modify the symbolic link itself instead of the file |
| 2713 | the link points to. |
| 2714 | |
| 2715 | Change the access permissions of a file. |
| 2716 | |
| 2717 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2718 | an open file descriptor. |
| 2719 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2720 | If they are unavailable, using them will raise a NotImplementedError. |
| 2721 | |
| 2722 | [clinic start generated code]*/ |
| 2723 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2724 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2725 | 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] | 2726 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2727 | /*[clinic end generated code: output=5cf6a94915cc7bff input=7f1618e5e15cc196]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2728 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2729 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2730 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2731 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2732 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2733 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2734 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2735 | #ifdef HAVE_FCHMODAT |
| 2736 | int fchmodat_nofollow_unsupported = 0; |
| 2737 | #endif |
| 2738 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2739 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2740 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2741 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2742 | #endif |
| 2743 | |
| 2744 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2745 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2746 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2747 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2748 | result = 0; |
| 2749 | else { |
| 2750 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2751 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2752 | else |
| 2753 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2754 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2755 | } |
| 2756 | Py_END_ALLOW_THREADS |
| 2757 | |
| 2758 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2759 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2760 | } |
| 2761 | #else /* MS_WINDOWS */ |
| 2762 | Py_BEGIN_ALLOW_THREADS |
| 2763 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2764 | if (path->fd != -1) |
| 2765 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2766 | else |
| 2767 | #endif |
| 2768 | #ifdef HAVE_LCHMOD |
| 2769 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2770 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2771 | else |
| 2772 | #endif |
| 2773 | #ifdef HAVE_FCHMODAT |
| 2774 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2775 | /* |
| 2776 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2777 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2778 | * and then says it isn't implemented yet. |
| 2779 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2780 | * |
| 2781 | * Once it is supported, os.chmod will automatically |
| 2782 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2783 | * Until then, we need to be careful what exception we raise. |
| 2784 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2785 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2786 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2787 | /* |
| 2788 | * But wait! We can't throw the exception without allowing threads, |
| 2789 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2790 | */ |
| 2791 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2792 | result && |
| 2793 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2794 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2795 | } |
| 2796 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2797 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2798 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2799 | Py_END_ALLOW_THREADS |
| 2800 | |
| 2801 | if (result) { |
| 2802 | #ifdef HAVE_FCHMODAT |
| 2803 | if (fchmodat_nofollow_unsupported) { |
| 2804 | if (dir_fd != DEFAULT_DIR_FD) |
| 2805 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2806 | dir_fd, follow_symlinks); |
| 2807 | else |
| 2808 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2809 | } |
| 2810 | else |
| 2811 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2812 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2813 | } |
| 2814 | #endif |
| 2815 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2816 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2819 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2820 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2821 | /*[clinic input] |
| 2822 | os.fchmod |
| 2823 | |
| 2824 | fd: int |
| 2825 | mode: int |
| 2826 | |
| 2827 | Change the access permissions of the file given by file descriptor fd. |
| 2828 | |
| 2829 | Equivalent to os.chmod(fd, mode). |
| 2830 | [clinic start generated code]*/ |
| 2831 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2832 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2833 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 2834 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2835 | { |
| 2836 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2837 | int async_err = 0; |
| 2838 | |
| 2839 | do { |
| 2840 | Py_BEGIN_ALLOW_THREADS |
| 2841 | res = fchmod(fd, mode); |
| 2842 | Py_END_ALLOW_THREADS |
| 2843 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 2844 | if (res != 0) |
| 2845 | return (!async_err) ? posix_error() : NULL; |
| 2846 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2847 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2848 | } |
| 2849 | #endif /* HAVE_FCHMOD */ |
| 2850 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2851 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2852 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2853 | /*[clinic input] |
| 2854 | os.lchmod |
| 2855 | |
| 2856 | path: path_t |
| 2857 | mode: int |
| 2858 | |
| 2859 | Change the access permissions of a file, without following symbolic links. |
| 2860 | |
| 2861 | If path is a symlink, this affects the link itself rather than the target. |
| 2862 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 2863 | [clinic start generated code]*/ |
| 2864 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2865 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2866 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 2867 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2868 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2869 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2870 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2871 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2872 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2873 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2874 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2875 | return NULL; |
| 2876 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2877 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2878 | } |
| 2879 | #endif /* HAVE_LCHMOD */ |
| 2880 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2881 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2882 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2883 | /*[clinic input] |
| 2884 | os.chflags |
| 2885 | |
| 2886 | path: path_t |
| 2887 | flags: unsigned_long(bitwise=True) |
| 2888 | follow_symlinks: bool=True |
| 2889 | |
| 2890 | Set file flags. |
| 2891 | |
| 2892 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 2893 | link, chflags will change flags on the symbolic link itself instead of the |
| 2894 | file the link points to. |
| 2895 | follow_symlinks may not be implemented on your platform. If it is |
| 2896 | unavailable, using it will raise a NotImplementedError. |
| 2897 | |
| 2898 | [clinic start generated code]*/ |
| 2899 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2900 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2901 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2902 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2903 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2904 | { |
| 2905 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2906 | |
| 2907 | #ifndef HAVE_LCHFLAGS |
| 2908 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2909 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2910 | #endif |
| 2911 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2912 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2913 | #ifdef HAVE_LCHFLAGS |
| 2914 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2915 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2916 | else |
| 2917 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2918 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2919 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2920 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2921 | if (result) |
| 2922 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2923 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2924 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2925 | } |
| 2926 | #endif /* HAVE_CHFLAGS */ |
| 2927 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2928 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2929 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2930 | /*[clinic input] |
| 2931 | os.lchflags |
| 2932 | |
| 2933 | path: path_t |
| 2934 | flags: unsigned_long(bitwise=True) |
| 2935 | |
| 2936 | Set file flags. |
| 2937 | |
| 2938 | This function will not follow symbolic links. |
| 2939 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 2940 | [clinic start generated code]*/ |
| 2941 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2942 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2943 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 2944 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2945 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2946 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2947 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2948 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2949 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2950 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2951 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2952 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2953 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2954 | } |
| 2955 | #endif /* HAVE_LCHFLAGS */ |
| 2956 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2957 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2958 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2959 | /*[clinic input] |
| 2960 | os.chroot |
| 2961 | path: path_t |
| 2962 | |
| 2963 | Change root directory to path. |
| 2964 | |
| 2965 | [clinic start generated code]*/ |
| 2966 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2967 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2968 | os_chroot_impl(PyObject *module, path_t *path) |
| 2969 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2970 | { |
| 2971 | int res; |
| 2972 | Py_BEGIN_ALLOW_THREADS |
| 2973 | res = chroot(path->narrow); |
| 2974 | Py_END_ALLOW_THREADS |
| 2975 | if (res < 0) |
| 2976 | return path_error(path); |
| 2977 | Py_RETURN_NONE; |
| 2978 | } |
| 2979 | #endif /* HAVE_CHROOT */ |
| 2980 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2981 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2982 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2983 | /*[clinic input] |
| 2984 | os.fsync |
| 2985 | |
| 2986 | fd: fildes |
| 2987 | |
| 2988 | Force write of fd to disk. |
| 2989 | [clinic start generated code]*/ |
| 2990 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2991 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2992 | os_fsync_impl(PyObject *module, int fd) |
| 2993 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2994 | { |
| 2995 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2996 | } |
| 2997 | #endif /* HAVE_FSYNC */ |
| 2998 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2999 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3000 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3001 | /*[clinic input] |
| 3002 | os.sync |
| 3003 | |
| 3004 | Force write of everything to disk. |
| 3005 | [clinic start generated code]*/ |
| 3006 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3007 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3008 | os_sync_impl(PyObject *module) |
| 3009 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3010 | { |
| 3011 | Py_BEGIN_ALLOW_THREADS |
| 3012 | sync(); |
| 3013 | Py_END_ALLOW_THREADS |
| 3014 | Py_RETURN_NONE; |
| 3015 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3016 | #endif /* HAVE_SYNC */ |
| 3017 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3018 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3019 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3020 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3021 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3022 | #endif |
| 3023 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3024 | /*[clinic input] |
| 3025 | os.fdatasync |
| 3026 | |
| 3027 | fd: fildes |
| 3028 | |
| 3029 | Force write of fd to disk without forcing update of metadata. |
| 3030 | [clinic start generated code]*/ |
| 3031 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3032 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3033 | os_fdatasync_impl(PyObject *module, int fd) |
| 3034 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3035 | { |
| 3036 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3037 | } |
| 3038 | #endif /* HAVE_FDATASYNC */ |
| 3039 | |
| 3040 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3041 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3042 | /*[clinic input] |
| 3043 | os.chown |
| 3044 | |
| 3045 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
| 3046 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 3047 | |
| 3048 | uid: uid_t |
| 3049 | |
| 3050 | gid: gid_t |
| 3051 | |
| 3052 | * |
| 3053 | |
| 3054 | dir_fd : dir_fd(requires='fchownat') = None |
| 3055 | If not None, it should be a file descriptor open to a directory, |
| 3056 | and path should be relative; path will then be relative to that |
| 3057 | directory. |
| 3058 | |
| 3059 | follow_symlinks: bool = True |
| 3060 | If False, and the last element of the path is a symbolic link, |
| 3061 | stat will examine the symbolic link itself instead of the file |
| 3062 | the link points to. |
| 3063 | |
| 3064 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3065 | |
| 3066 | path may always be specified as a string. |
| 3067 | On some platforms, path may also be specified as an open file descriptor. |
| 3068 | If this functionality is unavailable, using it raises an exception. |
| 3069 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3070 | and path should be relative; path will then be relative to that directory. |
| 3071 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3072 | link, chown will modify the symbolic link itself instead of the file the |
| 3073 | link points to. |
| 3074 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3075 | an open file descriptor. |
| 3076 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3077 | If they are unavailable, using them will raise a NotImplementedError. |
| 3078 | |
| 3079 | [clinic start generated code]*/ |
| 3080 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3081 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3082 | 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] | 3083 | int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3084 | /*[clinic end generated code: output=4beadab0db5f70cd input=a61cc35574814d5d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3085 | { |
| 3086 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3087 | |
| 3088 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3089 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3090 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3091 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3092 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3093 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3094 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3095 | |
| 3096 | #ifdef __APPLE__ |
| 3097 | /* |
| 3098 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3099 | * (But we still have an lchown symbol because of weak-linking.) |
| 3100 | * It doesn't have fchownat either. So there's no possibility |
| 3101 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3102 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3103 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3104 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3105 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3106 | } |
| 3107 | #endif |
| 3108 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3109 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3110 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3111 | if (path->fd != -1) |
| 3112 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3113 | else |
| 3114 | #endif |
| 3115 | #ifdef HAVE_LCHOWN |
| 3116 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3117 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3118 | else |
| 3119 | #endif |
| 3120 | #ifdef HAVE_FCHOWNAT |
| 3121 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3122 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3123 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3124 | else |
| 3125 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3126 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3127 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3128 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3129 | if (result) |
| 3130 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3131 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3132 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3133 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3134 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3135 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3136 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3137 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3138 | /*[clinic input] |
| 3139 | os.fchown |
| 3140 | |
| 3141 | fd: int |
| 3142 | uid: uid_t |
| 3143 | gid: gid_t |
| 3144 | |
| 3145 | Change the owner and group id of the file specified by file descriptor. |
| 3146 | |
| 3147 | Equivalent to os.chown(fd, uid, gid). |
| 3148 | |
| 3149 | [clinic start generated code]*/ |
| 3150 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3151 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3152 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3153 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3154 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3155 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3156 | int async_err = 0; |
| 3157 | |
| 3158 | do { |
| 3159 | Py_BEGIN_ALLOW_THREADS |
| 3160 | res = fchown(fd, uid, gid); |
| 3161 | Py_END_ALLOW_THREADS |
| 3162 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3163 | if (res != 0) |
| 3164 | return (!async_err) ? posix_error() : NULL; |
| 3165 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3166 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3167 | } |
| 3168 | #endif /* HAVE_FCHOWN */ |
| 3169 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3170 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3171 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3172 | /*[clinic input] |
| 3173 | os.lchown |
| 3174 | |
| 3175 | path : path_t |
| 3176 | uid: uid_t |
| 3177 | gid: gid_t |
| 3178 | |
| 3179 | Change the owner and group id of path to the numeric uid and gid. |
| 3180 | |
| 3181 | This function will not follow symbolic links. |
| 3182 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3183 | [clinic start generated code]*/ |
| 3184 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3185 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3186 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3187 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3188 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3189 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3190 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3191 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3192 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3193 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3194 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3195 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3196 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3197 | } |
| 3198 | #endif /* HAVE_LCHOWN */ |
| 3199 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3200 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3201 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3202 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3203 | { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3204 | char *buf, *tmpbuf; |
| 3205 | char *cwd; |
| 3206 | const size_t chunk = 1024; |
| 3207 | size_t buflen = 0; |
| 3208 | PyObject *obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3209 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3210 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3211 | if (!use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3212 | wchar_t wbuf[MAXPATHLEN]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3213 | wchar_t *wbuf2 = wbuf; |
| 3214 | PyObject *resobj; |
| 3215 | DWORD len; |
| 3216 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3217 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3218 | /* If the buffer is large enough, len does not include the |
| 3219 | terminating \0. If the buffer is too small, len includes |
| 3220 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3221 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3222 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3223 | if (wbuf2) |
| 3224 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3225 | } |
| 3226 | Py_END_ALLOW_THREADS |
| 3227 | if (!wbuf2) { |
| 3228 | PyErr_NoMemory(); |
| 3229 | return NULL; |
| 3230 | } |
| 3231 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3232 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3233 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3234 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3235 | } |
| 3236 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3237 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3238 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3239 | return resobj; |
| 3240 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3241 | |
| 3242 | if (win32_warn_bytes_api()) |
| 3243 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3244 | #endif |
| 3245 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3246 | buf = cwd = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3247 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3248 | do { |
| 3249 | buflen += chunk; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3250 | #ifdef MS_WINDOWS |
| 3251 | if (buflen > INT_MAX) { |
| 3252 | PyErr_NoMemory(); |
| 3253 | break; |
| 3254 | } |
| 3255 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3256 | tmpbuf = PyMem_RawRealloc(buf, buflen); |
| 3257 | if (tmpbuf == NULL) |
| 3258 | break; |
| 3259 | |
| 3260 | buf = tmpbuf; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3261 | #ifdef MS_WINDOWS |
| 3262 | cwd = getcwd(buf, (int)buflen); |
| 3263 | #else |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3264 | cwd = getcwd(buf, buflen); |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3265 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3266 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3267 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3268 | |
| 3269 | if (cwd == NULL) { |
| 3270 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3271 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3272 | } |
| 3273 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3274 | if (use_bytes) |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3275 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 3276 | else |
| 3277 | obj = PyUnicode_DecodeFSDefault(buf); |
| 3278 | PyMem_RawFree(buf); |
| 3279 | |
| 3280 | return obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3281 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3283 | |
| 3284 | /*[clinic input] |
| 3285 | os.getcwd |
| 3286 | |
| 3287 | Return a unicode string representing the current working directory. |
| 3288 | [clinic start generated code]*/ |
| 3289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3290 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3291 | os_getcwd_impl(PyObject *module) |
| 3292 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3293 | { |
| 3294 | return posix_getcwd(0); |
| 3295 | } |
| 3296 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3297 | |
| 3298 | /*[clinic input] |
| 3299 | os.getcwdb |
| 3300 | |
| 3301 | Return a bytes string representing the current working directory. |
| 3302 | [clinic start generated code]*/ |
| 3303 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3304 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3305 | os_getcwdb_impl(PyObject *module) |
| 3306 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3307 | { |
| 3308 | return posix_getcwd(1); |
| 3309 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3310 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3311 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3312 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3313 | #define HAVE_LINK 1 |
| 3314 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3315 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3316 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3317 | /*[clinic input] |
| 3318 | |
| 3319 | os.link |
| 3320 | |
| 3321 | src : path_t |
| 3322 | dst : path_t |
| 3323 | * |
| 3324 | src_dir_fd : dir_fd = None |
| 3325 | dst_dir_fd : dir_fd = None |
| 3326 | follow_symlinks: bool = True |
| 3327 | |
| 3328 | Create a hard link to a file. |
| 3329 | |
| 3330 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3331 | descriptor open to a directory, and the respective path string (src or dst) |
| 3332 | should be relative; the path will then be relative to that directory. |
| 3333 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3334 | link, link will create a link to the symbolic link itself instead of the |
| 3335 | file the link points to. |
| 3336 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3337 | platform. If they are unavailable, using them will raise a |
| 3338 | NotImplementedError. |
| 3339 | [clinic start generated code]*/ |
| 3340 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3341 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3342 | 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] | 3343 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3344 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3345 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3346 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3347 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3348 | #else |
| 3349 | int result; |
| 3350 | #endif |
| 3351 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3352 | #ifndef HAVE_LINKAT |
| 3353 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3354 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3355 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3356 | } |
| 3357 | #endif |
| 3358 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3359 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3360 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3361 | PyErr_SetString(PyExc_NotImplementedError, |
| 3362 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3363 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3364 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3365 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3366 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3367 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3368 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3369 | if (src->wide) |
| 3370 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3371 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3372 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3373 | if (!result) |
| 3374 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3375 | #else |
| 3376 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3377 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3378 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3379 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3380 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3381 | result = linkat(src_dir_fd, src->narrow, |
| 3382 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3383 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3384 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3385 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3386 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3387 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3388 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3389 | if (result) |
| 3390 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3391 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3392 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3393 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3394 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3395 | #endif |
| 3396 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3397 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3398 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3399 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3400 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3401 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3402 | PyObject *v; |
| 3403 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3404 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3405 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3406 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3407 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3408 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3409 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3410 | WIN32_FIND_DATAW wFileData; |
| 3411 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3412 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3413 | if (!path->wide) { /* Default arg: "." */ |
| 3414 | po_wchars = L"."; |
| 3415 | len = 1; |
| 3416 | } else { |
| 3417 | po_wchars = path->wide; |
| 3418 | len = wcslen(path->wide); |
| 3419 | } |
| 3420 | /* The +5 is so we can append "\\*.*\0" */ |
| 3421 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3422 | if (!wnamebuf) { |
| 3423 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3424 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3425 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3426 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3427 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3428 | wchar_t wch = wnamebuf[len-1]; |
| 3429 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3430 | wnamebuf[len++] = SEP; |
| 3431 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3432 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3433 | if ((list = PyList_New(0)) == NULL) { |
| 3434 | goto exit; |
| 3435 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3436 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3437 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3438 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3439 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3440 | int error = GetLastError(); |
| 3441 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3442 | goto exit; |
| 3443 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3444 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3445 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3446 | } |
| 3447 | do { |
| 3448 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3449 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3450 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3451 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3452 | wcslen(wFileData.cFileName)); |
| 3453 | if (path->narrow && v) { |
| 3454 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3455 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3456 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3457 | Py_DECREF(list); |
| 3458 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3459 | break; |
| 3460 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3461 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3462 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3463 | Py_DECREF(list); |
| 3464 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3465 | break; |
| 3466 | } |
| 3467 | Py_DECREF(v); |
| 3468 | } |
| 3469 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3470 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3471 | Py_END_ALLOW_THREADS |
| 3472 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3473 | it got to the end of the directory. */ |
| 3474 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3475 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3476 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3477 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3478 | } |
| 3479 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3480 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3481 | exit: |
| 3482 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3483 | if (FindClose(hFindFile) == FALSE) { |
| 3484 | if (list != NULL) { |
| 3485 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3486 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3487 | } |
| 3488 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3489 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3490 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3491 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3492 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3493 | } /* end of _listdir_windows_no_opendir */ |
| 3494 | |
| 3495 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3496 | |
| 3497 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3498 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3499 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3500 | PyObject *v; |
| 3501 | DIR *dirp = NULL; |
| 3502 | struct dirent *ep; |
| 3503 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3504 | #ifdef HAVE_FDOPENDIR |
| 3505 | int fd = -1; |
| 3506 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3507 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3508 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3509 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3510 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3511 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3512 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3513 | if (fd == -1) |
| 3514 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3515 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3516 | return_str = 1; |
| 3517 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3518 | Py_BEGIN_ALLOW_THREADS |
| 3519 | dirp = fdopendir(fd); |
| 3520 | Py_END_ALLOW_THREADS |
| 3521 | } |
| 3522 | else |
| 3523 | #endif |
| 3524 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3525 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3526 | if (path->narrow) { |
| 3527 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3528 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3529 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3530 | } |
| 3531 | else { |
| 3532 | name = "."; |
| 3533 | return_str = 1; |
| 3534 | } |
| 3535 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3536 | Py_BEGIN_ALLOW_THREADS |
| 3537 | dirp = opendir(name); |
| 3538 | Py_END_ALLOW_THREADS |
| 3539 | } |
| 3540 | |
| 3541 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3542 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3543 | #ifdef HAVE_FDOPENDIR |
| 3544 | if (fd != -1) { |
| 3545 | Py_BEGIN_ALLOW_THREADS |
| 3546 | close(fd); |
| 3547 | Py_END_ALLOW_THREADS |
| 3548 | } |
| 3549 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3550 | goto exit; |
| 3551 | } |
| 3552 | if ((list = PyList_New(0)) == NULL) { |
| 3553 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3554 | } |
| 3555 | for (;;) { |
| 3556 | errno = 0; |
| 3557 | Py_BEGIN_ALLOW_THREADS |
| 3558 | ep = readdir(dirp); |
| 3559 | Py_END_ALLOW_THREADS |
| 3560 | if (ep == NULL) { |
| 3561 | if (errno == 0) { |
| 3562 | break; |
| 3563 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3564 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3565 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3566 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3567 | } |
| 3568 | } |
| 3569 | if (ep->d_name[0] == '.' && |
| 3570 | (NAMLEN(ep) == 1 || |
| 3571 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3572 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3573 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3574 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3575 | else |
| 3576 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3577 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3578 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3579 | break; |
| 3580 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3581 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3582 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3583 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3584 | break; |
| 3585 | } |
| 3586 | Py_DECREF(v); |
| 3587 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3588 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3589 | exit: |
| 3590 | if (dirp != NULL) { |
| 3591 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3592 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3593 | if (fd > -1) |
| 3594 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3595 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3596 | closedir(dirp); |
| 3597 | Py_END_ALLOW_THREADS |
| 3598 | } |
| 3599 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3600 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3601 | } /* end of _posix_listdir */ |
| 3602 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3603 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3604 | |
| 3605 | /*[clinic input] |
| 3606 | os.listdir |
| 3607 | |
| 3608 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3609 | |
| 3610 | Return a list containing the names of the files in the directory. |
| 3611 | |
| 3612 | path can be specified as either str or bytes. If path is bytes, |
| 3613 | the filenames returned will also be bytes; in all other circumstances |
| 3614 | the filenames returned will be str. |
| 3615 | If path is None, uses the path='.'. |
| 3616 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3617 | the file descriptor must refer to a directory. |
| 3618 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3619 | |
| 3620 | The list is in arbitrary order. It does not include the special |
| 3621 | entries '.' and '..' even if they are present in the directory. |
| 3622 | |
| 3623 | |
| 3624 | [clinic start generated code]*/ |
| 3625 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3626 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3627 | os_listdir_impl(PyObject *module, path_t *path) |
| 3628 | /*[clinic end generated code: output=293045673fcd1a75 input=09e300416e3cd729]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3629 | { |
| 3630 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3631 | return _listdir_windows_no_opendir(path, NULL); |
| 3632 | #else |
| 3633 | return _posix_listdir(path, NULL); |
| 3634 | #endif |
| 3635 | } |
| 3636 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3637 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3638 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3639 | /*[clinic input] |
| 3640 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3641 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3642 | path: path_t |
| 3643 | / |
| 3644 | |
| 3645 | [clinic start generated code]*/ |
| 3646 | |
| 3647 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3648 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 3649 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3650 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3651 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
| 3652 | wchar_t *wtemp; |
| 3653 | DWORD result; |
| 3654 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3655 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3656 | result = GetFullPathNameW(path->wide, |
| 3657 | Py_ARRAY_LENGTH(woutbuf), |
| 3658 | woutbuf, &wtemp); |
| 3659 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
| 3660 | woutbufp = PyMem_New(wchar_t, result); |
| 3661 | if (!woutbufp) |
| 3662 | return PyErr_NoMemory(); |
| 3663 | result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3664 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3665 | if (result) { |
| 3666 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
| 3667 | if (path->narrow) |
| 3668 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3669 | } else |
| 3670 | v = win32_error_object("GetFullPathNameW", path->object); |
| 3671 | if (woutbufp != woutbuf) |
| 3672 | PyMem_Free(woutbufp); |
| 3673 | return v; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3674 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3675 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3676 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3677 | /*[clinic input] |
| 3678 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3679 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3680 | path: unicode |
| 3681 | / |
| 3682 | |
| 3683 | A helper function for samepath on windows. |
| 3684 | [clinic start generated code]*/ |
| 3685 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3686 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3687 | os__getfinalpathname_impl(PyObject *module, PyObject *path) |
| 3688 | /*[clinic end generated code: output=9bd78d0e52782e75 input=71d5e89334891bf4]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3689 | { |
| 3690 | HANDLE hFile; |
| 3691 | int buf_size; |
| 3692 | wchar_t *target_path; |
| 3693 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3694 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3695 | const wchar_t *path_wchar; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3696 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3697 | path_wchar = PyUnicode_AsUnicode(path); |
| 3698 | if (path_wchar == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3699 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3700 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3701 | hFile = CreateFileW( |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3702 | path_wchar, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3703 | 0, /* desired access */ |
| 3704 | 0, /* share mode */ |
| 3705 | NULL, /* security attributes */ |
| 3706 | OPEN_EXISTING, |
| 3707 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3708 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3709 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3710 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3711 | if(hFile == INVALID_HANDLE_VALUE) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3712 | return win32_error_object("CreateFileW", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3713 | |
| 3714 | /* We have a good handle to the target, use it to determine the |
| 3715 | target path name. */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 3716 | buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3717 | |
| 3718 | if(!buf_size) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3719 | return win32_error_object("GetFinalPathNameByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3720 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3721 | target_path = PyMem_New(wchar_t, buf_size+1); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3722 | if(!target_path) |
| 3723 | return PyErr_NoMemory(); |
| 3724 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 3725 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 3726 | buf_size, VOLUME_NAME_DOS); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3727 | if(!result_length) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3728 | return win32_error_object("GetFinalPathNamyByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3729 | |
| 3730 | if(!CloseHandle(hFile)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3731 | return win32_error_object("CloseHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3732 | |
| 3733 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3734 | result = PyUnicode_FromWideChar(target_path, result_length); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3735 | PyMem_Free(target_path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3736 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3737 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3738 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3739 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3740 | "Return true if the pathname refers to an existing directory."); |
| 3741 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3742 | /*[clinic input] |
| 3743 | os._isdir |
| 3744 | |
| 3745 | path: path_t |
| 3746 | / |
| 3747 | |
| 3748 | [clinic start generated code]*/ |
| 3749 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3750 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3751 | os__isdir_impl(PyObject *module, path_t *path) |
| 3752 | /*[clinic end generated code: output=75f56f32720836cb input=e794f12faab62a2a]*/ |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3753 | { |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3754 | DWORD attributes; |
| 3755 | |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3756 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3757 | attributes = GetFileAttributesW(path->wide); |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3758 | Py_END_ALLOW_THREADS |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3759 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3760 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3761 | Py_RETURN_FALSE; |
| 3762 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3763 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3764 | Py_RETURN_TRUE; |
| 3765 | else |
| 3766 | Py_RETURN_FALSE; |
| 3767 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3768 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3769 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3770 | /*[clinic input] |
| 3771 | os._getvolumepathname |
| 3772 | |
| 3773 | path: unicode |
| 3774 | |
| 3775 | A helper function for ismount on Win32. |
| 3776 | [clinic start generated code]*/ |
| 3777 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3778 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3779 | os__getvolumepathname_impl(PyObject *module, PyObject *path) |
| 3780 | /*[clinic end generated code: output=cbdcbd1059ceef4c input=7eacadc40acbda6b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3781 | { |
| 3782 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3783 | const wchar_t *path_wchar; |
| 3784 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3785 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3786 | BOOL ret; |
| 3787 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3788 | path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen); |
| 3789 | if (path_wchar == NULL) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3790 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3791 | buflen += 1; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3792 | |
| 3793 | /* Volume path should be shorter than entire path */ |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3794 | buflen = Py_MAX(buflen, MAX_PATH); |
| 3795 | |
| 3796 | if (buflen > DWORD_MAX) { |
| 3797 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 3798 | return NULL; |
| 3799 | } |
| 3800 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3801 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3802 | if (mountpath == NULL) |
| 3803 | return PyErr_NoMemory(); |
| 3804 | |
| 3805 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3806 | ret = GetVolumePathNameW(path_wchar, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3807 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3808 | Py_END_ALLOW_THREADS |
| 3809 | |
| 3810 | if (!ret) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3811 | result = win32_error_object("_getvolumepathname", path); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3812 | goto exit; |
| 3813 | } |
| 3814 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
| 3815 | |
| 3816 | exit: |
| 3817 | PyMem_Free(mountpath); |
| 3818 | return result; |
| 3819 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3820 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3821 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3822 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3823 | |
| 3824 | /*[clinic input] |
| 3825 | os.mkdir |
| 3826 | |
| 3827 | path : path_t |
| 3828 | |
| 3829 | mode: int = 0o777 |
| 3830 | |
| 3831 | * |
| 3832 | |
| 3833 | dir_fd : dir_fd(requires='mkdirat') = None |
| 3834 | |
| 3835 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3836 | |
| 3837 | Create a directory. |
| 3838 | |
| 3839 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3840 | and path should be relative; path will then be relative to that directory. |
| 3841 | dir_fd may not be implemented on your platform. |
| 3842 | If it is unavailable, using it will raise a NotImplementedError. |
| 3843 | |
| 3844 | The mode argument is ignored on Windows. |
| 3845 | [clinic start generated code]*/ |
| 3846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3847 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3848 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 3849 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3850 | { |
| 3851 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3852 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3853 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3854 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3855 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3856 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3857 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3858 | if (!result) |
| 3859 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3860 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3861 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3862 | #if HAVE_MKDIRAT |
| 3863 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3864 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3865 | else |
| 3866 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3867 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3868 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3869 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3870 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3871 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3872 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3873 | if (result < 0) |
| 3874 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3875 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3876 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3879 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3880 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3881 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3882 | #include <sys/resource.h> |
| 3883 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3884 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3885 | |
| 3886 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3887 | /*[clinic input] |
| 3888 | os.nice |
| 3889 | |
| 3890 | increment: int |
| 3891 | / |
| 3892 | |
| 3893 | Add increment to the priority of process and return the new priority. |
| 3894 | [clinic start generated code]*/ |
| 3895 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3896 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3897 | os_nice_impl(PyObject *module, int increment) |
| 3898 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3899 | { |
| 3900 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3901 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3902 | /* There are two flavours of 'nice': one that returns the new |
| 3903 | priority (as required by almost all standards out there) and the |
| 3904 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3905 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3906 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3907 | If we are of the nice family that returns the new priority, we |
| 3908 | need to clear errno before the call, and check if errno is filled |
| 3909 | before calling posix_error() on a returnvalue of -1, because the |
| 3910 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3911 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3912 | errno = 0; |
| 3913 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3914 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3915 | if (value == 0) |
| 3916 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3917 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3918 | if (value == -1 && errno != 0) |
| 3919 | /* either nice() or getpriority() returned an error */ |
| 3920 | return posix_error(); |
| 3921 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3922 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3923 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3924 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3925 | |
| 3926 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3927 | /*[clinic input] |
| 3928 | os.getpriority |
| 3929 | |
| 3930 | which: int |
| 3931 | who: int |
| 3932 | |
| 3933 | Return program scheduling priority. |
| 3934 | [clinic start generated code]*/ |
| 3935 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3936 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3937 | os_getpriority_impl(PyObject *module, int which, int who) |
| 3938 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3939 | { |
| 3940 | int retval; |
| 3941 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3942 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3943 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3944 | if (errno != 0) |
| 3945 | return posix_error(); |
| 3946 | return PyLong_FromLong((long)retval); |
| 3947 | } |
| 3948 | #endif /* HAVE_GETPRIORITY */ |
| 3949 | |
| 3950 | |
| 3951 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3952 | /*[clinic input] |
| 3953 | os.setpriority |
| 3954 | |
| 3955 | which: int |
| 3956 | who: int |
| 3957 | priority: int |
| 3958 | |
| 3959 | Set program scheduling priority. |
| 3960 | [clinic start generated code]*/ |
| 3961 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3962 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3963 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 3964 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3965 | { |
| 3966 | int retval; |
| 3967 | |
| 3968 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3969 | if (retval == -1) |
| 3970 | return posix_error(); |
| 3971 | Py_RETURN_NONE; |
| 3972 | } |
| 3973 | #endif /* HAVE_SETPRIORITY */ |
| 3974 | |
| 3975 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3976 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3977 | 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] | 3978 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3979 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3980 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3981 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3982 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3983 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3984 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3985 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3986 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3987 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3988 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3989 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 3990 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 3991 | #ifndef HAVE_RENAMEAT |
| 3992 | if (dir_fd_specified) { |
| 3993 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3994 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3995 | } |
| 3996 | #endif |
| 3997 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3998 | #ifdef MS_WINDOWS |
| 3999 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4000 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4001 | Py_END_ALLOW_THREADS |
| 4002 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4003 | if (!result) |
| 4004 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4005 | |
| 4006 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4007 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4008 | PyErr_Format(PyExc_ValueError, |
| 4009 | "%s: src and dst must be the same type", function_name); |
| 4010 | return NULL; |
| 4011 | } |
| 4012 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4013 | Py_BEGIN_ALLOW_THREADS |
| 4014 | #ifdef HAVE_RENAMEAT |
| 4015 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4016 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4017 | else |
| 4018 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4019 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4020 | Py_END_ALLOW_THREADS |
| 4021 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4022 | if (result) |
| 4023 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4024 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4025 | Py_RETURN_NONE; |
| 4026 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4027 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4028 | |
| 4029 | /*[clinic input] |
| 4030 | os.rename |
| 4031 | |
| 4032 | src : path_t |
| 4033 | dst : path_t |
| 4034 | * |
| 4035 | src_dir_fd : dir_fd = None |
| 4036 | dst_dir_fd : dir_fd = None |
| 4037 | |
| 4038 | Rename a file or directory. |
| 4039 | |
| 4040 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4041 | descriptor open to a directory, and the respective path string (src or dst) |
| 4042 | should be relative; the path will then be relative to that directory. |
| 4043 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4044 | If they are unavailable, using them will raise a NotImplementedError. |
| 4045 | [clinic start generated code]*/ |
| 4046 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4047 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4048 | 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] | 4049 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4050 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4051 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4052 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4053 | } |
| 4054 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4055 | |
| 4056 | /*[clinic input] |
| 4057 | os.replace = os.rename |
| 4058 | |
| 4059 | Rename a file or directory, overwriting the destination. |
| 4060 | |
| 4061 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4062 | descriptor open to a directory, and the respective path string (src or dst) |
| 4063 | should be relative; the path will then be relative to that directory. |
| 4064 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4065 | If they are unavailable, using them will raise a NotImplementedError." |
| 4066 | [clinic start generated code]*/ |
| 4067 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4068 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4069 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4070 | int dst_dir_fd) |
| 4071 | /*[clinic end generated code: output=1968c02e7857422b input=25515dfb107c8421]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4072 | { |
| 4073 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4074 | } |
| 4075 | |
| 4076 | |
| 4077 | /*[clinic input] |
| 4078 | os.rmdir |
| 4079 | |
| 4080 | path: path_t |
| 4081 | * |
| 4082 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4083 | |
| 4084 | Remove a directory. |
| 4085 | |
| 4086 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4087 | and path should be relative; path will then be relative to that directory. |
| 4088 | dir_fd may not be implemented on your platform. |
| 4089 | If it is unavailable, using it will raise a NotImplementedError. |
| 4090 | [clinic start generated code]*/ |
| 4091 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4092 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4093 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4094 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4095 | { |
| 4096 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4097 | |
| 4098 | Py_BEGIN_ALLOW_THREADS |
| 4099 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4100 | /* Windows, success=1, UNIX, success=0 */ |
| 4101 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4102 | #else |
| 4103 | #ifdef HAVE_UNLINKAT |
| 4104 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4105 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4106 | else |
| 4107 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4108 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4109 | #endif |
| 4110 | Py_END_ALLOW_THREADS |
| 4111 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4112 | if (result) |
| 4113 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4114 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4115 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4118 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4119 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4120 | #ifdef MS_WINDOWS |
| 4121 | /*[clinic input] |
| 4122 | os.system -> long |
| 4123 | |
| 4124 | command: Py_UNICODE |
| 4125 | |
| 4126 | Execute the command in a subshell. |
| 4127 | [clinic start generated code]*/ |
| 4128 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4129 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4130 | os_system_impl(PyObject *module, Py_UNICODE *command) |
| 4131 | /*[clinic end generated code: output=96c4dffee36dfb48 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4132 | { |
| 4133 | long result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4134 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4135 | result = _wsystem(command); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4136 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4137 | return result; |
| 4138 | } |
| 4139 | #else /* MS_WINDOWS */ |
| 4140 | /*[clinic input] |
| 4141 | os.system -> long |
| 4142 | |
| 4143 | command: FSConverter |
| 4144 | |
| 4145 | Execute the command in a subshell. |
| 4146 | [clinic start generated code]*/ |
| 4147 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4148 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4149 | os_system_impl(PyObject *module, PyObject *command) |
| 4150 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4151 | { |
| 4152 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4153 | const char *bytes = PyBytes_AsString(command); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4154 | Py_BEGIN_ALLOW_THREADS |
| 4155 | result = system(bytes); |
| 4156 | Py_END_ALLOW_THREADS |
| 4157 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4158 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4159 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4160 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4161 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4162 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4163 | /*[clinic input] |
| 4164 | os.umask |
| 4165 | |
| 4166 | mask: int |
| 4167 | / |
| 4168 | |
| 4169 | Set the current numeric umask and return the previous umask. |
| 4170 | [clinic start generated code]*/ |
| 4171 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4172 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4173 | os_umask_impl(PyObject *module, int mask) |
| 4174 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4175 | { |
| 4176 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4177 | if (i < 0) |
| 4178 | return posix_error(); |
| 4179 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4180 | } |
| 4181 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4182 | #ifdef MS_WINDOWS |
| 4183 | |
| 4184 | /* override the default DeleteFileW behavior so that directory |
| 4185 | symlinks can be removed with this function, the same as with |
| 4186 | Unix symlinks */ |
| 4187 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4188 | { |
| 4189 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4190 | WIN32_FIND_DATAW find_data; |
| 4191 | HANDLE find_data_handle; |
| 4192 | int is_directory = 0; |
| 4193 | int is_link = 0; |
| 4194 | |
| 4195 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4196 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4197 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4198 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4199 | it is a symlink */ |
| 4200 | if(is_directory && |
| 4201 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4202 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4203 | |
| 4204 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4205 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4206 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4207 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4208 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4209 | FindClose(find_data_handle); |
| 4210 | } |
| 4211 | } |
| 4212 | } |
| 4213 | |
| 4214 | if (is_directory && is_link) |
| 4215 | return RemoveDirectoryW(lpFileName); |
| 4216 | |
| 4217 | return DeleteFileW(lpFileName); |
| 4218 | } |
| 4219 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4220 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4222 | /*[clinic input] |
| 4223 | os.unlink |
| 4224 | |
| 4225 | path: path_t |
| 4226 | * |
| 4227 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4228 | |
| 4229 | Remove a file (same as remove()). |
| 4230 | |
| 4231 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4232 | and path should be relative; path will then be relative to that directory. |
| 4233 | dir_fd may not be implemented on your platform. |
| 4234 | If it is unavailable, using it will raise a NotImplementedError. |
| 4235 | |
| 4236 | [clinic start generated code]*/ |
| 4237 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4238 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4239 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4240 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4241 | { |
| 4242 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4243 | |
| 4244 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4245 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4246 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4247 | /* Windows, success=1, UNIX, success=0 */ |
| 4248 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4249 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4250 | #ifdef HAVE_UNLINKAT |
| 4251 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4252 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4253 | else |
| 4254 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4255 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4256 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4257 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4258 | Py_END_ALLOW_THREADS |
| 4259 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4260 | if (result) |
| 4261 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4262 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4263 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4266 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4267 | /*[clinic input] |
| 4268 | os.remove = os.unlink |
| 4269 | |
| 4270 | Remove a file (same as unlink()). |
| 4271 | |
| 4272 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4273 | and path should be relative; path will then be relative to that directory. |
| 4274 | dir_fd may not be implemented on your platform. |
| 4275 | If it is unavailable, using it will raise a NotImplementedError. |
| 4276 | [clinic start generated code]*/ |
| 4277 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4278 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4279 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4280 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4281 | { |
| 4282 | return os_unlink_impl(module, path, dir_fd); |
| 4283 | } |
| 4284 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4285 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4286 | static PyStructSequence_Field uname_result_fields[] = { |
| 4287 | {"sysname", "operating system name"}, |
| 4288 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4289 | {"release", "operating system release"}, |
| 4290 | {"version", "operating system version"}, |
| 4291 | {"machine", "hardware identifier"}, |
| 4292 | {NULL} |
| 4293 | }; |
| 4294 | |
| 4295 | PyDoc_STRVAR(uname_result__doc__, |
| 4296 | "uname_result: Result from os.uname().\n\n\ |
| 4297 | This object may be accessed either as a tuple of\n\ |
| 4298 | (sysname, nodename, release, version, machine),\n\ |
| 4299 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4300 | \n\ |
| 4301 | See os.uname for more information."); |
| 4302 | |
| 4303 | static PyStructSequence_Desc uname_result_desc = { |
| 4304 | "uname_result", /* name */ |
| 4305 | uname_result__doc__, /* doc */ |
| 4306 | uname_result_fields, |
| 4307 | 5 |
| 4308 | }; |
| 4309 | |
| 4310 | static PyTypeObject UnameResultType; |
| 4311 | |
| 4312 | |
| 4313 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4314 | /*[clinic input] |
| 4315 | os.uname |
| 4316 | |
| 4317 | Return an object identifying the current operating system. |
| 4318 | |
| 4319 | The object behaves like a named tuple with the following fields: |
| 4320 | (sysname, nodename, release, version, machine) |
| 4321 | |
| 4322 | [clinic start generated code]*/ |
| 4323 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4324 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4325 | os_uname_impl(PyObject *module) |
| 4326 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4327 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4328 | struct utsname u; |
| 4329 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4330 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4331 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4332 | Py_BEGIN_ALLOW_THREADS |
| 4333 | res = uname(&u); |
| 4334 | Py_END_ALLOW_THREADS |
| 4335 | if (res < 0) |
| 4336 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4337 | |
| 4338 | value = PyStructSequence_New(&UnameResultType); |
| 4339 | if (value == NULL) |
| 4340 | return NULL; |
| 4341 | |
| 4342 | #define SET(i, field) \ |
| 4343 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4344 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4345 | if (!o) { \ |
| 4346 | Py_DECREF(value); \ |
| 4347 | return NULL; \ |
| 4348 | } \ |
| 4349 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4350 | } \ |
| 4351 | |
| 4352 | SET(0, u.sysname); |
| 4353 | SET(1, u.nodename); |
| 4354 | SET(2, u.release); |
| 4355 | SET(3, u.version); |
| 4356 | SET(4, u.machine); |
| 4357 | |
| 4358 | #undef SET |
| 4359 | |
| 4360 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4361 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4362 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4363 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4364 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4365 | |
| 4366 | typedef struct { |
| 4367 | int now; |
| 4368 | time_t atime_s; |
| 4369 | long atime_ns; |
| 4370 | time_t mtime_s; |
| 4371 | long mtime_ns; |
| 4372 | } utime_t; |
| 4373 | |
| 4374 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4375 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4376 | * they also intentionally leak the declaration of a pointer named "time" |
| 4377 | */ |
| 4378 | #define UTIME_TO_TIMESPEC \ |
| 4379 | struct timespec ts[2]; \ |
| 4380 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4381 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4382 | time = NULL; \ |
| 4383 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4384 | ts[0].tv_sec = ut->atime_s; \ |
| 4385 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4386 | ts[1].tv_sec = ut->mtime_s; \ |
| 4387 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4388 | time = ts; \ |
| 4389 | } \ |
| 4390 | |
| 4391 | #define UTIME_TO_TIMEVAL \ |
| 4392 | struct timeval tv[2]; \ |
| 4393 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4394 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4395 | time = NULL; \ |
| 4396 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4397 | tv[0].tv_sec = ut->atime_s; \ |
| 4398 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4399 | tv[1].tv_sec = ut->mtime_s; \ |
| 4400 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4401 | time = tv; \ |
| 4402 | } \ |
| 4403 | |
| 4404 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4405 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4406 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4407 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4408 | time = NULL; \ |
| 4409 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4410 | u.actime = ut->atime_s; \ |
| 4411 | u.modtime = ut->mtime_s; \ |
| 4412 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | #define UTIME_TO_TIME_T \ |
| 4416 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4417 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4418 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4419 | time = NULL; \ |
| 4420 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4421 | timet[0] = ut->atime_s; \ |
| 4422 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4423 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4424 | } \ |
| 4425 | |
| 4426 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4427 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4428 | |
| 4429 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4430 | 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] | 4431 | { |
| 4432 | #ifdef HAVE_UTIMENSAT |
| 4433 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4434 | UTIME_TO_TIMESPEC; |
| 4435 | return utimensat(dir_fd, path, time, flags); |
| 4436 | #elif defined(HAVE_FUTIMESAT) |
| 4437 | UTIME_TO_TIMEVAL; |
| 4438 | /* |
| 4439 | * follow_symlinks will never be false here; |
| 4440 | * we only allow !follow_symlinks and dir_fd together |
| 4441 | * if we have utimensat() |
| 4442 | */ |
| 4443 | assert(follow_symlinks); |
| 4444 | return futimesat(dir_fd, path, time); |
| 4445 | #endif |
| 4446 | } |
| 4447 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4448 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4449 | #else |
| 4450 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4451 | #endif |
| 4452 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4453 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4454 | |
| 4455 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4456 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4457 | { |
| 4458 | #ifdef HAVE_FUTIMENS |
| 4459 | UTIME_TO_TIMESPEC; |
| 4460 | return futimens(fd, time); |
| 4461 | #else |
| 4462 | UTIME_TO_TIMEVAL; |
| 4463 | return futimes(fd, time); |
| 4464 | #endif |
| 4465 | } |
| 4466 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4467 | #define PATH_UTIME_HAVE_FD 1 |
| 4468 | #else |
| 4469 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4470 | #endif |
| 4471 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4472 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4473 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4474 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4475 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4476 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4477 | |
| 4478 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4479 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4480 | { |
| 4481 | #ifdef HAVE_UTIMENSAT |
| 4482 | UTIME_TO_TIMESPEC; |
| 4483 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4484 | #else |
| 4485 | UTIME_TO_TIMEVAL; |
| 4486 | return lutimes(path, time); |
| 4487 | #endif |
| 4488 | } |
| 4489 | |
| 4490 | #endif |
| 4491 | |
| 4492 | #ifndef MS_WINDOWS |
| 4493 | |
| 4494 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4495 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4496 | { |
| 4497 | #ifdef HAVE_UTIMENSAT |
| 4498 | UTIME_TO_TIMESPEC; |
| 4499 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4500 | #elif defined(HAVE_UTIMES) |
| 4501 | UTIME_TO_TIMEVAL; |
| 4502 | return utimes(path, time); |
| 4503 | #elif defined(HAVE_UTIME_H) |
| 4504 | UTIME_TO_UTIMBUF; |
| 4505 | return utime(path, time); |
| 4506 | #else |
| 4507 | UTIME_TO_TIME_T; |
| 4508 | return utime(path, time); |
| 4509 | #endif |
| 4510 | } |
| 4511 | |
| 4512 | #endif |
| 4513 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4514 | static int |
| 4515 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4516 | { |
| 4517 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4518 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4519 | divmod = PyNumber_Divmod(py_long, billion); |
| 4520 | if (!divmod) |
| 4521 | goto exit; |
| 4522 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4523 | if ((*s == -1) && PyErr_Occurred()) |
| 4524 | goto exit; |
| 4525 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4526 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4527 | goto exit; |
| 4528 | |
| 4529 | result = 1; |
| 4530 | exit: |
| 4531 | Py_XDECREF(divmod); |
| 4532 | return result; |
| 4533 | } |
| 4534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4535 | |
| 4536 | /*[clinic input] |
| 4537 | os.utime |
| 4538 | |
| 4539 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
| 4540 | times: object = NULL |
| 4541 | * |
| 4542 | ns: object = NULL |
| 4543 | dir_fd: dir_fd(requires='futimensat') = None |
| 4544 | follow_symlinks: bool=True |
| 4545 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4546 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4547 | |
| 4548 | Set the access and modified time of path. |
| 4549 | |
| 4550 | path may always be specified as a string. |
| 4551 | On some platforms, path may also be specified as an open file descriptor. |
| 4552 | If this functionality is unavailable, using it raises an exception. |
| 4553 | |
| 4554 | If times is not None, it must be a tuple (atime, mtime); |
| 4555 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4556 | 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] | 4557 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4558 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4559 | 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] | 4560 | Specifying tuples for both times and ns is an error. |
| 4561 | |
| 4562 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4563 | and path should be relative; path will then be relative to that directory. |
| 4564 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4565 | link, utime will modify the symbolic link itself instead of the file the |
| 4566 | link points to. |
| 4567 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4568 | as an open file descriptor. |
| 4569 | dir_fd and follow_symlinks may not be available on your platform. |
| 4570 | If they are unavailable, using them will raise a NotImplementedError. |
| 4571 | |
| 4572 | [clinic start generated code]*/ |
| 4573 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4574 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4575 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4576 | int dir_fd, int follow_symlinks) |
| 4577 | /*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4578 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4579 | #ifdef MS_WINDOWS |
| 4580 | HANDLE hFile; |
| 4581 | FILETIME atime, mtime; |
| 4582 | #else |
| 4583 | int result; |
| 4584 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4585 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4586 | PyObject *return_value = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4587 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4588 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4589 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4590 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4591 | if (times && (times != Py_None) && ns) { |
| 4592 | PyErr_SetString(PyExc_ValueError, |
| 4593 | "utime: you may specify either 'times'" |
| 4594 | " or 'ns' but not both"); |
| 4595 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4599 | time_t a_sec, m_sec; |
| 4600 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4601 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4602 | PyErr_SetString(PyExc_TypeError, |
| 4603 | "utime: 'times' must be either" |
| 4604 | " a tuple of two ints or None"); |
| 4605 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4606 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4607 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4608 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4609 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4610 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4611 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4612 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4613 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4614 | utime.atime_s = a_sec; |
| 4615 | utime.atime_ns = a_nsec; |
| 4616 | utime.mtime_s = m_sec; |
| 4617 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4618 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4619 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4620 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4621 | PyErr_SetString(PyExc_TypeError, |
| 4622 | "utime: 'ns' must be a tuple of two ints"); |
| 4623 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4624 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4625 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4626 | 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] | 4627 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4628 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4629 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4630 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4631 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4632 | } |
| 4633 | else { |
| 4634 | /* times and ns are both None/unspecified. use "now". */ |
| 4635 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4636 | } |
| 4637 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4638 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4639 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4640 | goto exit; |
| 4641 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4642 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4643 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4644 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4645 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4646 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4647 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4648 | #if !defined(HAVE_UTIMENSAT) |
| 4649 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4650 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4651 | "utime: cannot use dir_fd and follow_symlinks " |
| 4652 | "together on this platform"); |
| 4653 | goto exit; |
| 4654 | } |
| 4655 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4656 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4657 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4658 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4659 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 4660 | NULL, OPEN_EXISTING, |
| 4661 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4662 | Py_END_ALLOW_THREADS |
| 4663 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4664 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4665 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4666 | } |
| 4667 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4668 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4669 | GetSystemTimeAsFileTime(&mtime); |
| 4670 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4671 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4672 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4673 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4674 | _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] | 4675 | } |
| 4676 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4677 | /* Avoid putting the file name into the error here, |
| 4678 | as that may confuse the user into believing that |
| 4679 | something is wrong with the file, when it also |
| 4680 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4681 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4682 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4683 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4684 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4685 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4686 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4687 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4688 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4689 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4690 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4691 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4692 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4693 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4694 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4695 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4696 | else |
| 4697 | #endif |
| 4698 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4699 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4700 | if (path->fd != -1) |
| 4701 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4702 | else |
| 4703 | #endif |
| 4704 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4705 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4706 | |
| 4707 | Py_END_ALLOW_THREADS |
| 4708 | |
| 4709 | if (result < 0) { |
| 4710 | /* see previous comment about not putting filename in error here */ |
| 4711 | return_value = posix_error(); |
| 4712 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4713 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4714 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4715 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4716 | |
| 4717 | Py_INCREF(Py_None); |
| 4718 | return_value = Py_None; |
| 4719 | |
| 4720 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4721 | #ifdef MS_WINDOWS |
| 4722 | if (hFile != INVALID_HANDLE_VALUE) |
| 4723 | CloseHandle(hFile); |
| 4724 | #endif |
| 4725 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4726 | } |
| 4727 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4728 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4729 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4730 | |
| 4731 | /*[clinic input] |
| 4732 | os._exit |
| 4733 | |
| 4734 | status: int |
| 4735 | |
| 4736 | Exit to the system with specified status, without normal exit processing. |
| 4737 | [clinic start generated code]*/ |
| 4738 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4739 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4740 | os__exit_impl(PyObject *module, int status) |
| 4741 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4742 | { |
| 4743 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4744 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4747 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4748 | #define EXECV_CHAR wchar_t |
| 4749 | #else |
| 4750 | #define EXECV_CHAR char |
| 4751 | #endif |
| 4752 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4753 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4754 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4755 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4756 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4757 | Py_ssize_t i; |
| 4758 | for (i = 0; i < count; i++) |
| 4759 | PyMem_Free(array[i]); |
| 4760 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4761 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4762 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4763 | static |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4764 | int fsconvert_strdup(PyObject *o, EXECV_CHAR**out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4765 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4766 | Py_ssize_t size; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4767 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4768 | *out = PyUnicode_AsWideCharString(o, &size); |
| 4769 | if (!*out) |
| 4770 | return 0; |
| 4771 | #else |
| 4772 | PyObject *bytes; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4773 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4774 | return 0; |
| 4775 | size = PyBytes_GET_SIZE(bytes); |
| 4776 | *out = PyMem_Malloc(size+1); |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4777 | if (!*out) { |
| 4778 | PyErr_NoMemory(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4779 | return 0; |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4780 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4781 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4782 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4783 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4784 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4785 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4786 | #endif |
| 4787 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4788 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4789 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4790 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4791 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4792 | Py_ssize_t i, pos, envc; |
| 4793 | PyObject *keys=NULL, *vals=NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4794 | PyObject *key, *val, *keyval; |
| 4795 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4796 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4797 | i = PyMapping_Size(env); |
| 4798 | if (i < 0) |
| 4799 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4800 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4801 | if (envlist == NULL) { |
| 4802 | PyErr_NoMemory(); |
| 4803 | return NULL; |
| 4804 | } |
| 4805 | envc = 0; |
| 4806 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4807 | if (!keys) |
| 4808 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4809 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4810 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4811 | goto error; |
| 4812 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4813 | PyErr_Format(PyExc_TypeError, |
| 4814 | "env.keys() or env.values() is not a list"); |
| 4815 | goto error; |
| 4816 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4817 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4818 | for (pos = 0; pos < i; pos++) { |
| 4819 | key = PyList_GetItem(keys, pos); |
| 4820 | val = PyList_GetItem(vals, pos); |
| 4821 | if (!key || !val) |
| 4822 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4823 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4824 | keyval = PyUnicode_FromFormat("%U=%U", key, val); |
| 4825 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4826 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4827 | |
| 4828 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 4829 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4830 | goto error; |
| 4831 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4832 | |
| 4833 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4834 | } |
| 4835 | Py_DECREF(vals); |
| 4836 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4837 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4838 | envlist[envc] = 0; |
| 4839 | *envc_ptr = envc; |
| 4840 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4841 | |
| 4842 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4843 | Py_XDECREF(keys); |
| 4844 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4845 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4846 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4847 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4848 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4849 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4850 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4851 | { |
| 4852 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4853 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4854 | if (argvlist == NULL) { |
| 4855 | PyErr_NoMemory(); |
| 4856 | return NULL; |
| 4857 | } |
| 4858 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4859 | PyObject* item = PySequence_ITEM(argv, i); |
| 4860 | if (item == NULL) |
| 4861 | goto fail; |
| 4862 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4863 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4864 | goto fail; |
| 4865 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4866 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4867 | } |
| 4868 | argvlist[*argc] = NULL; |
| 4869 | return argvlist; |
| 4870 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4871 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4872 | free_string_array(argvlist, *argc); |
| 4873 | return NULL; |
| 4874 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4875 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4876 | #endif |
| 4877 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4878 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4879 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4880 | /*[clinic input] |
| 4881 | os.execv |
| 4882 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4883 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4884 | Path of executable file. |
| 4885 | argv: object |
| 4886 | Tuple or list of strings. |
| 4887 | / |
| 4888 | |
| 4889 | Execute an executable path with arguments, replacing current process. |
| 4890 | [clinic start generated code]*/ |
| 4891 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4892 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4893 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 4894 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4895 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4896 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4897 | Py_ssize_t argc; |
| 4898 | |
| 4899 | /* execv has two arguments: (path, argv), where |
| 4900 | argv is a list or tuple of strings. */ |
| 4901 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4902 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4903 | PyErr_SetString(PyExc_TypeError, |
| 4904 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4905 | return NULL; |
| 4906 | } |
| 4907 | argc = PySequence_Size(argv); |
| 4908 | if (argc < 1) { |
| 4909 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4910 | return NULL; |
| 4911 | } |
| 4912 | |
| 4913 | argvlist = parse_arglist(argv, &argc); |
| 4914 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4915 | return NULL; |
| 4916 | } |
| 4917 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4918 | #ifdef HAVE_WEXECV |
| 4919 | _wexecv(path->wide, argvlist); |
| 4920 | #else |
| 4921 | execv(path->narrow, argvlist); |
| 4922 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4923 | |
| 4924 | /* If we get here it's definitely an error */ |
| 4925 | |
| 4926 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4927 | return posix_error(); |
| 4928 | } |
| 4929 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4930 | |
| 4931 | /*[clinic input] |
| 4932 | os.execve |
| 4933 | |
| 4934 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 4935 | Path of executable file. |
| 4936 | argv: object |
| 4937 | Tuple or list of strings. |
| 4938 | env: object |
| 4939 | Dictionary of strings mapping to strings. |
| 4940 | |
| 4941 | Execute an executable path with arguments, replacing current process. |
| 4942 | [clinic start generated code]*/ |
| 4943 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4944 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4945 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 4946 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4947 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4948 | EXECV_CHAR **argvlist = NULL; |
| 4949 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4950 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4951 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4952 | /* execve has three arguments: (path, argv, env), where |
| 4953 | argv is a list or tuple of strings and env is a dictionary |
| 4954 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4955 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4956 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4957 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4958 | "execve: argv must be a tuple or list"); |
| 4959 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4960 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4961 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4962 | if (!PyMapping_Check(env)) { |
| 4963 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4964 | "execve: environment must be a mapping object"); |
| 4965 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4966 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4967 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4968 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4969 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4970 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4971 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4972 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4973 | envlist = parse_envlist(env, &envc); |
| 4974 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4975 | goto fail; |
| 4976 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4977 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4978 | if (path->fd > -1) |
| 4979 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4980 | else |
| 4981 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4982 | #ifdef HAVE_WEXECV |
| 4983 | _wexecve(path->wide, argvlist, envlist); |
| 4984 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4985 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4986 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4987 | |
| 4988 | /* If we get here it's definitely an error */ |
| 4989 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4990 | path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4991 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4992 | free_string_array(envlist, envc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4993 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4994 | if (argvlist) |
| 4995 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4996 | return NULL; |
| 4997 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4998 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4999 | #endif /* HAVE_EXECV */ |
| 5000 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5001 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5002 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5003 | /*[clinic input] |
| 5004 | os.spawnv |
| 5005 | |
| 5006 | mode: int |
| 5007 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5008 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5009 | Path of executable file. |
| 5010 | argv: object |
| 5011 | Tuple or list of strings. |
| 5012 | / |
| 5013 | |
| 5014 | Execute the program specified by path in a new process. |
| 5015 | [clinic start generated code]*/ |
| 5016 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5017 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5018 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5019 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5020 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5021 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5022 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5023 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5024 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5025 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5026 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5027 | /* spawnv has three arguments: (mode, path, argv), where |
| 5028 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5029 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5030 | if (PyList_Check(argv)) { |
| 5031 | argc = PyList_Size(argv); |
| 5032 | getitem = PyList_GetItem; |
| 5033 | } |
| 5034 | else if (PyTuple_Check(argv)) { |
| 5035 | argc = PyTuple_Size(argv); |
| 5036 | getitem = PyTuple_GetItem; |
| 5037 | } |
| 5038 | else { |
| 5039 | PyErr_SetString(PyExc_TypeError, |
| 5040 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5041 | return NULL; |
| 5042 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5043 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5044 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5045 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5046 | return PyErr_NoMemory(); |
| 5047 | } |
| 5048 | for (i = 0; i < argc; i++) { |
| 5049 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5050 | &argvlist[i])) { |
| 5051 | free_string_array(argvlist, i); |
| 5052 | PyErr_SetString( |
| 5053 | PyExc_TypeError, |
| 5054 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5055 | return NULL; |
| 5056 | } |
| 5057 | } |
| 5058 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5059 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5060 | if (mode == _OLD_P_OVERLAY) |
| 5061 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5062 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5063 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5064 | #ifdef HAVE_WSPAWNV |
| 5065 | spawnval = _wspawnv(mode, path->wide, argvlist); |
| 5066 | #else |
| 5067 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 5068 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5069 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5070 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5071 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5072 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5073 | if (spawnval == -1) |
| 5074 | return posix_error(); |
| 5075 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5076 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5080 | /*[clinic input] |
| 5081 | os.spawnve |
| 5082 | |
| 5083 | mode: int |
| 5084 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5085 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5086 | Path of executable file. |
| 5087 | argv: object |
| 5088 | Tuple or list of strings. |
| 5089 | env: object |
| 5090 | Dictionary of strings mapping to strings. |
| 5091 | / |
| 5092 | |
| 5093 | Execute the program specified by path in a new process. |
| 5094 | [clinic start generated code]*/ |
| 5095 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5096 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5097 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5098 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5099 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5100 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5101 | EXECV_CHAR **argvlist; |
| 5102 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5103 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5104 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5105 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5106 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5107 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5108 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5109 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5110 | argv is a list or tuple of strings and env is a dictionary |
| 5111 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5112 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5113 | if (PyList_Check(argv)) { |
| 5114 | argc = PyList_Size(argv); |
| 5115 | getitem = PyList_GetItem; |
| 5116 | } |
| 5117 | else if (PyTuple_Check(argv)) { |
| 5118 | argc = PyTuple_Size(argv); |
| 5119 | getitem = PyTuple_GetItem; |
| 5120 | } |
| 5121 | else { |
| 5122 | PyErr_SetString(PyExc_TypeError, |
| 5123 | "spawnve() arg 2 must be a tuple or list"); |
| 5124 | goto fail_0; |
| 5125 | } |
| 5126 | if (!PyMapping_Check(env)) { |
| 5127 | PyErr_SetString(PyExc_TypeError, |
| 5128 | "spawnve() arg 3 must be a mapping object"); |
| 5129 | goto fail_0; |
| 5130 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5131 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5132 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5133 | if (argvlist == NULL) { |
| 5134 | PyErr_NoMemory(); |
| 5135 | goto fail_0; |
| 5136 | } |
| 5137 | for (i = 0; i < argc; i++) { |
| 5138 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5139 | &argvlist[i])) |
| 5140 | { |
| 5141 | lastarg = i; |
| 5142 | goto fail_1; |
| 5143 | } |
| 5144 | } |
| 5145 | lastarg = argc; |
| 5146 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5147 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5148 | envlist = parse_envlist(env, &envc); |
| 5149 | if (envlist == NULL) |
| 5150 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5151 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5152 | if (mode == _OLD_P_OVERLAY) |
| 5153 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5154 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5155 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5156 | #ifdef HAVE_WSPAWNV |
| 5157 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
| 5158 | #else |
| 5159 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 5160 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5161 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5162 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5163 | if (spawnval == -1) |
| 5164 | (void) posix_error(); |
| 5165 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5166 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5167 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5168 | while (--envc >= 0) |
| 5169 | PyMem_DEL(envlist[envc]); |
| 5170 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5171 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5172 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5173 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5174 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5175 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5176 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5177 | #endif /* HAVE_SPAWNV */ |
| 5178 | |
| 5179 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5180 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5181 | /*[clinic input] |
| 5182 | os.fork1 |
| 5183 | |
| 5184 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 5185 | |
| 5186 | Return 0 to child process and PID of child to parent process. |
| 5187 | [clinic start generated code]*/ |
| 5188 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5189 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5190 | os_fork1_impl(PyObject *module) |
| 5191 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5192 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5193 | pid_t pid; |
| 5194 | int result = 0; |
| 5195 | _PyImport_AcquireLock(); |
| 5196 | pid = fork1(); |
| 5197 | if (pid == 0) { |
| 5198 | /* child: this clobbers and resets the import lock. */ |
| 5199 | PyOS_AfterFork(); |
| 5200 | } else { |
| 5201 | /* parent: release the import lock. */ |
| 5202 | result = _PyImport_ReleaseLock(); |
| 5203 | } |
| 5204 | if (pid == -1) |
| 5205 | return posix_error(); |
| 5206 | if (result < 0) { |
| 5207 | /* Don't clobber the OSError if the fork failed. */ |
| 5208 | PyErr_SetString(PyExc_RuntimeError, |
| 5209 | "not holding the import lock"); |
| 5210 | return NULL; |
| 5211 | } |
| 5212 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5213 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5214 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5215 | |
| 5216 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5217 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5218 | /*[clinic input] |
| 5219 | os.fork |
| 5220 | |
| 5221 | Fork a child process. |
| 5222 | |
| 5223 | Return 0 to child process and PID of child to parent process. |
| 5224 | [clinic start generated code]*/ |
| 5225 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5226 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5227 | os_fork_impl(PyObject *module) |
| 5228 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5229 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5230 | pid_t pid; |
| 5231 | int result = 0; |
| 5232 | _PyImport_AcquireLock(); |
| 5233 | pid = fork(); |
| 5234 | if (pid == 0) { |
| 5235 | /* child: this clobbers and resets the import lock. */ |
| 5236 | PyOS_AfterFork(); |
| 5237 | } else { |
| 5238 | /* parent: release the import lock. */ |
| 5239 | result = _PyImport_ReleaseLock(); |
| 5240 | } |
| 5241 | if (pid == -1) |
| 5242 | return posix_error(); |
| 5243 | if (result < 0) { |
| 5244 | /* Don't clobber the OSError if the fork failed. */ |
| 5245 | PyErr_SetString(PyExc_RuntimeError, |
| 5246 | "not holding the import lock"); |
| 5247 | return NULL; |
| 5248 | } |
| 5249 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5250 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5251 | #endif /* HAVE_FORK */ |
| 5252 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5253 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5254 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5255 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5256 | /*[clinic input] |
| 5257 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5258 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5259 | policy: int |
| 5260 | |
| 5261 | Get the maximum scheduling priority for policy. |
| 5262 | [clinic start generated code]*/ |
| 5263 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5264 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5265 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 5266 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5267 | { |
| 5268 | int max; |
| 5269 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5270 | max = sched_get_priority_max(policy); |
| 5271 | if (max < 0) |
| 5272 | return posix_error(); |
| 5273 | return PyLong_FromLong(max); |
| 5274 | } |
| 5275 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5276 | |
| 5277 | /*[clinic input] |
| 5278 | os.sched_get_priority_min |
| 5279 | |
| 5280 | policy: int |
| 5281 | |
| 5282 | Get the minimum scheduling priority for policy. |
| 5283 | [clinic start generated code]*/ |
| 5284 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5285 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5286 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 5287 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5288 | { |
| 5289 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5290 | if (min < 0) |
| 5291 | return posix_error(); |
| 5292 | return PyLong_FromLong(min); |
| 5293 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5294 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5295 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5296 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5297 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5298 | /*[clinic input] |
| 5299 | os.sched_getscheduler |
| 5300 | pid: pid_t |
| 5301 | / |
| 5302 | |
| 5303 | Get the scheduling policy for the process identifiedy by pid. |
| 5304 | |
| 5305 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 5306 | [clinic start generated code]*/ |
| 5307 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5308 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5309 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
| 5310 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5311 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5312 | int policy; |
| 5313 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5314 | policy = sched_getscheduler(pid); |
| 5315 | if (policy < 0) |
| 5316 | return posix_error(); |
| 5317 | return PyLong_FromLong(policy); |
| 5318 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5319 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5320 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5321 | |
| 5322 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5323 | /*[clinic input] |
| 5324 | class os.sched_param "PyObject *" "&SchedParamType" |
| 5325 | |
| 5326 | @classmethod |
| 5327 | os.sched_param.__new__ |
| 5328 | |
| 5329 | sched_priority: object |
| 5330 | A scheduling parameter. |
| 5331 | |
| 5332 | Current has only one field: sched_priority"); |
| 5333 | [clinic start generated code]*/ |
| 5334 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5335 | static PyObject * |
| 5336 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5337 | /*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5338 | { |
| 5339 | PyObject *res; |
| 5340 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5341 | res = PyStructSequence_New(type); |
| 5342 | if (!res) |
| 5343 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5344 | Py_INCREF(sched_priority); |
| 5345 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5346 | return res; |
| 5347 | } |
| 5348 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5349 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5350 | PyDoc_VAR(os_sched_param__doc__); |
| 5351 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5352 | static PyStructSequence_Field sched_param_fields[] = { |
| 5353 | {"sched_priority", "the scheduling priority"}, |
| 5354 | {0} |
| 5355 | }; |
| 5356 | |
| 5357 | static PyStructSequence_Desc sched_param_desc = { |
| 5358 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5359 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5360 | sched_param_fields, |
| 5361 | 1 |
| 5362 | }; |
| 5363 | |
| 5364 | static int |
| 5365 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5366 | { |
| 5367 | long priority; |
| 5368 | |
| 5369 | if (Py_TYPE(param) != &SchedParamType) { |
| 5370 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5371 | return 0; |
| 5372 | } |
| 5373 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5374 | if (priority == -1 && PyErr_Occurred()) |
| 5375 | return 0; |
| 5376 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5377 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5378 | return 0; |
| 5379 | } |
| 5380 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5381 | return 1; |
| 5382 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5383 | #endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5384 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5385 | |
| 5386 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5387 | /*[clinic input] |
| 5388 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5389 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5390 | pid: pid_t |
| 5391 | policy: int |
| 5392 | param: sched_param |
| 5393 | / |
| 5394 | |
| 5395 | Set the scheduling policy for the process identified by pid. |
| 5396 | |
| 5397 | If pid is 0, the calling process is changed. |
| 5398 | param is an instance of sched_param. |
| 5399 | [clinic start generated code]*/ |
| 5400 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5401 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5402 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5403 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5404 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5405 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5406 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5407 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5408 | ** scheduling policy under Solaris/Illumos, and others. |
| 5409 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5410 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5411 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5412 | return posix_error(); |
| 5413 | Py_RETURN_NONE; |
| 5414 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5415 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5416 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5417 | |
| 5418 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5419 | /*[clinic input] |
| 5420 | os.sched_getparam |
| 5421 | pid: pid_t |
| 5422 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5423 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5424 | Returns scheduling parameters for the process identified by pid. |
| 5425 | |
| 5426 | If pid is 0, returns parameters for the calling process. |
| 5427 | Return value is an instance of sched_param. |
| 5428 | [clinic start generated code]*/ |
| 5429 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5430 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5431 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 5432 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5433 | { |
| 5434 | struct sched_param param; |
| 5435 | PyObject *result; |
| 5436 | PyObject *priority; |
| 5437 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5438 | if (sched_getparam(pid, ¶m)) |
| 5439 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5440 | result = PyStructSequence_New(&SchedParamType); |
| 5441 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5442 | return NULL; |
| 5443 | priority = PyLong_FromLong(param.sched_priority); |
| 5444 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5445 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5446 | return NULL; |
| 5447 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5448 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 5449 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5450 | } |
| 5451 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5452 | |
| 5453 | /*[clinic input] |
| 5454 | os.sched_setparam |
| 5455 | pid: pid_t |
| 5456 | param: sched_param |
| 5457 | / |
| 5458 | |
| 5459 | Set scheduling parameters for the process identified by pid. |
| 5460 | |
| 5461 | If pid is 0, sets parameters for the calling process. |
| 5462 | param should be an instance of sched_param. |
| 5463 | [clinic start generated code]*/ |
| 5464 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5465 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5466 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5467 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5468 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5469 | { |
| 5470 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5471 | return posix_error(); |
| 5472 | Py_RETURN_NONE; |
| 5473 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5474 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5475 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5476 | |
| 5477 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5478 | /*[clinic input] |
| 5479 | os.sched_rr_get_interval -> double |
| 5480 | pid: pid_t |
| 5481 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5482 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5483 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 5484 | |
| 5485 | Value returned is a float. |
| 5486 | [clinic start generated code]*/ |
| 5487 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5488 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5489 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 5490 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5491 | { |
| 5492 | struct timespec interval; |
| 5493 | if (sched_rr_get_interval(pid, &interval)) { |
| 5494 | posix_error(); |
| 5495 | return -1.0; |
| 5496 | } |
| 5497 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 5498 | } |
| 5499 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5500 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5501 | |
| 5502 | /*[clinic input] |
| 5503 | os.sched_yield |
| 5504 | |
| 5505 | Voluntarily relinquish the CPU. |
| 5506 | [clinic start generated code]*/ |
| 5507 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5508 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5509 | os_sched_yield_impl(PyObject *module) |
| 5510 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5511 | { |
| 5512 | if (sched_yield()) |
| 5513 | return posix_error(); |
| 5514 | Py_RETURN_NONE; |
| 5515 | } |
| 5516 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5517 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5518 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5519 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5520 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5521 | /*[clinic input] |
| 5522 | os.sched_setaffinity |
| 5523 | pid: pid_t |
| 5524 | mask : object |
| 5525 | / |
| 5526 | |
| 5527 | Set the CPU affinity of the process identified by pid to mask. |
| 5528 | |
| 5529 | mask should be an iterable of integers identifying CPUs. |
| 5530 | [clinic start generated code]*/ |
| 5531 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5532 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5533 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 5534 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5535 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5536 | int ncpus; |
| 5537 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5538 | cpu_set_t *cpu_set = NULL; |
| 5539 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5540 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5541 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5542 | if (iterator == NULL) |
| 5543 | return NULL; |
| 5544 | |
| 5545 | ncpus = NCPUS_START; |
| 5546 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5547 | cpu_set = CPU_ALLOC(ncpus); |
| 5548 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5549 | PyErr_NoMemory(); |
| 5550 | goto error; |
| 5551 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5552 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5553 | |
| 5554 | while ((item = PyIter_Next(iterator))) { |
| 5555 | long cpu; |
| 5556 | if (!PyLong_Check(item)) { |
| 5557 | PyErr_Format(PyExc_TypeError, |
| 5558 | "expected an iterator of ints, " |
| 5559 | "but iterator yielded %R", |
| 5560 | Py_TYPE(item)); |
| 5561 | Py_DECREF(item); |
| 5562 | goto error; |
| 5563 | } |
| 5564 | cpu = PyLong_AsLong(item); |
| 5565 | Py_DECREF(item); |
| 5566 | if (cpu < 0) { |
| 5567 | if (!PyErr_Occurred()) |
| 5568 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5569 | goto error; |
| 5570 | } |
| 5571 | if (cpu > INT_MAX - 1) { |
| 5572 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5573 | goto error; |
| 5574 | } |
| 5575 | if (cpu >= ncpus) { |
| 5576 | /* Grow CPU mask to fit the CPU number */ |
| 5577 | int newncpus = ncpus; |
| 5578 | cpu_set_t *newmask; |
| 5579 | size_t newsetsize; |
| 5580 | while (newncpus <= cpu) { |
| 5581 | if (newncpus > INT_MAX / 2) |
| 5582 | newncpus = cpu + 1; |
| 5583 | else |
| 5584 | newncpus = newncpus * 2; |
| 5585 | } |
| 5586 | newmask = CPU_ALLOC(newncpus); |
| 5587 | if (newmask == NULL) { |
| 5588 | PyErr_NoMemory(); |
| 5589 | goto error; |
| 5590 | } |
| 5591 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5592 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5593 | memcpy(newmask, cpu_set, setsize); |
| 5594 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5595 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5596 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5597 | ncpus = newncpus; |
| 5598 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5599 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5600 | } |
| 5601 | Py_CLEAR(iterator); |
| 5602 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5603 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5604 | posix_error(); |
| 5605 | goto error; |
| 5606 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5607 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5608 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5609 | |
| 5610 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5611 | if (cpu_set) |
| 5612 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5613 | Py_XDECREF(iterator); |
| 5614 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5615 | } |
| 5616 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5617 | |
| 5618 | /*[clinic input] |
| 5619 | os.sched_getaffinity |
| 5620 | pid: pid_t |
| 5621 | / |
| 5622 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 5623 | 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] | 5624 | |
| 5625 | The affinity is returned as a set of CPU identifiers. |
| 5626 | [clinic start generated code]*/ |
| 5627 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5628 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5629 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 5630 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5631 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5632 | int cpu, ncpus, count; |
| 5633 | size_t setsize; |
| 5634 | cpu_set_t *mask = NULL; |
| 5635 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5636 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5637 | ncpus = NCPUS_START; |
| 5638 | while (1) { |
| 5639 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5640 | mask = CPU_ALLOC(ncpus); |
| 5641 | if (mask == NULL) |
| 5642 | return PyErr_NoMemory(); |
| 5643 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5644 | break; |
| 5645 | CPU_FREE(mask); |
| 5646 | if (errno != EINVAL) |
| 5647 | return posix_error(); |
| 5648 | if (ncpus > INT_MAX / 2) { |
| 5649 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5650 | "a large enough CPU set"); |
| 5651 | return NULL; |
| 5652 | } |
| 5653 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5654 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5655 | |
| 5656 | res = PySet_New(NULL); |
| 5657 | if (res == NULL) |
| 5658 | goto error; |
| 5659 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5660 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5661 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5662 | --count; |
| 5663 | if (cpu_num == NULL) |
| 5664 | goto error; |
| 5665 | if (PySet_Add(res, cpu_num)) { |
| 5666 | Py_DECREF(cpu_num); |
| 5667 | goto error; |
| 5668 | } |
| 5669 | Py_DECREF(cpu_num); |
| 5670 | } |
| 5671 | } |
| 5672 | CPU_FREE(mask); |
| 5673 | return res; |
| 5674 | |
| 5675 | error: |
| 5676 | if (mask) |
| 5677 | CPU_FREE(mask); |
| 5678 | Py_XDECREF(res); |
| 5679 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5680 | } |
| 5681 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5682 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5683 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5684 | #endif /* HAVE_SCHED_H */ |
| 5685 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5686 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5687 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5688 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5689 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5690 | #define DEV_PTY_FILE "/dev/ptc" |
| 5691 | #define HAVE_DEV_PTMX |
| 5692 | #else |
| 5693 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5694 | #endif |
| 5695 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5696 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5697 | #ifdef HAVE_PTY_H |
| 5698 | #include <pty.h> |
| 5699 | #else |
| 5700 | #ifdef HAVE_LIBUTIL_H |
| 5701 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5702 | #else |
| 5703 | #ifdef HAVE_UTIL_H |
| 5704 | #include <util.h> |
| 5705 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5706 | #endif /* HAVE_LIBUTIL_H */ |
| 5707 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5708 | #ifdef HAVE_STROPTS_H |
| 5709 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5710 | #endif |
| 5711 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5712 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5713 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5714 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5715 | /*[clinic input] |
| 5716 | os.openpty |
| 5717 | |
| 5718 | Open a pseudo-terminal. |
| 5719 | |
| 5720 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 5721 | for both the master and slave ends. |
| 5722 | [clinic start generated code]*/ |
| 5723 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5724 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5725 | os_openpty_impl(PyObject *module) |
| 5726 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5727 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5728 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5729 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5730 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5731 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5732 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5733 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5734 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5735 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5736 | #endif |
| 5737 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5738 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5739 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5740 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5741 | goto posix_error; |
| 5742 | |
| 5743 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5744 | goto error; |
| 5745 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 5746 | goto error; |
| 5747 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5748 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5749 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5750 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5751 | goto posix_error; |
| 5752 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5753 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5754 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5755 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5756 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5757 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5758 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5759 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5760 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5761 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5762 | goto posix_error; |
| 5763 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5764 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5765 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5766 | /* change permission of slave */ |
| 5767 | if (grantpt(master_fd) < 0) { |
| 5768 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5769 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5770 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5771 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5772 | /* unlock slave */ |
| 5773 | if (unlockpt(master_fd) < 0) { |
| 5774 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5775 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5776 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5777 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5778 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5779 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5780 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5781 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5782 | goto posix_error; |
| 5783 | |
| 5784 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5785 | if (slave_fd == -1) |
| 5786 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5787 | |
| 5788 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5789 | goto posix_error; |
| 5790 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 5791 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5792 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5793 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5794 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5795 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5796 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5797 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5798 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5799 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5800 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5801 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5802 | posix_error: |
| 5803 | posix_error(); |
| 5804 | error: |
| 5805 | if (master_fd != -1) |
| 5806 | close(master_fd); |
| 5807 | if (slave_fd != -1) |
| 5808 | close(slave_fd); |
| 5809 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5810 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5811 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5812 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5813 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5814 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5815 | /*[clinic input] |
| 5816 | os.forkpty |
| 5817 | |
| 5818 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 5819 | |
| 5820 | Returns a tuple of (pid, master_fd). |
| 5821 | Like fork(), return pid of 0 to the child process, |
| 5822 | and pid of child to the parent process. |
| 5823 | To both, return fd of newly opened pseudo-terminal. |
| 5824 | [clinic start generated code]*/ |
| 5825 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5826 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5827 | os_forkpty_impl(PyObject *module) |
| 5828 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5829 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5830 | int master_fd = -1, result = 0; |
| 5831 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5832 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5833 | _PyImport_AcquireLock(); |
| 5834 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5835 | if (pid == 0) { |
| 5836 | /* child: this clobbers and resets the import lock. */ |
| 5837 | PyOS_AfterFork(); |
| 5838 | } else { |
| 5839 | /* parent: release the import lock. */ |
| 5840 | result = _PyImport_ReleaseLock(); |
| 5841 | } |
| 5842 | if (pid == -1) |
| 5843 | return posix_error(); |
| 5844 | if (result < 0) { |
| 5845 | /* Don't clobber the OSError if the fork failed. */ |
| 5846 | PyErr_SetString(PyExc_RuntimeError, |
| 5847 | "not holding the import lock"); |
| 5848 | return NULL; |
| 5849 | } |
| 5850 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5851 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5852 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5853 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5854 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5855 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5856 | /*[clinic input] |
| 5857 | os.getegid |
| 5858 | |
| 5859 | Return the current process's effective group id. |
| 5860 | [clinic start generated code]*/ |
| 5861 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5862 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5863 | os_getegid_impl(PyObject *module) |
| 5864 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5865 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5866 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5867 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5868 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5869 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5870 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5871 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5872 | /*[clinic input] |
| 5873 | os.geteuid |
| 5874 | |
| 5875 | Return the current process's effective user id. |
| 5876 | [clinic start generated code]*/ |
| 5877 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5878 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5879 | os_geteuid_impl(PyObject *module) |
| 5880 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5881 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5882 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5883 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5884 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5885 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5886 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5887 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5888 | /*[clinic input] |
| 5889 | os.getgid |
| 5890 | |
| 5891 | Return the current process's group id. |
| 5892 | [clinic start generated code]*/ |
| 5893 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5894 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5895 | os_getgid_impl(PyObject *module) |
| 5896 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5897 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5898 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5899 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5900 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5901 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5902 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5903 | /*[clinic input] |
| 5904 | os.getpid |
| 5905 | |
| 5906 | Return the current process id. |
| 5907 | [clinic start generated code]*/ |
| 5908 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5909 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5910 | os_getpid_impl(PyObject *module) |
| 5911 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5912 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5913 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5914 | } |
| 5915 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5916 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5917 | |
| 5918 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5919 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5920 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5921 | Returns a list of groups to which a user belongs.\n\n\ |
| 5922 | user: username to lookup\n\ |
| 5923 | group: base group id of the user"); |
| 5924 | |
| 5925 | static PyObject * |
| 5926 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5927 | { |
| 5928 | #ifdef NGROUPS_MAX |
| 5929 | #define MAX_GROUPS NGROUPS_MAX |
| 5930 | #else |
| 5931 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5932 | #define MAX_GROUPS 64 |
| 5933 | #endif |
| 5934 | |
| 5935 | const char *user; |
| 5936 | int i, ngroups; |
| 5937 | PyObject *list; |
| 5938 | #ifdef __APPLE__ |
| 5939 | int *groups, basegid; |
| 5940 | #else |
| 5941 | gid_t *groups, basegid; |
| 5942 | #endif |
| 5943 | ngroups = MAX_GROUPS; |
| 5944 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5945 | #ifdef __APPLE__ |
| 5946 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5947 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5948 | #else |
| 5949 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 5950 | _Py_Gid_Converter, &basegid)) |
| 5951 | return NULL; |
| 5952 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5953 | |
| 5954 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5955 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5956 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5957 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5958 | #endif |
| 5959 | if (groups == NULL) |
| 5960 | return PyErr_NoMemory(); |
| 5961 | |
| 5962 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5963 | PyMem_Del(groups); |
| 5964 | return posix_error(); |
| 5965 | } |
| 5966 | |
| 5967 | list = PyList_New(ngroups); |
| 5968 | if (list == NULL) { |
| 5969 | PyMem_Del(groups); |
| 5970 | return NULL; |
| 5971 | } |
| 5972 | |
| 5973 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5974 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5975 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5976 | #else |
| 5977 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 5978 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5979 | if (o == NULL) { |
| 5980 | Py_DECREF(list); |
| 5981 | PyMem_Del(groups); |
| 5982 | return NULL; |
| 5983 | } |
| 5984 | PyList_SET_ITEM(list, i, o); |
| 5985 | } |
| 5986 | |
| 5987 | PyMem_Del(groups); |
| 5988 | |
| 5989 | return list; |
| 5990 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5991 | #endif /* HAVE_GETGROUPLIST */ |
| 5992 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5993 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5994 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5995 | /*[clinic input] |
| 5996 | os.getgroups |
| 5997 | |
| 5998 | Return list of supplemental group IDs for the process. |
| 5999 | [clinic start generated code]*/ |
| 6000 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6001 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6002 | os_getgroups_impl(PyObject *module) |
| 6003 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6004 | { |
| 6005 | PyObject *result = NULL; |
| 6006 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6007 | #ifdef NGROUPS_MAX |
| 6008 | #define MAX_GROUPS NGROUPS_MAX |
| 6009 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6010 | /* 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] | 6011 | #define MAX_GROUPS 64 |
| 6012 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6013 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6014 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6015 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6016 | * This is a helper variable to store the intermediate result when |
| 6017 | * that happens. |
| 6018 | * |
| 6019 | * To keep the code readable the OSX behaviour is unconditional, |
| 6020 | * according to the POSIX spec this should be safe on all unix-y |
| 6021 | * systems. |
| 6022 | */ |
| 6023 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6024 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6025 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6026 | #ifdef __APPLE__ |
| 6027 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6028 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6029 | * always first call getgroups with length 0 to get the actual number |
| 6030 | * of groups. |
| 6031 | */ |
| 6032 | n = getgroups(0, NULL); |
| 6033 | if (n < 0) { |
| 6034 | return posix_error(); |
| 6035 | } else if (n <= MAX_GROUPS) { |
| 6036 | /* groups will fit in existing array */ |
| 6037 | alt_grouplist = grouplist; |
| 6038 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6039 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6040 | if (alt_grouplist == NULL) { |
| 6041 | errno = EINVAL; |
| 6042 | return posix_error(); |
| 6043 | } |
| 6044 | } |
| 6045 | |
| 6046 | n = getgroups(n, alt_grouplist); |
| 6047 | if (n == -1) { |
| 6048 | if (alt_grouplist != grouplist) { |
| 6049 | PyMem_Free(alt_grouplist); |
| 6050 | } |
| 6051 | return posix_error(); |
| 6052 | } |
| 6053 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6054 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6055 | if (n < 0) { |
| 6056 | if (errno == EINVAL) { |
| 6057 | n = getgroups(0, NULL); |
| 6058 | if (n == -1) { |
| 6059 | return posix_error(); |
| 6060 | } |
| 6061 | if (n == 0) { |
| 6062 | /* Avoid malloc(0) */ |
| 6063 | alt_grouplist = grouplist; |
| 6064 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6065 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6066 | if (alt_grouplist == NULL) { |
| 6067 | errno = EINVAL; |
| 6068 | return posix_error(); |
| 6069 | } |
| 6070 | n = getgroups(n, alt_grouplist); |
| 6071 | if (n == -1) { |
| 6072 | PyMem_Free(alt_grouplist); |
| 6073 | return posix_error(); |
| 6074 | } |
| 6075 | } |
| 6076 | } else { |
| 6077 | return posix_error(); |
| 6078 | } |
| 6079 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6080 | #endif |
| 6081 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6082 | result = PyList_New(n); |
| 6083 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6084 | int i; |
| 6085 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6086 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6087 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6088 | Py_DECREF(result); |
| 6089 | result = NULL; |
| 6090 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6091 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6092 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6093 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6094 | } |
| 6095 | |
| 6096 | if (alt_grouplist != grouplist) { |
| 6097 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6098 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6099 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6100 | return result; |
| 6101 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6102 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6103 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6104 | #ifdef HAVE_INITGROUPS |
| 6105 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6106 | "initgroups(username, gid) -> None\n\n\ |
| 6107 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6108 | the groups of which the specified username is a member, plus the specified\n\ |
| 6109 | group id."); |
| 6110 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6111 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6112 | static PyObject * |
| 6113 | posix_initgroups(PyObject *self, PyObject *args) |
| 6114 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6115 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6116 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6117 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6118 | #ifdef __APPLE__ |
| 6119 | int gid; |
| 6120 | #else |
| 6121 | gid_t gid; |
| 6122 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6123 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6124 | #ifdef __APPLE__ |
| 6125 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6126 | PyUnicode_FSConverter, &oname, |
| 6127 | &gid)) |
| 6128 | #else |
| 6129 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6130 | PyUnicode_FSConverter, &oname, |
| 6131 | _Py_Gid_Converter, &gid)) |
| 6132 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6133 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6134 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6135 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6136 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6137 | Py_DECREF(oname); |
| 6138 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6139 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6140 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6141 | Py_INCREF(Py_None); |
| 6142 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6143 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6144 | #endif /* HAVE_INITGROUPS */ |
| 6145 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6146 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6147 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6148 | /*[clinic input] |
| 6149 | os.getpgid |
| 6150 | |
| 6151 | pid: pid_t |
| 6152 | |
| 6153 | Call the system call getpgid(), and return the result. |
| 6154 | [clinic start generated code]*/ |
| 6155 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6156 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6157 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 6158 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6159 | { |
| 6160 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6161 | if (pgid < 0) |
| 6162 | return posix_error(); |
| 6163 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6164 | } |
| 6165 | #endif /* HAVE_GETPGID */ |
| 6166 | |
| 6167 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6168 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6169 | /*[clinic input] |
| 6170 | os.getpgrp |
| 6171 | |
| 6172 | Return the current process group id. |
| 6173 | [clinic start generated code]*/ |
| 6174 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6175 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6176 | os_getpgrp_impl(PyObject *module) |
| 6177 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6178 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6179 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6180 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6181 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6182 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6183 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6184 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6185 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6186 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6187 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6188 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6189 | /*[clinic input] |
| 6190 | os.setpgrp |
| 6191 | |
| 6192 | Make the current process the leader of its process group. |
| 6193 | [clinic start generated code]*/ |
| 6194 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6195 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6196 | os_setpgrp_impl(PyObject *module) |
| 6197 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6198 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6199 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6200 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6201 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6202 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6203 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6204 | return posix_error(); |
| 6205 | Py_INCREF(Py_None); |
| 6206 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6207 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6208 | #endif /* HAVE_SETPGRP */ |
| 6209 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6210 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6211 | |
| 6212 | #ifdef MS_WINDOWS |
| 6213 | #include <tlhelp32.h> |
| 6214 | |
| 6215 | static PyObject* |
| 6216 | win32_getppid() |
| 6217 | { |
| 6218 | HANDLE snapshot; |
| 6219 | pid_t mypid; |
| 6220 | PyObject* result = NULL; |
| 6221 | BOOL have_record; |
| 6222 | PROCESSENTRY32 pe; |
| 6223 | |
| 6224 | mypid = getpid(); /* This function never fails */ |
| 6225 | |
| 6226 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6227 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6228 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6229 | |
| 6230 | pe.dwSize = sizeof(pe); |
| 6231 | have_record = Process32First(snapshot, &pe); |
| 6232 | while (have_record) { |
| 6233 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6234 | /* We could cache the ulong value in a static variable. */ |
| 6235 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6236 | break; |
| 6237 | } |
| 6238 | |
| 6239 | have_record = Process32Next(snapshot, &pe); |
| 6240 | } |
| 6241 | |
| 6242 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6243 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6244 | * error anyway, so let's raise it. */ |
| 6245 | if (!result) |
| 6246 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6247 | |
| 6248 | CloseHandle(snapshot); |
| 6249 | |
| 6250 | return result; |
| 6251 | } |
| 6252 | #endif /*MS_WINDOWS*/ |
| 6253 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6254 | |
| 6255 | /*[clinic input] |
| 6256 | os.getppid |
| 6257 | |
| 6258 | Return the parent's process id. |
| 6259 | |
| 6260 | If the parent process has already exited, Windows machines will still |
| 6261 | return its id; others systems will return the id of the 'init' process (1). |
| 6262 | [clinic start generated code]*/ |
| 6263 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6264 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6265 | os_getppid_impl(PyObject *module) |
| 6266 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6267 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6268 | #ifdef MS_WINDOWS |
| 6269 | return win32_getppid(); |
| 6270 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6271 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6272 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6273 | } |
| 6274 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6275 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6276 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6277 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6278 | /*[clinic input] |
| 6279 | os.getlogin |
| 6280 | |
| 6281 | Return the actual login name. |
| 6282 | [clinic start generated code]*/ |
| 6283 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6284 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6285 | os_getlogin_impl(PyObject *module) |
| 6286 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6287 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6288 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6289 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6290 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6291 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6292 | |
| 6293 | if (GetUserNameW(user_name, &num_chars)) { |
| 6294 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6295 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6296 | } |
| 6297 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6298 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6299 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6300 | char *name; |
| 6301 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6302 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6303 | errno = 0; |
| 6304 | name = getlogin(); |
| 6305 | if (name == NULL) { |
| 6306 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6307 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6308 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6309 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6310 | } |
| 6311 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6312 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6313 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6314 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6315 | return result; |
| 6316 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6317 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6318 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6319 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6320 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6321 | /*[clinic input] |
| 6322 | os.getuid |
| 6323 | |
| 6324 | Return the current process's user id. |
| 6325 | [clinic start generated code]*/ |
| 6326 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6327 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6328 | os_getuid_impl(PyObject *module) |
| 6329 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6330 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6331 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6332 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6333 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6334 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6335 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6336 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6337 | #define HAVE_KILL |
| 6338 | #endif /* MS_WINDOWS */ |
| 6339 | |
| 6340 | #ifdef HAVE_KILL |
| 6341 | /*[clinic input] |
| 6342 | os.kill |
| 6343 | |
| 6344 | pid: pid_t |
| 6345 | signal: Py_ssize_t |
| 6346 | / |
| 6347 | |
| 6348 | Kill a process with a signal. |
| 6349 | [clinic start generated code]*/ |
| 6350 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6351 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6352 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 6353 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6354 | #ifndef MS_WINDOWS |
| 6355 | { |
| 6356 | if (kill(pid, (int)signal) == -1) |
| 6357 | return posix_error(); |
| 6358 | Py_RETURN_NONE; |
| 6359 | } |
| 6360 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6361 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6362 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6363 | DWORD sig = (DWORD)signal; |
| 6364 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6365 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6366 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6367 | /* Console processes which share a common console can be sent CTRL+C or |
| 6368 | CTRL+BREAK events, provided they handle said events. */ |
| 6369 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6370 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6371 | err = GetLastError(); |
| 6372 | PyErr_SetFromWindowsErr(err); |
| 6373 | } |
| 6374 | else |
| 6375 | Py_RETURN_NONE; |
| 6376 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6377 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6378 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6379 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6380 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6381 | if (handle == NULL) { |
| 6382 | err = GetLastError(); |
| 6383 | return PyErr_SetFromWindowsErr(err); |
| 6384 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6385 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6386 | if (TerminateProcess(handle, sig) == 0) { |
| 6387 | err = GetLastError(); |
| 6388 | result = PyErr_SetFromWindowsErr(err); |
| 6389 | } else { |
| 6390 | Py_INCREF(Py_None); |
| 6391 | result = Py_None; |
| 6392 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6393 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6394 | CloseHandle(handle); |
| 6395 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6396 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6397 | #endif /* !MS_WINDOWS */ |
| 6398 | #endif /* HAVE_KILL */ |
| 6399 | |
| 6400 | |
| 6401 | #ifdef HAVE_KILLPG |
| 6402 | /*[clinic input] |
| 6403 | os.killpg |
| 6404 | |
| 6405 | pgid: pid_t |
| 6406 | signal: int |
| 6407 | / |
| 6408 | |
| 6409 | Kill a process group with a signal. |
| 6410 | [clinic start generated code]*/ |
| 6411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6412 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6413 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 6414 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6415 | { |
| 6416 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6417 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6418 | take the same type. Moreover, pid_t is always at least as wide as |
| 6419 | int (else compilation of this module fails), which is safe. */ |
| 6420 | if (killpg(pgid, signal) == -1) |
| 6421 | return posix_error(); |
| 6422 | Py_RETURN_NONE; |
| 6423 | } |
| 6424 | #endif /* HAVE_KILLPG */ |
| 6425 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6426 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6427 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6428 | #ifdef HAVE_SYS_LOCK_H |
| 6429 | #include <sys/lock.h> |
| 6430 | #endif |
| 6431 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6432 | /*[clinic input] |
| 6433 | os.plock |
| 6434 | op: int |
| 6435 | / |
| 6436 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6437 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6438 | [clinic start generated code]*/ |
| 6439 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6440 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6441 | os_plock_impl(PyObject *module, int op) |
| 6442 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6443 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6444 | if (plock(op) == -1) |
| 6445 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6446 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6447 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6448 | #endif /* HAVE_PLOCK */ |
| 6449 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6450 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6451 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6452 | /*[clinic input] |
| 6453 | os.setuid |
| 6454 | |
| 6455 | uid: uid_t |
| 6456 | / |
| 6457 | |
| 6458 | Set the current process's user id. |
| 6459 | [clinic start generated code]*/ |
| 6460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6461 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6462 | os_setuid_impl(PyObject *module, uid_t uid) |
| 6463 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6464 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6465 | if (setuid(uid) < 0) |
| 6466 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6467 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6468 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6469 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6470 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6471 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6472 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6473 | /*[clinic input] |
| 6474 | os.seteuid |
| 6475 | |
| 6476 | euid: uid_t |
| 6477 | / |
| 6478 | |
| 6479 | Set the current process's effective user id. |
| 6480 | [clinic start generated code]*/ |
| 6481 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6482 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6483 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 6484 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6485 | { |
| 6486 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6487 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6488 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6489 | } |
| 6490 | #endif /* HAVE_SETEUID */ |
| 6491 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6492 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6493 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6494 | /*[clinic input] |
| 6495 | os.setegid |
| 6496 | |
| 6497 | egid: gid_t |
| 6498 | / |
| 6499 | |
| 6500 | Set the current process's effective group id. |
| 6501 | [clinic start generated code]*/ |
| 6502 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6503 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6504 | os_setegid_impl(PyObject *module, gid_t egid) |
| 6505 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6506 | { |
| 6507 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6508 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6509 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6510 | } |
| 6511 | #endif /* HAVE_SETEGID */ |
| 6512 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6513 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6514 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6515 | /*[clinic input] |
| 6516 | os.setreuid |
| 6517 | |
| 6518 | ruid: uid_t |
| 6519 | euid: uid_t |
| 6520 | / |
| 6521 | |
| 6522 | Set the current process's real and effective user ids. |
| 6523 | [clinic start generated code]*/ |
| 6524 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6525 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6526 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 6527 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6528 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6529 | if (setreuid(ruid, euid) < 0) { |
| 6530 | return posix_error(); |
| 6531 | } else { |
| 6532 | Py_INCREF(Py_None); |
| 6533 | return Py_None; |
| 6534 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6535 | } |
| 6536 | #endif /* HAVE_SETREUID */ |
| 6537 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6538 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6539 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6540 | /*[clinic input] |
| 6541 | os.setregid |
| 6542 | |
| 6543 | rgid: gid_t |
| 6544 | egid: gid_t |
| 6545 | / |
| 6546 | |
| 6547 | Set the current process's real and effective group ids. |
| 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_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 6552 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6553 | { |
| 6554 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6555 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6556 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6557 | } |
| 6558 | #endif /* HAVE_SETREGID */ |
| 6559 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6560 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6561 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6562 | /*[clinic input] |
| 6563 | os.setgid |
| 6564 | gid: gid_t |
| 6565 | / |
| 6566 | |
| 6567 | Set the current process's group id. |
| 6568 | [clinic start generated code]*/ |
| 6569 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6570 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6571 | os_setgid_impl(PyObject *module, gid_t gid) |
| 6572 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6573 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6574 | if (setgid(gid) < 0) |
| 6575 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6576 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6577 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6578 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6579 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6580 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6581 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6582 | /*[clinic input] |
| 6583 | os.setgroups |
| 6584 | |
| 6585 | groups: object |
| 6586 | / |
| 6587 | |
| 6588 | Set the groups of the current process to list. |
| 6589 | [clinic start generated code]*/ |
| 6590 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6591 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6592 | os_setgroups(PyObject *module, PyObject *groups) |
| 6593 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6594 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6595 | int i, len; |
| 6596 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6597 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6598 | if (!PySequence_Check(groups)) { |
| 6599 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6600 | return NULL; |
| 6601 | } |
| 6602 | len = PySequence_Size(groups); |
| 6603 | if (len > MAX_GROUPS) { |
| 6604 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6605 | return NULL; |
| 6606 | } |
| 6607 | for(i = 0; i < len; i++) { |
| 6608 | PyObject *elem; |
| 6609 | elem = PySequence_GetItem(groups, i); |
| 6610 | if (!elem) |
| 6611 | return NULL; |
| 6612 | if (!PyLong_Check(elem)) { |
| 6613 | PyErr_SetString(PyExc_TypeError, |
| 6614 | "groups must be integers"); |
| 6615 | Py_DECREF(elem); |
| 6616 | return NULL; |
| 6617 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6618 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6619 | Py_DECREF(elem); |
| 6620 | return NULL; |
| 6621 | } |
| 6622 | } |
| 6623 | Py_DECREF(elem); |
| 6624 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6625 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6626 | if (setgroups(len, grouplist) < 0) |
| 6627 | return posix_error(); |
| 6628 | Py_INCREF(Py_None); |
| 6629 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6630 | } |
| 6631 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6632 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6633 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6634 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6635 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6636 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6637 | PyObject *result; |
| 6638 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6639 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6640 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6641 | if (pid == -1) |
| 6642 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6643 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6644 | if (struct_rusage == NULL) { |
| 6645 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6646 | if (m == NULL) |
| 6647 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6648 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6649 | Py_DECREF(m); |
| 6650 | if (struct_rusage == NULL) |
| 6651 | return NULL; |
| 6652 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6653 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6654 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6655 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6656 | if (!result) |
| 6657 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6658 | |
| 6659 | #ifndef doubletime |
| 6660 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6661 | #endif |
| 6662 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6663 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6664 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6665 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6666 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6667 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6668 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6669 | SET_INT(result, 2, ru->ru_maxrss); |
| 6670 | SET_INT(result, 3, ru->ru_ixrss); |
| 6671 | SET_INT(result, 4, ru->ru_idrss); |
| 6672 | SET_INT(result, 5, ru->ru_isrss); |
| 6673 | SET_INT(result, 6, ru->ru_minflt); |
| 6674 | SET_INT(result, 7, ru->ru_majflt); |
| 6675 | SET_INT(result, 8, ru->ru_nswap); |
| 6676 | SET_INT(result, 9, ru->ru_inblock); |
| 6677 | SET_INT(result, 10, ru->ru_oublock); |
| 6678 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6679 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6680 | SET_INT(result, 13, ru->ru_nsignals); |
| 6681 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6682 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6683 | #undef SET_INT |
| 6684 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6685 | if (PyErr_Occurred()) { |
| 6686 | Py_DECREF(result); |
| 6687 | return NULL; |
| 6688 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6689 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6690 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6691 | } |
| 6692 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6693 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6694 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6695 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6696 | /*[clinic input] |
| 6697 | os.wait3 |
| 6698 | |
| 6699 | options: int |
| 6700 | Wait for completion of a child process. |
| 6701 | |
| 6702 | Returns a tuple of information about the child process: |
| 6703 | (pid, status, rusage) |
| 6704 | [clinic start generated code]*/ |
| 6705 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6706 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6707 | os_wait3_impl(PyObject *module, int options) |
| 6708 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6709 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6710 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6711 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6712 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6713 | WAIT_TYPE status; |
| 6714 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6715 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6716 | do { |
| 6717 | Py_BEGIN_ALLOW_THREADS |
| 6718 | pid = wait3(&status, options, &ru); |
| 6719 | Py_END_ALLOW_THREADS |
| 6720 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6721 | if (pid < 0) |
| 6722 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6723 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6724 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6725 | } |
| 6726 | #endif /* HAVE_WAIT3 */ |
| 6727 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6728 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6729 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6730 | /*[clinic input] |
| 6731 | |
| 6732 | os.wait4 |
| 6733 | |
| 6734 | pid: pid_t |
| 6735 | options: int |
| 6736 | |
| 6737 | Wait for completion of a specific child process. |
| 6738 | |
| 6739 | Returns a tuple of information about the child process: |
| 6740 | (pid, status, rusage) |
| 6741 | [clinic start generated code]*/ |
| 6742 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6743 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6744 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 6745 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6746 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6747 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6748 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6749 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6750 | WAIT_TYPE status; |
| 6751 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6752 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6753 | do { |
| 6754 | Py_BEGIN_ALLOW_THREADS |
| 6755 | res = wait4(pid, &status, options, &ru); |
| 6756 | Py_END_ALLOW_THREADS |
| 6757 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6758 | if (res < 0) |
| 6759 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6760 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6761 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6762 | } |
| 6763 | #endif /* HAVE_WAIT4 */ |
| 6764 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6765 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6766 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6767 | /*[clinic input] |
| 6768 | os.waitid |
| 6769 | |
| 6770 | idtype: idtype_t |
| 6771 | Must be one of be P_PID, P_PGID or P_ALL. |
| 6772 | id: id_t |
| 6773 | The id to wait on. |
| 6774 | options: int |
| 6775 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 6776 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 6777 | / |
| 6778 | |
| 6779 | Returns the result of waiting for a process or processes. |
| 6780 | |
| 6781 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 6782 | no children in a waitable state. |
| 6783 | [clinic start generated code]*/ |
| 6784 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6785 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6786 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 6787 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6788 | { |
| 6789 | PyObject *result; |
| 6790 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6791 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6792 | siginfo_t si; |
| 6793 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6794 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6795 | do { |
| 6796 | Py_BEGIN_ALLOW_THREADS |
| 6797 | res = waitid(idtype, id, &si, options); |
| 6798 | Py_END_ALLOW_THREADS |
| 6799 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6800 | if (res < 0) |
| 6801 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6802 | |
| 6803 | if (si.si_pid == 0) |
| 6804 | Py_RETURN_NONE; |
| 6805 | |
| 6806 | result = PyStructSequence_New(&WaitidResultType); |
| 6807 | if (!result) |
| 6808 | return NULL; |
| 6809 | |
| 6810 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6811 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6812 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6813 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6814 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6815 | if (PyErr_Occurred()) { |
| 6816 | Py_DECREF(result); |
| 6817 | return NULL; |
| 6818 | } |
| 6819 | |
| 6820 | return result; |
| 6821 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6822 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6823 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6824 | |
| 6825 | #if defined(HAVE_WAITPID) |
| 6826 | /*[clinic input] |
| 6827 | os.waitpid |
| 6828 | pid: pid_t |
| 6829 | options: int |
| 6830 | / |
| 6831 | |
| 6832 | Wait for completion of a given child process. |
| 6833 | |
| 6834 | Returns a tuple of information regarding the child process: |
| 6835 | (pid, status) |
| 6836 | |
| 6837 | The options argument is ignored on Windows. |
| 6838 | [clinic start generated code]*/ |
| 6839 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6840 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6841 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 6842 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6843 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6844 | pid_t res; |
| 6845 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6846 | WAIT_TYPE status; |
| 6847 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6848 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6849 | do { |
| 6850 | Py_BEGIN_ALLOW_THREADS |
| 6851 | res = waitpid(pid, &status, options); |
| 6852 | Py_END_ALLOW_THREADS |
| 6853 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6854 | if (res < 0) |
| 6855 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6856 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6857 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6858 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6859 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6860 | /* 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] | 6861 | /*[clinic input] |
| 6862 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6863 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6864 | options: int |
| 6865 | / |
| 6866 | |
| 6867 | Wait for completion of a given process. |
| 6868 | |
| 6869 | Returns a tuple of information regarding the process: |
| 6870 | (pid, status << 8) |
| 6871 | |
| 6872 | The options argument is ignored on Windows. |
| 6873 | [clinic start generated code]*/ |
| 6874 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6875 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6876 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 6877 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6878 | { |
| 6879 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6880 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6881 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6882 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6883 | do { |
| 6884 | Py_BEGIN_ALLOW_THREADS |
| 6885 | res = _cwait(&status, pid, options); |
| 6886 | Py_END_ALLOW_THREADS |
| 6887 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 6888 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6889 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6890 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6891 | /* 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] | 6892 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6893 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6894 | #endif |
| 6895 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6896 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6897 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6898 | /*[clinic input] |
| 6899 | os.wait |
| 6900 | |
| 6901 | Wait for completion of a child process. |
| 6902 | |
| 6903 | Returns a tuple of information about the child process: |
| 6904 | (pid, status) |
| 6905 | [clinic start generated code]*/ |
| 6906 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6907 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6908 | os_wait_impl(PyObject *module) |
| 6909 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6910 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6911 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6912 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6913 | WAIT_TYPE status; |
| 6914 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6915 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6916 | do { |
| 6917 | Py_BEGIN_ALLOW_THREADS |
| 6918 | pid = wait(&status); |
| 6919 | Py_END_ALLOW_THREADS |
| 6920 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6921 | if (pid < 0) |
| 6922 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6923 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6924 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6925 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6926 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6927 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6928 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6929 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 6930 | PyDoc_STRVAR(readlink__doc__, |
| 6931 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 6932 | Return a string representing the path to which the symbolic link points.\n\ |
| 6933 | \n\ |
| 6934 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 6935 | and path should be relative; path will then be relative to that directory.\n\ |
| 6936 | dir_fd may not be implemented on your platform.\n\ |
| 6937 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6938 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6939 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6940 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6941 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6942 | /* AC 3.5: merge win32 and not together */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6943 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6944 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6945 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6946 | path_t path; |
| 6947 | int dir_fd = DEFAULT_DIR_FD; |
| 6948 | char buffer[MAXPATHLEN]; |
| 6949 | ssize_t length; |
| 6950 | PyObject *return_value = NULL; |
| 6951 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6952 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6953 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6954 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6955 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 6956 | path_converter, &path, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6957 | READLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6958 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6959 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6960 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6961 | #ifdef HAVE_READLINKAT |
| 6962 | if (dir_fd != DEFAULT_DIR_FD) |
| 6963 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6964 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6965 | #endif |
| 6966 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 6967 | Py_END_ALLOW_THREADS |
| 6968 | |
| 6969 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6970 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6971 | goto exit; |
| 6972 | } |
| 6973 | |
| 6974 | if (PyUnicode_Check(path.object)) |
| 6975 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 6976 | else |
| 6977 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 6978 | exit: |
| 6979 | path_cleanup(&path); |
| 6980 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6981 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6982 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6983 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6984 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6985 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6986 | |
| 6987 | static PyObject * |
| 6988 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6989 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6990 | const wchar_t *path; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6991 | DWORD n_bytes_returned; |
| 6992 | DWORD io_result; |
| 6993 | PyObject *po, *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6994 | int dir_fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6995 | HANDLE reparse_point_handle; |
| 6996 | |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 6997 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6998 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6999 | const wchar_t *print_name; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7000 | |
| 7001 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7002 | |
| 7003 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7004 | &po, |
| 7005 | dir_fd_unavailable, &dir_fd |
| 7006 | )) |
| 7007 | return NULL; |
| 7008 | |
| 7009 | path = PyUnicode_AsUnicode(po); |
| 7010 | if (path == NULL) |
| 7011 | return NULL; |
| 7012 | |
| 7013 | /* First get a handle to the reparse point */ |
| 7014 | Py_BEGIN_ALLOW_THREADS |
| 7015 | reparse_point_handle = CreateFileW( |
| 7016 | path, |
| 7017 | 0, |
| 7018 | 0, |
| 7019 | 0, |
| 7020 | OPEN_EXISTING, |
| 7021 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7022 | 0); |
| 7023 | Py_END_ALLOW_THREADS |
| 7024 | |
| 7025 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 7026 | return win32_error_object("readlink", po); |
| 7027 | |
| 7028 | Py_BEGIN_ALLOW_THREADS |
| 7029 | /* New call DeviceIoControl to read the reparse point */ |
| 7030 | io_result = DeviceIoControl( |
| 7031 | reparse_point_handle, |
| 7032 | FSCTL_GET_REPARSE_POINT, |
| 7033 | 0, 0, /* in buffer */ |
| 7034 | target_buffer, sizeof(target_buffer), |
| 7035 | &n_bytes_returned, |
| 7036 | 0 /* we're not using OVERLAPPED_IO */ |
| 7037 | ); |
| 7038 | CloseHandle(reparse_point_handle); |
| 7039 | Py_END_ALLOW_THREADS |
| 7040 | |
| 7041 | if (io_result==0) |
| 7042 | return win32_error_object("readlink", po); |
| 7043 | |
| 7044 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7045 | { |
| 7046 | PyErr_SetString(PyExc_ValueError, |
| 7047 | "not a symbolic link"); |
| 7048 | return NULL; |
| 7049 | } |
| 7050 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7051 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7052 | |
| 7053 | result = PyUnicode_FromWideChar(print_name, |
| 7054 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
| 7055 | return result; |
| 7056 | } |
| 7057 | |
| 7058 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7059 | |
| 7060 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7061 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7062 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7063 | |
| 7064 | #if defined(MS_WINDOWS) |
| 7065 | |
| 7066 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7067 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7068 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7069 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7070 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7071 | { |
| 7072 | HINSTANCE hKernel32; |
| 7073 | /* only recheck */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7074 | if (Py_CreateSymbolicLinkW) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7075 | return 1; |
| 7076 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7077 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7078 | "CreateSymbolicLinkW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7079 | return Py_CreateSymbolicLinkW != NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7080 | } |
| 7081 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7082 | /* Remove the last portion of the path */ |
| 7083 | static void |
| 7084 | _dirnameW(WCHAR *path) |
| 7085 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7086 | WCHAR *ptr; |
| 7087 | |
| 7088 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7089 | for(ptr = path + wcslen(path); ptr != path; ptr--) { |
Victor Stinner | 072318b | 2013-06-05 02:07:46 +0200 | [diff] [blame] | 7090 | if (*ptr == L'\\' || *ptr == L'/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7091 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7092 | } |
| 7093 | *ptr = 0; |
| 7094 | } |
| 7095 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7096 | /* Is this path absolute? */ |
| 7097 | static int |
| 7098 | _is_absW(const WCHAR *path) |
| 7099 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7100 | return path[0] == L'\\' || path[0] == L'/' || path[1] == L':'; |
| 7101 | |
| 7102 | } |
| 7103 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7104 | /* join root and rest with a backslash */ |
| 7105 | static void |
| 7106 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7107 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7108 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7109 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7110 | if (_is_absW(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7111 | wcscpy(dest_path, rest); |
| 7112 | return; |
| 7113 | } |
| 7114 | |
| 7115 | root_len = wcslen(root); |
| 7116 | |
| 7117 | wcscpy(dest_path, root); |
| 7118 | if(root_len) { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7119 | dest_path[root_len] = L'\\'; |
| 7120 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7121 | } |
| 7122 | wcscpy(dest_path+root_len, rest); |
| 7123 | } |
| 7124 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7125 | /* Return True if the path at src relative to dest is a directory */ |
| 7126 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7127 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7128 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7129 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7130 | WCHAR dest_parent[MAX_PATH]; |
| 7131 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7132 | |
| 7133 | /* dest_parent = os.path.dirname(dest) */ |
| 7134 | wcscpy(dest_parent, dest); |
| 7135 | _dirnameW(dest_parent); |
| 7136 | /* src_resolved = os.path.join(dest_parent, src) */ |
| 7137 | _joinW(src_resolved, dest_parent, src); |
| 7138 | return ( |
| 7139 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7140 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7141 | ); |
| 7142 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7143 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7144 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7145 | |
| 7146 | /*[clinic input] |
| 7147 | os.symlink |
| 7148 | src: path_t |
| 7149 | dst: path_t |
| 7150 | target_is_directory: bool = False |
| 7151 | * |
| 7152 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7153 | |
| 7154 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7155 | |
| 7156 | Create a symbolic link pointing to src named dst. |
| 7157 | |
| 7158 | target_is_directory is required on Windows if the target is to be |
| 7159 | interpreted as a directory. (On Windows, symlink requires |
| 7160 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7161 | target_is_directory is ignored on non-Windows platforms. |
| 7162 | |
| 7163 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7164 | and path should be relative; path will then be relative to that directory. |
| 7165 | dir_fd may not be implemented on your platform. |
| 7166 | If it is unavailable, using it will raise a NotImplementedError. |
| 7167 | |
| 7168 | [clinic start generated code]*/ |
| 7169 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7170 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7171 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7172 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7173 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7174 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7175 | #ifdef MS_WINDOWS |
| 7176 | DWORD result; |
| 7177 | #else |
| 7178 | int result; |
| 7179 | #endif |
| 7180 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7181 | #ifdef MS_WINDOWS |
| 7182 | if (!check_CreateSymbolicLink()) { |
| 7183 | PyErr_SetString(PyExc_NotImplementedError, |
| 7184 | "CreateSymbolicLink functions not found"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7185 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7186 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7187 | if (!win32_can_symlink) { |
| 7188 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7189 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7190 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7191 | #endif |
| 7192 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7193 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7194 | PyErr_SetString(PyExc_ValueError, |
| 7195 | "symlink: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7196 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7197 | } |
| 7198 | |
| 7199 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7200 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7201 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7202 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7203 | target_is_directory |= _check_dirW(src->wide, dst->wide); |
| 7204 | result = Py_CreateSymbolicLinkW(dst->wide, src->wide, |
| 7205 | target_is_directory); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7206 | Py_END_ALLOW_THREADS |
| 7207 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7208 | if (!result) |
| 7209 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7210 | |
| 7211 | #else |
| 7212 | |
| 7213 | Py_BEGIN_ALLOW_THREADS |
| 7214 | #if HAVE_SYMLINKAT |
| 7215 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7216 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7217 | else |
| 7218 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7219 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7220 | Py_END_ALLOW_THREADS |
| 7221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7222 | if (result) |
| 7223 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7224 | #endif |
| 7225 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7226 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7227 | } |
| 7228 | #endif /* HAVE_SYMLINK */ |
| 7229 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7230 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7231 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7232 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7233 | static PyStructSequence_Field times_result_fields[] = { |
| 7234 | {"user", "user time"}, |
| 7235 | {"system", "system time"}, |
| 7236 | {"children_user", "user time of children"}, |
| 7237 | {"children_system", "system time of children"}, |
| 7238 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7239 | {NULL} |
| 7240 | }; |
| 7241 | |
| 7242 | PyDoc_STRVAR(times_result__doc__, |
| 7243 | "times_result: Result from os.times().\n\n\ |
| 7244 | This object may be accessed either as a tuple of\n\ |
| 7245 | (user, system, children_user, children_system, elapsed),\n\ |
| 7246 | or via the attributes user, system, children_user, children_system,\n\ |
| 7247 | and elapsed.\n\ |
| 7248 | \n\ |
| 7249 | See os.times for more information."); |
| 7250 | |
| 7251 | static PyStructSequence_Desc times_result_desc = { |
| 7252 | "times_result", /* name */ |
| 7253 | times_result__doc__, /* doc */ |
| 7254 | times_result_fields, |
| 7255 | 5 |
| 7256 | }; |
| 7257 | |
| 7258 | static PyTypeObject TimesResultType; |
| 7259 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7260 | #ifdef MS_WINDOWS |
| 7261 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7262 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7263 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7264 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7265 | |
| 7266 | static PyObject * |
| 7267 | build_times_result(double user, double system, |
| 7268 | double children_user, double children_system, |
| 7269 | double elapsed) |
| 7270 | { |
| 7271 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7272 | if (value == NULL) |
| 7273 | return NULL; |
| 7274 | |
| 7275 | #define SET(i, field) \ |
| 7276 | { \ |
| 7277 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7278 | if (!o) { \ |
| 7279 | Py_DECREF(value); \ |
| 7280 | return NULL; \ |
| 7281 | } \ |
| 7282 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7283 | } \ |
| 7284 | |
| 7285 | SET(0, user); |
| 7286 | SET(1, system); |
| 7287 | SET(2, children_user); |
| 7288 | SET(3, children_system); |
| 7289 | SET(4, elapsed); |
| 7290 | |
| 7291 | #undef SET |
| 7292 | |
| 7293 | return value; |
| 7294 | } |
| 7295 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7296 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7297 | #ifndef MS_WINDOWS |
| 7298 | #define NEED_TICKS_PER_SECOND |
| 7299 | static long ticks_per_second = -1; |
| 7300 | #endif /* MS_WINDOWS */ |
| 7301 | |
| 7302 | /*[clinic input] |
| 7303 | os.times |
| 7304 | |
| 7305 | Return a collection containing process timing information. |
| 7306 | |
| 7307 | The object returned behaves like a named tuple with these fields: |
| 7308 | (utime, stime, cutime, cstime, elapsed_time) |
| 7309 | All fields are floating point numbers. |
| 7310 | [clinic start generated code]*/ |
| 7311 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7312 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7313 | os_times_impl(PyObject *module) |
| 7314 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7315 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7316 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7317 | FILETIME create, exit, kernel, user; |
| 7318 | HANDLE hProc; |
| 7319 | hProc = GetCurrentProcess(); |
| 7320 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7321 | /* The fields of a FILETIME structure are the hi and lo part |
| 7322 | of a 64-bit value expressed in 100 nanosecond units. |
| 7323 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7324 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7325 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7326 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7327 | (double)(user.dwHighDateTime*429.4967296 + |
| 7328 | user.dwLowDateTime*1e-7), |
| 7329 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7330 | kernel.dwLowDateTime*1e-7), |
| 7331 | (double)0, |
| 7332 | (double)0, |
| 7333 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7334 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7335 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7336 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7337 | |
| 7338 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7339 | struct tms t; |
| 7340 | clock_t c; |
| 7341 | errno = 0; |
| 7342 | c = times(&t); |
| 7343 | if (c == (clock_t) -1) |
| 7344 | return posix_error(); |
| 7345 | return build_times_result( |
| 7346 | (double)t.tms_utime / ticks_per_second, |
| 7347 | (double)t.tms_stime / ticks_per_second, |
| 7348 | (double)t.tms_cutime / ticks_per_second, |
| 7349 | (double)t.tms_cstime / ticks_per_second, |
| 7350 | (double)c / ticks_per_second); |
| 7351 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7352 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7353 | #endif /* HAVE_TIMES */ |
| 7354 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7355 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7356 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7357 | /*[clinic input] |
| 7358 | os.getsid |
| 7359 | |
| 7360 | pid: pid_t |
| 7361 | / |
| 7362 | |
| 7363 | Call the system call getsid(pid) and return the result. |
| 7364 | [clinic start generated code]*/ |
| 7365 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7366 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7367 | os_getsid_impl(PyObject *module, pid_t pid) |
| 7368 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7369 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7370 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7371 | sid = getsid(pid); |
| 7372 | if (sid < 0) |
| 7373 | return posix_error(); |
| 7374 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7375 | } |
| 7376 | #endif /* HAVE_GETSID */ |
| 7377 | |
| 7378 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7379 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7380 | /*[clinic input] |
| 7381 | os.setsid |
| 7382 | |
| 7383 | Call the system call setsid(). |
| 7384 | [clinic start generated code]*/ |
| 7385 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7386 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7387 | os_setsid_impl(PyObject *module) |
| 7388 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7389 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7390 | if (setsid() < 0) |
| 7391 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7392 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7393 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7394 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7395 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7396 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7397 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7398 | /*[clinic input] |
| 7399 | os.setpgid |
| 7400 | |
| 7401 | pid: pid_t |
| 7402 | pgrp: pid_t |
| 7403 | / |
| 7404 | |
| 7405 | Call the system call setpgid(pid, pgrp). |
| 7406 | [clinic start generated code]*/ |
| 7407 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7408 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7409 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 7410 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7411 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7412 | if (setpgid(pid, pgrp) < 0) |
| 7413 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7414 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7415 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7416 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7417 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7418 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7419 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7420 | /*[clinic input] |
| 7421 | os.tcgetpgrp |
| 7422 | |
| 7423 | fd: int |
| 7424 | / |
| 7425 | |
| 7426 | Return the process group associated with the terminal specified by fd. |
| 7427 | [clinic start generated code]*/ |
| 7428 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7429 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7430 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 7431 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7432 | { |
| 7433 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7434 | if (pgid < 0) |
| 7435 | return posix_error(); |
| 7436 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7437 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7438 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7439 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7440 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7441 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7442 | /*[clinic input] |
| 7443 | os.tcsetpgrp |
| 7444 | |
| 7445 | fd: int |
| 7446 | pgid: pid_t |
| 7447 | / |
| 7448 | |
| 7449 | Set the process group associated with the terminal specified by fd. |
| 7450 | [clinic start generated code]*/ |
| 7451 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7452 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7453 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 7454 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7455 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7456 | if (tcsetpgrp(fd, pgid) < 0) |
| 7457 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7458 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7459 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7460 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7461 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7462 | /* Functions acting on file descriptors */ |
| 7463 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7464 | #ifdef O_CLOEXEC |
| 7465 | extern int _Py_open_cloexec_works; |
| 7466 | #endif |
| 7467 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7468 | |
| 7469 | /*[clinic input] |
| 7470 | os.open -> int |
| 7471 | path: path_t |
| 7472 | flags: int |
| 7473 | mode: int = 0o777 |
| 7474 | * |
| 7475 | dir_fd: dir_fd(requires='openat') = None |
| 7476 | |
| 7477 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7478 | |
| 7479 | Open a file for low level IO. Returns a file descriptor (integer). |
| 7480 | |
| 7481 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7482 | and path should be relative; path will then be relative to that directory. |
| 7483 | dir_fd may not be implemented on your platform. |
| 7484 | If it is unavailable, using it will raise a NotImplementedError. |
| 7485 | [clinic start generated code]*/ |
| 7486 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7487 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7488 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 7489 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7490 | { |
| 7491 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7492 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7493 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7494 | #ifdef O_CLOEXEC |
| 7495 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 7496 | #elif !defined(MS_WINDOWS) |
| 7497 | int *atomic_flag_works = NULL; |
| 7498 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7499 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7500 | #ifdef MS_WINDOWS |
| 7501 | flags |= O_NOINHERIT; |
| 7502 | #elif defined(O_CLOEXEC) |
| 7503 | flags |= O_CLOEXEC; |
| 7504 | #endif |
| 7505 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7506 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7507 | do { |
| 7508 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7509 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7510 | fd = _wopen(path->wide, flags, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7511 | #endif |
| 7512 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7513 | if (dir_fd != DEFAULT_DIR_FD) |
| 7514 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 7515 | else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7516 | fd = open(path->narrow, flags, mode); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7517 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7518 | Py_END_ALLOW_THREADS |
| 7519 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7520 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7521 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7522 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7523 | if (!async_err) |
| 7524 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7525 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7526 | } |
| 7527 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7528 | #ifndef MS_WINDOWS |
| 7529 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 7530 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7531 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7532 | } |
| 7533 | #endif |
| 7534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7535 | return fd; |
| 7536 | } |
| 7537 | |
| 7538 | |
| 7539 | /*[clinic input] |
| 7540 | os.close |
| 7541 | |
| 7542 | fd: int |
| 7543 | |
| 7544 | Close a file descriptor. |
| 7545 | [clinic start generated code]*/ |
| 7546 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7547 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7548 | os_close_impl(PyObject *module, int fd) |
| 7549 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7550 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7551 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7552 | if (!_PyVerify_fd(fd)) |
| 7553 | return posix_error(); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7554 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 7555 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 7556 | * for more details. |
| 7557 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7558 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7559 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7560 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7561 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7562 | Py_END_ALLOW_THREADS |
| 7563 | if (res < 0) |
| 7564 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7565 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7566 | } |
| 7567 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7568 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7569 | /*[clinic input] |
| 7570 | os.closerange |
| 7571 | |
| 7572 | fd_low: int |
| 7573 | fd_high: int |
| 7574 | / |
| 7575 | |
| 7576 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 7577 | [clinic start generated code]*/ |
| 7578 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7579 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7580 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 7581 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7582 | { |
| 7583 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7584 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7585 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7586 | for (i = fd_low; i < fd_high; i++) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7587 | if (_PyVerify_fd(i)) |
| 7588 | close(i); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7589 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7590 | Py_END_ALLOW_THREADS |
| 7591 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7592 | } |
| 7593 | |
| 7594 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7595 | /*[clinic input] |
| 7596 | os.dup -> int |
| 7597 | |
| 7598 | fd: int |
| 7599 | / |
| 7600 | |
| 7601 | Return a duplicate of a file descriptor. |
| 7602 | [clinic start generated code]*/ |
| 7603 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7604 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7605 | os_dup_impl(PyObject *module, int fd) |
| 7606 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7607 | { |
| 7608 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7609 | } |
| 7610 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7611 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7612 | /*[clinic input] |
| 7613 | os.dup2 |
| 7614 | fd: int |
| 7615 | fd2: int |
| 7616 | inheritable: bool=True |
| 7617 | |
| 7618 | Duplicate file descriptor. |
| 7619 | [clinic start generated code]*/ |
| 7620 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7621 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7622 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
| 7623 | /*[clinic end generated code: output=db832a2d872ccc5f input=76e96f511be0352f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7624 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7625 | int res; |
| 7626 | #if defined(HAVE_DUP3) && \ |
| 7627 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 7628 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
| 7629 | int dup3_works = -1; |
| 7630 | #endif |
| 7631 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7632 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7633 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7634 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7635 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 7636 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 7637 | * upon close(), and therefore below. |
| 7638 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7639 | #ifdef MS_WINDOWS |
| 7640 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7641 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7642 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7643 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7644 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7645 | if (res < 0) |
| 7646 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7647 | |
| 7648 | /* Character files like console cannot be make non-inheritable */ |
| 7649 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7650 | close(fd2); |
| 7651 | return NULL; |
| 7652 | } |
| 7653 | |
| 7654 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 7655 | Py_BEGIN_ALLOW_THREADS |
| 7656 | if (!inheritable) |
| 7657 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 7658 | else |
| 7659 | res = dup2(fd, fd2); |
| 7660 | Py_END_ALLOW_THREADS |
| 7661 | if (res < 0) |
| 7662 | return posix_error(); |
| 7663 | |
| 7664 | #else |
| 7665 | |
| 7666 | #ifdef HAVE_DUP3 |
| 7667 | if (!inheritable && dup3_works != 0) { |
| 7668 | Py_BEGIN_ALLOW_THREADS |
| 7669 | res = dup3(fd, fd2, O_CLOEXEC); |
| 7670 | Py_END_ALLOW_THREADS |
| 7671 | if (res < 0) { |
| 7672 | if (dup3_works == -1) |
| 7673 | dup3_works = (errno != ENOSYS); |
| 7674 | if (dup3_works) |
| 7675 | return posix_error(); |
| 7676 | } |
| 7677 | } |
| 7678 | |
| 7679 | if (inheritable || dup3_works == 0) |
| 7680 | { |
| 7681 | #endif |
| 7682 | Py_BEGIN_ALLOW_THREADS |
| 7683 | res = dup2(fd, fd2); |
| 7684 | Py_END_ALLOW_THREADS |
| 7685 | if (res < 0) |
| 7686 | return posix_error(); |
| 7687 | |
| 7688 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7689 | close(fd2); |
| 7690 | return NULL; |
| 7691 | } |
| 7692 | #ifdef HAVE_DUP3 |
| 7693 | } |
| 7694 | #endif |
| 7695 | |
| 7696 | #endif |
| 7697 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7698 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7699 | } |
| 7700 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7701 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7702 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7703 | /*[clinic input] |
| 7704 | os.lockf |
| 7705 | |
| 7706 | fd: int |
| 7707 | An open file descriptor. |
| 7708 | command: int |
| 7709 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 7710 | length: Py_off_t |
| 7711 | The number of bytes to lock, starting at the current position. |
| 7712 | / |
| 7713 | |
| 7714 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 7715 | |
| 7716 | [clinic start generated code]*/ |
| 7717 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7718 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7719 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 7720 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7721 | { |
| 7722 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7723 | |
| 7724 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7725 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7726 | Py_END_ALLOW_THREADS |
| 7727 | |
| 7728 | if (res < 0) |
| 7729 | return posix_error(); |
| 7730 | |
| 7731 | Py_RETURN_NONE; |
| 7732 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7733 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7734 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7735 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7736 | /*[clinic input] |
| 7737 | os.lseek -> Py_off_t |
| 7738 | |
| 7739 | fd: int |
| 7740 | position: Py_off_t |
| 7741 | how: int |
| 7742 | / |
| 7743 | |
| 7744 | Set the position of a file descriptor. Return the new position. |
| 7745 | |
| 7746 | Return the new cursor position in number of bytes |
| 7747 | relative to the beginning of the file. |
| 7748 | [clinic start generated code]*/ |
| 7749 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7750 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7751 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 7752 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7753 | { |
| 7754 | Py_off_t result; |
| 7755 | |
| 7756 | if (!_PyVerify_fd(fd)) { |
| 7757 | posix_error(); |
| 7758 | return -1; |
| 7759 | } |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7760 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7761 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7762 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7763 | case 0: how = SEEK_SET; break; |
| 7764 | case 1: how = SEEK_CUR; break; |
| 7765 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7766 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7767 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7768 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7769 | if (PyErr_Occurred()) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7770 | return -1; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7771 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7772 | if (!_PyVerify_fd(fd)) { |
| 7773 | posix_error(); |
| 7774 | return -1; |
| 7775 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7776 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7777 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7778 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7779 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7780 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7781 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7782 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7783 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7784 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7785 | if (result < 0) |
| 7786 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7787 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7788 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7789 | } |
| 7790 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7791 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7792 | /*[clinic input] |
| 7793 | os.read |
| 7794 | fd: int |
| 7795 | length: Py_ssize_t |
| 7796 | / |
| 7797 | |
| 7798 | Read from a file descriptor. Returns a bytes object. |
| 7799 | [clinic start generated code]*/ |
| 7800 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7801 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7802 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 7803 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7804 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7805 | Py_ssize_t n; |
| 7806 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7807 | |
| 7808 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7809 | errno = EINVAL; |
| 7810 | return posix_error(); |
| 7811 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7812 | |
| 7813 | #ifdef MS_WINDOWS |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 7814 | /* On Windows, the count parameter of read() is an int */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7815 | if (length > INT_MAX) |
| 7816 | length = INT_MAX; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7817 | #endif |
| 7818 | |
| 7819 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7820 | if (buffer == NULL) |
| 7821 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7822 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 7823 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 7824 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7825 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 7826 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7827 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7828 | |
| 7829 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7830 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7831 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7832 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7833 | } |
| 7834 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7835 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7836 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7837 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7838 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7839 | { |
| 7840 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7841 | Py_ssize_t blen, total = 0; |
| 7842 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7843 | *iov = PyMem_New(struct iovec, cnt); |
| 7844 | if (*iov == NULL) { |
| 7845 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 7846 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7847 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7848 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7849 | *buf = PyMem_New(Py_buffer, cnt); |
| 7850 | if (*buf == NULL) { |
| 7851 | PyMem_Del(*iov); |
| 7852 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 7853 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7854 | } |
| 7855 | |
| 7856 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7857 | PyObject *item = PySequence_GetItem(seq, i); |
| 7858 | if (item == NULL) |
| 7859 | goto fail; |
| 7860 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7861 | Py_DECREF(item); |
| 7862 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7863 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7864 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7865 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7866 | blen = (*buf)[i].len; |
| 7867 | (*iov)[i].iov_len = blen; |
| 7868 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7869 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7870 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7871 | |
| 7872 | fail: |
| 7873 | PyMem_Del(*iov); |
| 7874 | for (j = 0; j < i; j++) { |
| 7875 | PyBuffer_Release(&(*buf)[j]); |
| 7876 | } |
| 7877 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 7878 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7879 | } |
| 7880 | |
| 7881 | static void |
| 7882 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7883 | { |
| 7884 | int i; |
| 7885 | PyMem_Del(iov); |
| 7886 | for (i = 0; i < cnt; i++) { |
| 7887 | PyBuffer_Release(&buf[i]); |
| 7888 | } |
| 7889 | PyMem_Del(buf); |
| 7890 | } |
| 7891 | #endif |
| 7892 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7893 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7894 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7895 | /*[clinic input] |
| 7896 | os.readv -> Py_ssize_t |
| 7897 | |
| 7898 | fd: int |
| 7899 | buffers: object |
| 7900 | / |
| 7901 | |
| 7902 | Read from a file descriptor fd into an iterable of buffers. |
| 7903 | |
| 7904 | The buffers should be mutable buffers accepting bytes. |
| 7905 | readv will transfer data into each buffer until it is full |
| 7906 | and then move on to the next buffer in the sequence to hold |
| 7907 | the rest of the data. |
| 7908 | |
| 7909 | readv returns the total number of bytes read, |
| 7910 | which may be less than the total capacity of all the buffers. |
| 7911 | [clinic start generated code]*/ |
| 7912 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7913 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7914 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 7915 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7916 | { |
| 7917 | int cnt; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7918 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7919 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7920 | struct iovec *iov; |
| 7921 | Py_buffer *buf; |
| 7922 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7923 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7924 | PyErr_SetString(PyExc_TypeError, |
| 7925 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7926 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7927 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7928 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7929 | cnt = PySequence_Size(buffers); |
| 7930 | |
| 7931 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 7932 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7933 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7934 | do { |
| 7935 | Py_BEGIN_ALLOW_THREADS |
| 7936 | n = readv(fd, iov, cnt); |
| 7937 | Py_END_ALLOW_THREADS |
| 7938 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7939 | |
| 7940 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7941 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7942 | if (!async_err) |
| 7943 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7944 | return -1; |
| 7945 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 7946 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7947 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7948 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7949 | #endif /* HAVE_READV */ |
| 7950 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7951 | |
| 7952 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7953 | /*[clinic input] |
| 7954 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 7955 | os.pread |
| 7956 | |
| 7957 | fd: int |
| 7958 | length: int |
| 7959 | offset: Py_off_t |
| 7960 | / |
| 7961 | |
| 7962 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 7963 | |
| 7964 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 7965 | the beginning of the file. The file offset remains unchanged. |
| 7966 | [clinic start generated code]*/ |
| 7967 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7968 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7969 | os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset) |
| 7970 | /*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7971 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7972 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7973 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7974 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7975 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7976 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7977 | errno = EINVAL; |
| 7978 | return posix_error(); |
| 7979 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7980 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7981 | if (buffer == NULL) |
| 7982 | return NULL; |
| 7983 | if (!_PyVerify_fd(fd)) { |
| 7984 | Py_DECREF(buffer); |
| 7985 | return posix_error(); |
| 7986 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7987 | |
| 7988 | do { |
| 7989 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7990 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7991 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7992 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7993 | Py_END_ALLOW_THREADS |
| 7994 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7995 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7996 | if (n < 0) { |
| 7997 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7998 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7999 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8000 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8001 | _PyBytes_Resize(&buffer, n); |
| 8002 | return buffer; |
| 8003 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8004 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8005 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8006 | |
| 8007 | /*[clinic input] |
| 8008 | os.write -> Py_ssize_t |
| 8009 | |
| 8010 | fd: int |
| 8011 | data: Py_buffer |
| 8012 | / |
| 8013 | |
| 8014 | Write a bytes object to a file descriptor. |
| 8015 | [clinic start generated code]*/ |
| 8016 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8017 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8018 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 8019 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8020 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8021 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8022 | } |
| 8023 | |
| 8024 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8025 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8026 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8027 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8028 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8029 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8030 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8031 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8032 | static PyObject * |
| 8033 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8034 | { |
| 8035 | int in, out; |
| 8036 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8037 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8038 | off_t offset; |
| 8039 | |
| 8040 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8041 | #ifndef __APPLE__ |
| 8042 | Py_ssize_t len; |
| 8043 | #endif |
| 8044 | PyObject *headers = NULL, *trailers = NULL; |
| 8045 | Py_buffer *hbuf, *tbuf; |
| 8046 | off_t sbytes; |
| 8047 | struct sf_hdtr sf; |
| 8048 | int flags = 0; |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8049 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8050 | static char *keywords[] = {"out", "in", |
| 8051 | "offset", "count", |
| 8052 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8053 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8054 | sf.headers = NULL; |
| 8055 | sf.trailers = NULL; |
| 8056 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8057 | #ifdef __APPLE__ |
| 8058 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8059 | 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] | 8060 | #else |
| 8061 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8062 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8063 | #endif |
| 8064 | &headers, &trailers, &flags)) |
| 8065 | return NULL; |
| 8066 | if (headers != NULL) { |
| 8067 | if (!PySequence_Check(headers)) { |
| 8068 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8069 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8070 | return NULL; |
| 8071 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8072 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8073 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8074 | if (sf.hdr_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8075 | (i = iov_setup(&(sf.headers), &hbuf, |
| 8076 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8077 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8078 | #ifdef __APPLE__ |
| 8079 | sbytes += i; |
| 8080 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8081 | } |
| 8082 | } |
| 8083 | if (trailers != NULL) { |
| 8084 | if (!PySequence_Check(trailers)) { |
| 8085 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8086 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8087 | return NULL; |
| 8088 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8089 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8090 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8091 | if (sf.trl_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8092 | (i = iov_setup(&(sf.trailers), &tbuf, |
| 8093 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8094 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8095 | #ifdef __APPLE__ |
| 8096 | sbytes += i; |
| 8097 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8098 | } |
| 8099 | } |
| 8100 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8101 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8102 | do { |
| 8103 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8104 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8105 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8106 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8107 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8108 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8109 | Py_END_ALLOW_THREADS |
| 8110 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8111 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8112 | |
| 8113 | if (sf.headers != NULL) |
| 8114 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8115 | if (sf.trailers != NULL) |
| 8116 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8117 | |
| 8118 | if (ret < 0) { |
| 8119 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8120 | if (sbytes != 0) { |
| 8121 | // some data has been sent |
| 8122 | goto done; |
| 8123 | } |
| 8124 | else { |
| 8125 | // no data has been sent; upper application is supposed |
| 8126 | // to retry on EAGAIN or EBUSY |
| 8127 | return posix_error(); |
| 8128 | } |
| 8129 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8130 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8131 | } |
| 8132 | goto done; |
| 8133 | |
| 8134 | done: |
| 8135 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8136 | return Py_BuildValue("l", sbytes); |
| 8137 | #else |
| 8138 | return Py_BuildValue("L", sbytes); |
| 8139 | #endif |
| 8140 | |
| 8141 | #else |
| 8142 | Py_ssize_t count; |
| 8143 | PyObject *offobj; |
| 8144 | static char *keywords[] = {"out", "in", |
| 8145 | "offset", "count", NULL}; |
| 8146 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8147 | keywords, &out, &in, &offobj, &count)) |
| 8148 | return NULL; |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 8149 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8150 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8151 | do { |
| 8152 | Py_BEGIN_ALLOW_THREADS |
| 8153 | ret = sendfile(out, in, NULL, count); |
| 8154 | Py_END_ALLOW_THREADS |
| 8155 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8156 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8157 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8158 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8159 | } |
| 8160 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8161 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8162 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8163 | |
| 8164 | do { |
| 8165 | Py_BEGIN_ALLOW_THREADS |
| 8166 | ret = sendfile(out, in, &offset, count); |
| 8167 | Py_END_ALLOW_THREADS |
| 8168 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8169 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8170 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8171 | return Py_BuildValue("n", ret); |
| 8172 | #endif |
| 8173 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8174 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8175 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8176 | |
| 8177 | /*[clinic input] |
| 8178 | os.fstat |
| 8179 | |
| 8180 | fd : int |
| 8181 | |
| 8182 | Perform a stat system call on the given file descriptor. |
| 8183 | |
| 8184 | Like stat(), but for an open file descriptor. |
| 8185 | Equivalent to os.stat(fd). |
| 8186 | [clinic start generated code]*/ |
| 8187 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8188 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8189 | os_fstat_impl(PyObject *module, int fd) |
| 8190 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8191 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8192 | STRUCT_STAT st; |
| 8193 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8194 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8195 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8196 | do { |
| 8197 | Py_BEGIN_ALLOW_THREADS |
| 8198 | res = FSTAT(fd, &st); |
| 8199 | Py_END_ALLOW_THREADS |
| 8200 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8201 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8202 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8203 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8204 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8205 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8206 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8207 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8208 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8209 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8210 | } |
| 8211 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8212 | |
| 8213 | /*[clinic input] |
| 8214 | os.isatty -> bool |
| 8215 | fd: int |
| 8216 | / |
| 8217 | |
| 8218 | Return True if the fd is connected to a terminal. |
| 8219 | |
| 8220 | Return True if the file descriptor is an open file descriptor |
| 8221 | connected to the slave end of a terminal. |
| 8222 | [clinic start generated code]*/ |
| 8223 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8224 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8225 | os_isatty_impl(PyObject *module, int fd) |
| 8226 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8227 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8228 | int return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8229 | if (!_PyVerify_fd(fd)) |
| 8230 | return 0; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8231 | _Py_BEGIN_SUPPRESS_IPH |
| 8232 | return_value = isatty(fd); |
| 8233 | _Py_END_SUPPRESS_IPH |
| 8234 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8235 | } |
| 8236 | |
| 8237 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8238 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8239 | /*[clinic input] |
| 8240 | os.pipe |
| 8241 | |
| 8242 | Create a pipe. |
| 8243 | |
| 8244 | Returns a tuple of two file descriptors: |
| 8245 | (read_fd, write_fd) |
| 8246 | [clinic start generated code]*/ |
| 8247 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8248 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8249 | os_pipe_impl(PyObject *module) |
| 8250 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8251 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8252 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8253 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8254 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8255 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8256 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8257 | #else |
| 8258 | int res; |
| 8259 | #endif |
| 8260 | |
| 8261 | #ifdef MS_WINDOWS |
| 8262 | attr.nLength = sizeof(attr); |
| 8263 | attr.lpSecurityDescriptor = NULL; |
| 8264 | attr.bInheritHandle = FALSE; |
| 8265 | |
| 8266 | Py_BEGIN_ALLOW_THREADS |
| 8267 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8268 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 8269 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 8270 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8271 | if (fds[0] == -1 || fds[1] == -1) { |
| 8272 | CloseHandle(read); |
| 8273 | CloseHandle(write); |
| 8274 | ok = 0; |
| 8275 | } |
| 8276 | } |
| 8277 | Py_END_ALLOW_THREADS |
| 8278 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8279 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8280 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8281 | #else |
| 8282 | |
| 8283 | #ifdef HAVE_PIPE2 |
| 8284 | Py_BEGIN_ALLOW_THREADS |
| 8285 | res = pipe2(fds, O_CLOEXEC); |
| 8286 | Py_END_ALLOW_THREADS |
| 8287 | |
| 8288 | if (res != 0 && errno == ENOSYS) |
| 8289 | { |
| 8290 | #endif |
| 8291 | Py_BEGIN_ALLOW_THREADS |
| 8292 | res = pipe(fds); |
| 8293 | Py_END_ALLOW_THREADS |
| 8294 | |
| 8295 | if (res == 0) { |
| 8296 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 8297 | close(fds[0]); |
| 8298 | close(fds[1]); |
| 8299 | return NULL; |
| 8300 | } |
| 8301 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 8302 | close(fds[0]); |
| 8303 | close(fds[1]); |
| 8304 | return NULL; |
| 8305 | } |
| 8306 | } |
| 8307 | #ifdef HAVE_PIPE2 |
| 8308 | } |
| 8309 | #endif |
| 8310 | |
| 8311 | if (res != 0) |
| 8312 | return PyErr_SetFromErrno(PyExc_OSError); |
| 8313 | #endif /* !MS_WINDOWS */ |
| 8314 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8315 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8316 | #endif /* HAVE_PIPE */ |
| 8317 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8318 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8319 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8320 | /*[clinic input] |
| 8321 | os.pipe2 |
| 8322 | |
| 8323 | flags: int |
| 8324 | / |
| 8325 | |
| 8326 | Create a pipe with flags set atomically. |
| 8327 | |
| 8328 | Returns a tuple of two file descriptors: |
| 8329 | (read_fd, write_fd) |
| 8330 | |
| 8331 | flags can be constructed by ORing together one or more of these values: |
| 8332 | O_NONBLOCK, O_CLOEXEC. |
| 8333 | [clinic start generated code]*/ |
| 8334 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8335 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8336 | os_pipe2_impl(PyObject *module, int flags) |
| 8337 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8338 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8339 | int fds[2]; |
| 8340 | int res; |
| 8341 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8342 | res = pipe2(fds, flags); |
| 8343 | if (res != 0) |
| 8344 | return posix_error(); |
| 8345 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8346 | } |
| 8347 | #endif /* HAVE_PIPE2 */ |
| 8348 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8349 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8350 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8351 | /*[clinic input] |
| 8352 | os.writev -> Py_ssize_t |
| 8353 | fd: int |
| 8354 | buffers: object |
| 8355 | / |
| 8356 | |
| 8357 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 8358 | |
| 8359 | Returns the total number of bytes written. |
| 8360 | buffers must be a sequence of bytes-like objects. |
| 8361 | [clinic start generated code]*/ |
| 8362 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8363 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8364 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 8365 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8366 | { |
| 8367 | int cnt; |
| 8368 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8369 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8370 | struct iovec *iov; |
| 8371 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8372 | |
| 8373 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8374 | PyErr_SetString(PyExc_TypeError, |
| 8375 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8376 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8377 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8378 | cnt = PySequence_Size(buffers); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8379 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8380 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 8381 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8382 | } |
| 8383 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8384 | do { |
| 8385 | Py_BEGIN_ALLOW_THREADS |
| 8386 | result = writev(fd, iov, cnt); |
| 8387 | Py_END_ALLOW_THREADS |
| 8388 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8389 | |
| 8390 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8391 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8392 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8393 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8394 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8395 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8396 | #endif /* HAVE_WRITEV */ |
| 8397 | |
| 8398 | |
| 8399 | #ifdef HAVE_PWRITE |
| 8400 | /*[clinic input] |
| 8401 | os.pwrite -> Py_ssize_t |
| 8402 | |
| 8403 | fd: int |
| 8404 | buffer: Py_buffer |
| 8405 | offset: Py_off_t |
| 8406 | / |
| 8407 | |
| 8408 | Write bytes to a file descriptor starting at a particular offset. |
| 8409 | |
| 8410 | Write buffer to fd, starting at offset bytes from the beginning of |
| 8411 | the file. Returns the number of bytes writte. Does not change the |
| 8412 | current file offset. |
| 8413 | [clinic start generated code]*/ |
| 8414 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8415 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8416 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 8417 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8418 | { |
| 8419 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8420 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8421 | |
| 8422 | if (!_PyVerify_fd(fd)) { |
| 8423 | posix_error(); |
| 8424 | return -1; |
| 8425 | } |
| 8426 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8427 | do { |
| 8428 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8429 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8430 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8431 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8432 | Py_END_ALLOW_THREADS |
| 8433 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8434 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8435 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8436 | posix_error(); |
| 8437 | return size; |
| 8438 | } |
| 8439 | #endif /* HAVE_PWRITE */ |
| 8440 | |
| 8441 | |
| 8442 | #ifdef HAVE_MKFIFO |
| 8443 | /*[clinic input] |
| 8444 | os.mkfifo |
| 8445 | |
| 8446 | path: path_t |
| 8447 | mode: int=0o666 |
| 8448 | * |
| 8449 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 8450 | |
| 8451 | Create a "fifo" (a POSIX named pipe). |
| 8452 | |
| 8453 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8454 | and path should be relative; path will then be relative to that directory. |
| 8455 | dir_fd may not be implemented on your platform. |
| 8456 | If it is unavailable, using it will raise a NotImplementedError. |
| 8457 | [clinic start generated code]*/ |
| 8458 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8459 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8460 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 8461 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8462 | { |
| 8463 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8464 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8465 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8466 | do { |
| 8467 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8468 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8469 | if (dir_fd != DEFAULT_DIR_FD) |
| 8470 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 8471 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8472 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8473 | result = mkfifo(path->narrow, mode); |
| 8474 | Py_END_ALLOW_THREADS |
| 8475 | } while (result != 0 && errno == EINTR && |
| 8476 | !(async_err = PyErr_CheckSignals())); |
| 8477 | if (result != 0) |
| 8478 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8479 | |
| 8480 | Py_RETURN_NONE; |
| 8481 | } |
| 8482 | #endif /* HAVE_MKFIFO */ |
| 8483 | |
| 8484 | |
| 8485 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 8486 | /*[clinic input] |
| 8487 | os.mknod |
| 8488 | |
| 8489 | path: path_t |
| 8490 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8491 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8492 | * |
| 8493 | dir_fd: dir_fd(requires='mknodat')=None |
| 8494 | |
| 8495 | Create a node in the file system. |
| 8496 | |
| 8497 | Create a node in the file system (file, device special file or named pipe) |
| 8498 | at path. mode specifies both the permissions to use and the |
| 8499 | type of node to be created, being combined (bitwise OR) with one of |
| 8500 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 8501 | device defines the newly created device special file (probably using |
| 8502 | os.makedev()). Otherwise device is ignored. |
| 8503 | |
| 8504 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8505 | and path should be relative; path will then be relative to that directory. |
| 8506 | dir_fd may not be implemented on your platform. |
| 8507 | If it is unavailable, using it will raise a NotImplementedError. |
| 8508 | [clinic start generated code]*/ |
| 8509 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8510 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8511 | 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] | 8512 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8513 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8514 | { |
| 8515 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8516 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8517 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8518 | do { |
| 8519 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8520 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8521 | if (dir_fd != DEFAULT_DIR_FD) |
| 8522 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 8523 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8524 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8525 | result = mknod(path->narrow, mode, device); |
| 8526 | Py_END_ALLOW_THREADS |
| 8527 | } while (result != 0 && errno == EINTR && |
| 8528 | !(async_err = PyErr_CheckSignals())); |
| 8529 | if (result != 0) |
| 8530 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8531 | |
| 8532 | Py_RETURN_NONE; |
| 8533 | } |
| 8534 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 8535 | |
| 8536 | |
| 8537 | #ifdef HAVE_DEVICE_MACROS |
| 8538 | /*[clinic input] |
| 8539 | os.major -> unsigned_int |
| 8540 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8541 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8542 | / |
| 8543 | |
| 8544 | Extracts a device major number from a raw device number. |
| 8545 | [clinic start generated code]*/ |
| 8546 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8547 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8548 | os_major_impl(PyObject *module, dev_t device) |
| 8549 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8550 | { |
| 8551 | return major(device); |
| 8552 | } |
| 8553 | |
| 8554 | |
| 8555 | /*[clinic input] |
| 8556 | os.minor -> unsigned_int |
| 8557 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8558 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8559 | / |
| 8560 | |
| 8561 | Extracts a device minor number from a raw device number. |
| 8562 | [clinic start generated code]*/ |
| 8563 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8564 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8565 | os_minor_impl(PyObject *module, dev_t device) |
| 8566 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8567 | { |
| 8568 | return minor(device); |
| 8569 | } |
| 8570 | |
| 8571 | |
| 8572 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8573 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8574 | |
| 8575 | major: int |
| 8576 | minor: int |
| 8577 | / |
| 8578 | |
| 8579 | Composes a raw device number from the major and minor device numbers. |
| 8580 | [clinic start generated code]*/ |
| 8581 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8582 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8583 | os_makedev_impl(PyObject *module, int major, int minor) |
| 8584 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8585 | { |
| 8586 | return makedev(major, minor); |
| 8587 | } |
| 8588 | #endif /* HAVE_DEVICE_MACROS */ |
| 8589 | |
| 8590 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8591 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8592 | /*[clinic input] |
| 8593 | os.ftruncate |
| 8594 | |
| 8595 | fd: int |
| 8596 | length: Py_off_t |
| 8597 | / |
| 8598 | |
| 8599 | Truncate a file, specified by file descriptor, to a specific length. |
| 8600 | [clinic start generated code]*/ |
| 8601 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8602 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8603 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 8604 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8605 | { |
| 8606 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8607 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8608 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8609 | if (!_PyVerify_fd(fd)) |
| 8610 | return posix_error(); |
| 8611 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8612 | do { |
| 8613 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8614 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8615 | #ifdef MS_WINDOWS |
| 8616 | result = _chsize_s(fd, length); |
| 8617 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8618 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8619 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8620 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8621 | Py_END_ALLOW_THREADS |
| 8622 | } while (result != 0 && errno == EINTR && |
| 8623 | !(async_err = PyErr_CheckSignals())); |
| 8624 | if (result != 0) |
| 8625 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8626 | Py_RETURN_NONE; |
| 8627 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8628 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8629 | |
| 8630 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8631 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8632 | /*[clinic input] |
| 8633 | os.truncate |
| 8634 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 8635 | length: Py_off_t |
| 8636 | |
| 8637 | Truncate a file, specified by path, to a specific length. |
| 8638 | |
| 8639 | On some platforms, path may also be specified as an open file descriptor. |
| 8640 | If this functionality is unavailable, using it raises an exception. |
| 8641 | [clinic start generated code]*/ |
| 8642 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8643 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8644 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 8645 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8646 | { |
| 8647 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8648 | #ifdef MS_WINDOWS |
| 8649 | int fd; |
| 8650 | #endif |
| 8651 | |
| 8652 | if (path->fd != -1) |
| 8653 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8654 | |
| 8655 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8656 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8657 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8658 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 8659 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8660 | result = -1; |
| 8661 | else { |
| 8662 | result = _chsize_s(fd, length); |
| 8663 | close(fd); |
| 8664 | if (result < 0) |
| 8665 | errno = result; |
| 8666 | } |
| 8667 | #else |
| 8668 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8669 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8670 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8671 | Py_END_ALLOW_THREADS |
| 8672 | if (result < 0) |
| 8673 | return path_error(path); |
| 8674 | |
| 8675 | Py_RETURN_NONE; |
| 8676 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8677 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8678 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8679 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8680 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 8681 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 8682 | defined, which is the case in Python on AIX. AIX bug report: |
| 8683 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 8684 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 8685 | # define POSIX_FADVISE_AIX_BUG |
| 8686 | #endif |
| 8687 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8688 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8689 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8690 | /*[clinic input] |
| 8691 | os.posix_fallocate |
| 8692 | |
| 8693 | fd: int |
| 8694 | offset: Py_off_t |
| 8695 | length: Py_off_t |
| 8696 | / |
| 8697 | |
| 8698 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 8699 | |
| 8700 | Ensure that the file specified by fd encompasses a range of bytes |
| 8701 | starting at offset bytes from the beginning and continuing for length bytes. |
| 8702 | [clinic start generated code]*/ |
| 8703 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8704 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8705 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8706 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8707 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8708 | { |
| 8709 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8710 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8711 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8712 | do { |
| 8713 | Py_BEGIN_ALLOW_THREADS |
| 8714 | result = posix_fallocate(fd, offset, length); |
| 8715 | Py_END_ALLOW_THREADS |
| 8716 | } while (result != 0 && errno == EINTR && |
| 8717 | !(async_err = PyErr_CheckSignals())); |
| 8718 | if (result != 0) |
| 8719 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8720 | Py_RETURN_NONE; |
| 8721 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8722 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8723 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8724 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8725 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8726 | /*[clinic input] |
| 8727 | os.posix_fadvise |
| 8728 | |
| 8729 | fd: int |
| 8730 | offset: Py_off_t |
| 8731 | length: Py_off_t |
| 8732 | advice: int |
| 8733 | / |
| 8734 | |
| 8735 | Announce an intention to access data in a specific pattern. |
| 8736 | |
| 8737 | Announce an intention to access data in a specific pattern, thus allowing |
| 8738 | the kernel to make optimizations. |
| 8739 | The advice applies to the region of the file specified by fd starting at |
| 8740 | offset and continuing for length bytes. |
| 8741 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 8742 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 8743 | POSIX_FADV_DONTNEED. |
| 8744 | [clinic start generated code]*/ |
| 8745 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8746 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8747 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8748 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8749 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8750 | { |
| 8751 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8752 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8753 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8754 | do { |
| 8755 | Py_BEGIN_ALLOW_THREADS |
| 8756 | result = posix_fadvise(fd, offset, length, advice); |
| 8757 | Py_END_ALLOW_THREADS |
| 8758 | } while (result != 0 && errno == EINTR && |
| 8759 | !(async_err = PyErr_CheckSignals())); |
| 8760 | if (result != 0) |
| 8761 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8762 | Py_RETURN_NONE; |
| 8763 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8764 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8765 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8766 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8767 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8768 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8769 | * get re-set with another call for the same key. */ |
| 8770 | static PyObject *posix_putenv_garbage; |
| 8771 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8772 | static void |
| 8773 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8774 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8775 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8776 | * this will cause previous value to be collected. This has to |
| 8777 | * happen after the real putenv() call because the old value |
| 8778 | * was still accessible until then. */ |
| 8779 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 8780 | /* really not much we can do; just leak */ |
| 8781 | PyErr_Clear(); |
| 8782 | else |
| 8783 | Py_DECREF(value); |
| 8784 | } |
| 8785 | |
| 8786 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8787 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8788 | /*[clinic input] |
| 8789 | os.putenv |
| 8790 | |
| 8791 | name: unicode |
| 8792 | value: unicode |
| 8793 | / |
| 8794 | |
| 8795 | Change or add an environment variable. |
| 8796 | [clinic start generated code]*/ |
| 8797 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8798 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8799 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 8800 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8801 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8802 | const wchar_t *env; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8803 | |
| 8804 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 8805 | if (unicode == NULL) { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8806 | PyErr_NoMemory(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8807 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8808 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8809 | if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8810 | PyErr_Format(PyExc_ValueError, |
| 8811 | "the environment variable is longer than %u characters", |
| 8812 | _MAX_ENV); |
| 8813 | goto error; |
| 8814 | } |
| 8815 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8816 | env = PyUnicode_AsUnicode(unicode); |
| 8817 | if (env == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8818 | goto error; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8819 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8820 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8821 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8822 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8823 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8824 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8825 | Py_RETURN_NONE; |
| 8826 | |
| 8827 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8828 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8829 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8830 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8831 | #else /* MS_WINDOWS */ |
| 8832 | /*[clinic input] |
| 8833 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8834 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8835 | name: FSConverter |
| 8836 | value: FSConverter |
| 8837 | / |
| 8838 | |
| 8839 | Change or add an environment variable. |
| 8840 | [clinic start generated code]*/ |
| 8841 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8842 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8843 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 8844 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8845 | { |
| 8846 | PyObject *bytes = NULL; |
| 8847 | char *env; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8848 | const char *name_string = PyBytes_AsString(name); |
| 8849 | const char *value_string = PyBytes_AsString(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8850 | |
| 8851 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 8852 | if (bytes == NULL) { |
| 8853 | PyErr_NoMemory(); |
| 8854 | return NULL; |
| 8855 | } |
| 8856 | |
| 8857 | env = PyBytes_AS_STRING(bytes); |
| 8858 | if (putenv(env)) { |
| 8859 | Py_DECREF(bytes); |
| 8860 | return posix_error(); |
| 8861 | } |
| 8862 | |
| 8863 | posix_putenv_garbage_setitem(name, bytes); |
| 8864 | Py_RETURN_NONE; |
| 8865 | } |
| 8866 | #endif /* MS_WINDOWS */ |
| 8867 | #endif /* HAVE_PUTENV */ |
| 8868 | |
| 8869 | |
| 8870 | #ifdef HAVE_UNSETENV |
| 8871 | /*[clinic input] |
| 8872 | os.unsetenv |
| 8873 | name: FSConverter |
| 8874 | / |
| 8875 | |
| 8876 | Delete an environment variable. |
| 8877 | [clinic start generated code]*/ |
| 8878 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8879 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8880 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 8881 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8882 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8883 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8884 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8885 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8886 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8887 | #ifdef HAVE_BROKEN_UNSETENV |
| 8888 | unsetenv(PyBytes_AS_STRING(name)); |
| 8889 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8890 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8891 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8892 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8893 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8894 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8895 | /* Remove the key from posix_putenv_garbage; |
| 8896 | * this will cause it to be collected. This has to |
| 8897 | * happen after the real unsetenv() call because the |
| 8898 | * old value was still accessible until then. |
| 8899 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8900 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8901 | /* really not much we can do; just leak */ |
| 8902 | PyErr_Clear(); |
| 8903 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8904 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8905 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8906 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8907 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8908 | |
| 8909 | /*[clinic input] |
| 8910 | os.strerror |
| 8911 | |
| 8912 | code: int |
| 8913 | / |
| 8914 | |
| 8915 | Translate an error code to a message string. |
| 8916 | [clinic start generated code]*/ |
| 8917 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8918 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8919 | os_strerror_impl(PyObject *module, int code) |
| 8920 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8921 | { |
| 8922 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8923 | if (message == NULL) { |
| 8924 | PyErr_SetString(PyExc_ValueError, |
| 8925 | "strerror() argument out of range"); |
| 8926 | return NULL; |
| 8927 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8928 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8929 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8930 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8931 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8932 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8933 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8934 | /*[clinic input] |
| 8935 | os.WCOREDUMP -> bool |
| 8936 | |
| 8937 | status: int |
| 8938 | / |
| 8939 | |
| 8940 | Return True if the process returning status was dumped to a core file. |
| 8941 | [clinic start generated code]*/ |
| 8942 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8943 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8944 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 8945 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8946 | { |
| 8947 | WAIT_TYPE wait_status; |
| 8948 | WAIT_STATUS_INT(wait_status) = status; |
| 8949 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8950 | } |
| 8951 | #endif /* WCOREDUMP */ |
| 8952 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8953 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8954 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8955 | /*[clinic input] |
| 8956 | os.WIFCONTINUED -> bool |
| 8957 | |
| 8958 | status: int |
| 8959 | |
| 8960 | Return True if a particular process was continued from a job control stop. |
| 8961 | |
| 8962 | Return True if the process returning status was continued from a |
| 8963 | job control stop. |
| 8964 | [clinic start generated code]*/ |
| 8965 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8966 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8967 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 8968 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8969 | { |
| 8970 | WAIT_TYPE wait_status; |
| 8971 | WAIT_STATUS_INT(wait_status) = status; |
| 8972 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8973 | } |
| 8974 | #endif /* WIFCONTINUED */ |
| 8975 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8976 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8977 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8978 | /*[clinic input] |
| 8979 | os.WIFSTOPPED -> bool |
| 8980 | |
| 8981 | status: int |
| 8982 | |
| 8983 | Return True if the process returning status was stopped. |
| 8984 | [clinic start generated code]*/ |
| 8985 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8986 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8987 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 8988 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8989 | { |
| 8990 | WAIT_TYPE wait_status; |
| 8991 | WAIT_STATUS_INT(wait_status) = status; |
| 8992 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8993 | } |
| 8994 | #endif /* WIFSTOPPED */ |
| 8995 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8996 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8997 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8998 | /*[clinic input] |
| 8999 | os.WIFSIGNALED -> bool |
| 9000 | |
| 9001 | status: int |
| 9002 | |
| 9003 | Return True if the process returning status was terminated by a signal. |
| 9004 | [clinic start generated code]*/ |
| 9005 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9006 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9007 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 9008 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9009 | { |
| 9010 | WAIT_TYPE wait_status; |
| 9011 | WAIT_STATUS_INT(wait_status) = status; |
| 9012 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9013 | } |
| 9014 | #endif /* WIFSIGNALED */ |
| 9015 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9016 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9017 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9018 | /*[clinic input] |
| 9019 | os.WIFEXITED -> bool |
| 9020 | |
| 9021 | status: int |
| 9022 | |
| 9023 | Return True if the process returning status exited via the exit() system call. |
| 9024 | [clinic start generated code]*/ |
| 9025 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9026 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9027 | os_WIFEXITED_impl(PyObject *module, int status) |
| 9028 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9029 | { |
| 9030 | WAIT_TYPE wait_status; |
| 9031 | WAIT_STATUS_INT(wait_status) = status; |
| 9032 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9033 | } |
| 9034 | #endif /* WIFEXITED */ |
| 9035 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9036 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9037 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9038 | /*[clinic input] |
| 9039 | os.WEXITSTATUS -> int |
| 9040 | |
| 9041 | status: int |
| 9042 | |
| 9043 | Return the process return code from status. |
| 9044 | [clinic start generated code]*/ |
| 9045 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9046 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9047 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 9048 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9049 | { |
| 9050 | WAIT_TYPE wait_status; |
| 9051 | WAIT_STATUS_INT(wait_status) = status; |
| 9052 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9053 | } |
| 9054 | #endif /* WEXITSTATUS */ |
| 9055 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9056 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9057 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9058 | /*[clinic input] |
| 9059 | os.WTERMSIG -> int |
| 9060 | |
| 9061 | status: int |
| 9062 | |
| 9063 | Return the signal that terminated the process that provided the status value. |
| 9064 | [clinic start generated code]*/ |
| 9065 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9066 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9067 | os_WTERMSIG_impl(PyObject *module, int status) |
| 9068 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9069 | { |
| 9070 | WAIT_TYPE wait_status; |
| 9071 | WAIT_STATUS_INT(wait_status) = status; |
| 9072 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9073 | } |
| 9074 | #endif /* WTERMSIG */ |
| 9075 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9076 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9077 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9078 | /*[clinic input] |
| 9079 | os.WSTOPSIG -> int |
| 9080 | |
| 9081 | status: int |
| 9082 | |
| 9083 | Return the signal that stopped the process that provided the status value. |
| 9084 | [clinic start generated code]*/ |
| 9085 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9086 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9087 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 9088 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9089 | { |
| 9090 | WAIT_TYPE wait_status; |
| 9091 | WAIT_STATUS_INT(wait_status) = status; |
| 9092 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9093 | } |
| 9094 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9095 | #endif /* HAVE_SYS_WAIT_H */ |
| 9096 | |
| 9097 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9098 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9099 | #ifdef _SCO_DS |
| 9100 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9101 | needed definitions in sys/statvfs.h */ |
| 9102 | #define _SVID3 |
| 9103 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9104 | #include <sys/statvfs.h> |
| 9105 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9106 | static PyObject* |
| 9107 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9108 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 9109 | if (v == NULL) |
| 9110 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9111 | |
| 9112 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9113 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9114 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9115 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9116 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9117 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9118 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9119 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9120 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9121 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9122 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9123 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9124 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9125 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9126 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9127 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9128 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9129 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9130 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9131 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9132 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9133 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9134 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9135 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9136 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9137 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9138 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9139 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9140 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9141 | if (PyErr_Occurred()) { |
| 9142 | Py_DECREF(v); |
| 9143 | return NULL; |
| 9144 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9145 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9146 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9147 | } |
| 9148 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9149 | |
| 9150 | /*[clinic input] |
| 9151 | os.fstatvfs |
| 9152 | fd: int |
| 9153 | / |
| 9154 | |
| 9155 | Perform an fstatvfs system call on the given fd. |
| 9156 | |
| 9157 | Equivalent to statvfs(fd). |
| 9158 | [clinic start generated code]*/ |
| 9159 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9160 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9161 | os_fstatvfs_impl(PyObject *module, int fd) |
| 9162 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9163 | { |
| 9164 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9165 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9166 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9167 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9168 | do { |
| 9169 | Py_BEGIN_ALLOW_THREADS |
| 9170 | result = fstatvfs(fd, &st); |
| 9171 | Py_END_ALLOW_THREADS |
| 9172 | } while (result != 0 && errno == EINTR && |
| 9173 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9174 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9175 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9176 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9177 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9178 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9179 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9180 | |
| 9181 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9182 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9183 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9184 | /*[clinic input] |
| 9185 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9186 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9187 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 9188 | |
| 9189 | Perform a statvfs system call on the given path. |
| 9190 | |
| 9191 | path may always be specified as a string. |
| 9192 | On some platforms, path may also be specified as an open file descriptor. |
| 9193 | If this functionality is unavailable, using it raises an exception. |
| 9194 | [clinic start generated code]*/ |
| 9195 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9196 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9197 | os_statvfs_impl(PyObject *module, path_t *path) |
| 9198 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9199 | { |
| 9200 | int result; |
| 9201 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9202 | |
| 9203 | Py_BEGIN_ALLOW_THREADS |
| 9204 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9205 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9206 | #ifdef __APPLE__ |
| 9207 | /* handle weak-linking on Mac OS X 10.3 */ |
| 9208 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9209 | fd_specified("statvfs", path->fd); |
| 9210 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9211 | } |
| 9212 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9213 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9214 | } |
| 9215 | else |
| 9216 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9217 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9218 | Py_END_ALLOW_THREADS |
| 9219 | |
| 9220 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9221 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9222 | } |
| 9223 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9224 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9225 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9226 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 9227 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9228 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9229 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9230 | /*[clinic input] |
| 9231 | os._getdiskusage |
| 9232 | |
| 9233 | path: Py_UNICODE |
| 9234 | |
| 9235 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 9236 | [clinic start generated code]*/ |
| 9237 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9238 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9239 | os__getdiskusage_impl(PyObject *module, Py_UNICODE *path) |
| 9240 | /*[clinic end generated code: output=76d6adcd86b1db0b input=6458133aed893c78]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9241 | { |
| 9242 | BOOL retval; |
| 9243 | ULARGE_INTEGER _, total, free; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9244 | |
| 9245 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9246 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9247 | Py_END_ALLOW_THREADS |
| 9248 | if (retval == 0) |
| 9249 | return PyErr_SetFromWindowsErr(0); |
| 9250 | |
| 9251 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9252 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9253 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9254 | |
| 9255 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9256 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9257 | * It maps strings representing configuration variable names to |
| 9258 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9259 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9260 | * rarely-used constants. There are three separate tables that use |
| 9261 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9262 | * |
| 9263 | * This code is always included, even if none of the interfaces that |
| 9264 | * need it are included. The #if hackery needed to avoid it would be |
| 9265 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9266 | */ |
| 9267 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 9268 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 9269 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9270 | }; |
| 9271 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9272 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9273 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9274 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9275 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9276 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 9277 | int value = _PyLong_AsInt(arg); |
| 9278 | if (value == -1 && PyErr_Occurred()) |
| 9279 | return 0; |
| 9280 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9281 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9282 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9283 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9284 | /* look up the value in the table using a binary search */ |
| 9285 | size_t lo = 0; |
| 9286 | size_t mid; |
| 9287 | size_t hi = tablesize; |
| 9288 | int cmp; |
| 9289 | const char *confname; |
| 9290 | if (!PyUnicode_Check(arg)) { |
| 9291 | PyErr_SetString(PyExc_TypeError, |
| 9292 | "configuration names must be strings or integers"); |
| 9293 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9294 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9295 | confname = _PyUnicode_AsString(arg); |
| 9296 | if (confname == NULL) |
| 9297 | return 0; |
| 9298 | while (lo < hi) { |
| 9299 | mid = (lo + hi) / 2; |
| 9300 | cmp = strcmp(confname, table[mid].name); |
| 9301 | if (cmp < 0) |
| 9302 | hi = mid; |
| 9303 | else if (cmp > 0) |
| 9304 | lo = mid + 1; |
| 9305 | else { |
| 9306 | *valuep = table[mid].value; |
| 9307 | return 1; |
| 9308 | } |
| 9309 | } |
| 9310 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9311 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9312 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9313 | } |
| 9314 | |
| 9315 | |
| 9316 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9317 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9318 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9319 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9320 | #endif |
| 9321 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9322 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9323 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9324 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9325 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9326 | #endif |
| 9327 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9328 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9329 | #endif |
| 9330 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9331 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9332 | #endif |
| 9333 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9334 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9335 | #endif |
| 9336 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9337 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9338 | #endif |
| 9339 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9340 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9341 | #endif |
| 9342 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9343 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9344 | #endif |
| 9345 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9346 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9347 | #endif |
| 9348 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9349 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9350 | #endif |
| 9351 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9352 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9353 | #endif |
| 9354 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9355 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9356 | #endif |
| 9357 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9358 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9359 | #endif |
| 9360 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9361 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9362 | #endif |
| 9363 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9364 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9365 | #endif |
| 9366 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9367 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9368 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9369 | #ifdef _PC_ACL_ENABLED |
| 9370 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9371 | #endif |
| 9372 | #ifdef _PC_MIN_HOLE_SIZE |
| 9373 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9374 | #endif |
| 9375 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9376 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9377 | #endif |
| 9378 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9379 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9380 | #endif |
| 9381 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9382 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9383 | #endif |
| 9384 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9385 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9386 | #endif |
| 9387 | #ifdef _PC_REC_XFER_ALIGN |
| 9388 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9389 | #endif |
| 9390 | #ifdef _PC_SYMLINK_MAX |
| 9391 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9392 | #endif |
| 9393 | #ifdef _PC_XATTR_ENABLED |
| 9394 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9395 | #endif |
| 9396 | #ifdef _PC_XATTR_EXISTS |
| 9397 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9398 | #endif |
| 9399 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9400 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9401 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9402 | }; |
| 9403 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9404 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9405 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9406 | { |
| 9407 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9408 | sizeof(posix_constants_pathconf) |
| 9409 | / sizeof(struct constdef)); |
| 9410 | } |
| 9411 | #endif |
| 9412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9413 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9414 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9415 | /*[clinic input] |
| 9416 | os.fpathconf -> long |
| 9417 | |
| 9418 | fd: int |
| 9419 | name: path_confname |
| 9420 | / |
| 9421 | |
| 9422 | Return the configuration limit name for the file descriptor fd. |
| 9423 | |
| 9424 | If there is no limit, return -1. |
| 9425 | [clinic start generated code]*/ |
| 9426 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9427 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9428 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 9429 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9430 | { |
| 9431 | long limit; |
| 9432 | |
| 9433 | errno = 0; |
| 9434 | limit = fpathconf(fd, name); |
| 9435 | if (limit == -1 && errno != 0) |
| 9436 | posix_error(); |
| 9437 | |
| 9438 | return limit; |
| 9439 | } |
| 9440 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9441 | |
| 9442 | |
| 9443 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9444 | /*[clinic input] |
| 9445 | os.pathconf -> long |
| 9446 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 9447 | name: path_confname |
| 9448 | |
| 9449 | Return the configuration limit name for the file or directory path. |
| 9450 | |
| 9451 | If there is no limit, return -1. |
| 9452 | On some platforms, path may also be specified as an open file descriptor. |
| 9453 | If this functionality is unavailable, using it raises an exception. |
| 9454 | [clinic start generated code]*/ |
| 9455 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9456 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9457 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 9458 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9459 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9460 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9461 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9462 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9463 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9464 | if (path->fd != -1) |
| 9465 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9466 | else |
| 9467 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9468 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9469 | if (limit == -1 && errno != 0) { |
| 9470 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9471 | /* could be a path or name problem */ |
| 9472 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9473 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9474 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9475 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9476 | |
| 9477 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9478 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9479 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9480 | |
| 9481 | #ifdef HAVE_CONFSTR |
| 9482 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9483 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9484 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9485 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9486 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9487 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9488 | #endif |
| 9489 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9490 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9491 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9492 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9493 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9494 | #endif |
| 9495 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9496 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9497 | #endif |
| 9498 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9499 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9500 | #endif |
| 9501 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9502 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9503 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9504 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9505 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9506 | #endif |
| 9507 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9508 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9509 | #endif |
| 9510 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9511 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9512 | #endif |
| 9513 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9514 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9515 | #endif |
| 9516 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9517 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9518 | #endif |
| 9519 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9520 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9521 | #endif |
| 9522 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9523 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9524 | #endif |
| 9525 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9526 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9527 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9528 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9529 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9530 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9531 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9532 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9533 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9534 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9535 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9536 | #endif |
| 9537 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9538 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9539 | #endif |
| 9540 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9541 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9542 | #endif |
| 9543 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9544 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9545 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9546 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9547 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9548 | #endif |
| 9549 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9550 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9551 | #endif |
| 9552 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9553 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9554 | #endif |
| 9555 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9556 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9557 | #endif |
| 9558 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9559 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9560 | #endif |
| 9561 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9562 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9563 | #endif |
| 9564 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9565 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9566 | #endif |
| 9567 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9568 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9569 | #endif |
| 9570 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9571 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9572 | #endif |
| 9573 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9574 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9575 | #endif |
| 9576 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9577 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9578 | #endif |
| 9579 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9580 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9581 | #endif |
| 9582 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9583 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9584 | #endif |
| 9585 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9586 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9587 | #endif |
| 9588 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9589 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9590 | #endif |
| 9591 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9592 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9593 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9594 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9595 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9596 | #endif |
| 9597 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9598 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9599 | #endif |
| 9600 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9601 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9602 | #endif |
| 9603 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9604 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9605 | #endif |
| 9606 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9607 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9608 | #endif |
| 9609 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9610 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9611 | #endif |
| 9612 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9613 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9614 | #endif |
| 9615 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9616 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9617 | #endif |
| 9618 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9619 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9620 | #endif |
| 9621 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9622 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9623 | #endif |
| 9624 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9625 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9626 | #endif |
| 9627 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9628 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9629 | #endif |
| 9630 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9631 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9632 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9633 | }; |
| 9634 | |
| 9635 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9636 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9637 | { |
| 9638 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9639 | sizeof(posix_constants_confstr) |
| 9640 | / sizeof(struct constdef)); |
| 9641 | } |
| 9642 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9643 | |
| 9644 | /*[clinic input] |
| 9645 | os.confstr |
| 9646 | |
| 9647 | name: confstr_confname |
| 9648 | / |
| 9649 | |
| 9650 | Return a string-valued system configuration variable. |
| 9651 | [clinic start generated code]*/ |
| 9652 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9653 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9654 | os_confstr_impl(PyObject *module, int name) |
| 9655 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9656 | { |
| 9657 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9658 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9659 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9660 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9661 | errno = 0; |
| 9662 | len = confstr(name, buffer, sizeof(buffer)); |
| 9663 | if (len == 0) { |
| 9664 | if (errno) { |
| 9665 | posix_error(); |
| 9666 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9667 | } |
| 9668 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9669 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9670 | } |
| 9671 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9672 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9673 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9674 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9675 | char *buf = PyMem_Malloc(len); |
| 9676 | if (buf == NULL) |
| 9677 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9678 | len2 = confstr(name, buf, len); |
| 9679 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 9680 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9681 | PyMem_Free(buf); |
| 9682 | } |
| 9683 | else |
| 9684 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9685 | return result; |
| 9686 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9687 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9688 | |
| 9689 | |
| 9690 | #ifdef HAVE_SYSCONF |
| 9691 | static struct constdef posix_constants_sysconf[] = { |
| 9692 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9693 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9694 | #endif |
| 9695 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9696 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9697 | #endif |
| 9698 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9699 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9700 | #endif |
| 9701 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9702 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9703 | #endif |
| 9704 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9705 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9706 | #endif |
| 9707 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9708 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9709 | #endif |
| 9710 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9711 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9712 | #endif |
| 9713 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9714 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9715 | #endif |
| 9716 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9717 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9718 | #endif |
| 9719 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9720 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9721 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9722 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9723 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9724 | #endif |
| 9725 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9726 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9727 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9728 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9729 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9730 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9731 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9732 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9733 | #endif |
| 9734 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9735 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9736 | #endif |
| 9737 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9738 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9739 | #endif |
| 9740 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9741 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9742 | #endif |
| 9743 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9744 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9745 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9746 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9747 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9748 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9749 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9750 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9751 | #endif |
| 9752 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9753 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9754 | #endif |
| 9755 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9756 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9757 | #endif |
| 9758 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9759 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9760 | #endif |
| 9761 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9762 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9763 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9764 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9765 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9766 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9767 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9768 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9769 | #endif |
| 9770 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9771 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9772 | #endif |
| 9773 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9774 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9775 | #endif |
| 9776 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9777 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9778 | #endif |
| 9779 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9780 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9781 | #endif |
| 9782 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9783 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9784 | #endif |
| 9785 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9786 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9787 | #endif |
| 9788 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9789 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9790 | #endif |
| 9791 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9792 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9793 | #endif |
| 9794 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9795 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9796 | #endif |
| 9797 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9798 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9799 | #endif |
| 9800 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9801 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9802 | #endif |
| 9803 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9804 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9805 | #endif |
| 9806 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9807 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9808 | #endif |
| 9809 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9810 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9811 | #endif |
| 9812 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9813 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9814 | #endif |
| 9815 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9816 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9817 | #endif |
| 9818 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9819 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9820 | #endif |
| 9821 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9822 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9823 | #endif |
| 9824 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9825 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9826 | #endif |
| 9827 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9828 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9829 | #endif |
| 9830 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9831 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9832 | #endif |
| 9833 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9834 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9835 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9836 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9837 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9838 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9839 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9840 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9841 | #endif |
| 9842 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9843 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9844 | #endif |
| 9845 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9846 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9847 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9848 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9849 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9850 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9851 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9852 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9853 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9854 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9855 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9856 | #endif |
| 9857 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9858 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9859 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9860 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9861 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9862 | #endif |
| 9863 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9864 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9865 | #endif |
| 9866 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9867 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9868 | #endif |
| 9869 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9870 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9871 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9872 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9873 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9874 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9875 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9876 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9877 | #endif |
| 9878 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9879 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9880 | #endif |
| 9881 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9882 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9883 | #endif |
| 9884 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9885 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9886 | #endif |
| 9887 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9888 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9889 | #endif |
| 9890 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9891 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9892 | #endif |
| 9893 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9894 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9895 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9896 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9897 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9898 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9899 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9900 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9901 | #endif |
| 9902 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9903 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9904 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9905 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9906 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9907 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9908 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9909 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9910 | #endif |
| 9911 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9912 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9913 | #endif |
| 9914 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9915 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9916 | #endif |
| 9917 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9918 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9919 | #endif |
| 9920 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9921 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9922 | #endif |
| 9923 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9924 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9925 | #endif |
| 9926 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9927 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9928 | #endif |
| 9929 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9930 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9931 | #endif |
| 9932 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9933 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9934 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9935 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9936 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9937 | #endif |
| 9938 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9939 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9940 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9941 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9942 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9943 | #endif |
| 9944 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9945 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9946 | #endif |
| 9947 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9948 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9949 | #endif |
| 9950 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9951 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9952 | #endif |
| 9953 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9954 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9955 | #endif |
| 9956 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9957 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9958 | #endif |
| 9959 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9960 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9961 | #endif |
| 9962 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9963 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9964 | #endif |
| 9965 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9966 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9967 | #endif |
| 9968 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9969 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9970 | #endif |
| 9971 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9972 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9973 | #endif |
| 9974 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9975 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9976 | #endif |
| 9977 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9978 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9979 | #endif |
| 9980 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9981 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9982 | #endif |
| 9983 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9984 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9985 | #endif |
| 9986 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9987 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9988 | #endif |
| 9989 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9990 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9991 | #endif |
| 9992 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9993 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9994 | #endif |
| 9995 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9996 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9997 | #endif |
| 9998 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9999 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10000 | #endif |
| 10001 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10002 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10003 | #endif |
| 10004 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10005 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10006 | #endif |
| 10007 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10008 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10009 | #endif |
| 10010 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10011 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10012 | #endif |
| 10013 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10014 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10015 | #endif |
| 10016 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10017 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10018 | #endif |
| 10019 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10020 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10021 | #endif |
| 10022 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10023 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10024 | #endif |
| 10025 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10026 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10027 | #endif |
| 10028 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10029 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10030 | #endif |
| 10031 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10032 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10033 | #endif |
| 10034 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10035 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10036 | #endif |
| 10037 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10038 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10039 | #endif |
| 10040 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10041 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10042 | #endif |
| 10043 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10044 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10045 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10046 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10047 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10048 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10049 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10050 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10051 | #endif |
| 10052 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10053 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10054 | #endif |
| 10055 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10056 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10057 | #endif |
| 10058 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10059 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10060 | #endif |
| 10061 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10062 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10063 | #endif |
| 10064 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10065 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10066 | #endif |
| 10067 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10068 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10069 | #endif |
| 10070 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10071 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10072 | #endif |
| 10073 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10074 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10075 | #endif |
| 10076 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10077 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10078 | #endif |
| 10079 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10080 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10081 | #endif |
| 10082 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10083 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10084 | #endif |
| 10085 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10086 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10087 | #endif |
| 10088 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10089 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10090 | #endif |
| 10091 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10092 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10093 | #endif |
| 10094 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10095 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10096 | #endif |
| 10097 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10098 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10099 | #endif |
| 10100 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10101 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10102 | #endif |
| 10103 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10104 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10105 | #endif |
| 10106 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10107 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10108 | #endif |
| 10109 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10110 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10111 | #endif |
| 10112 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10113 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10114 | #endif |
| 10115 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10116 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10117 | #endif |
| 10118 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10119 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10120 | #endif |
| 10121 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10122 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10123 | #endif |
| 10124 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10125 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10126 | #endif |
| 10127 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10128 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10129 | #endif |
| 10130 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10131 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10132 | #endif |
| 10133 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10134 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10135 | #endif |
| 10136 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10137 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10138 | #endif |
| 10139 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10140 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10141 | #endif |
| 10142 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10143 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10144 | #endif |
| 10145 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10146 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10147 | #endif |
| 10148 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10149 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10150 | #endif |
| 10151 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10152 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10153 | #endif |
| 10154 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10155 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10156 | #endif |
| 10157 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10158 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10159 | #endif |
| 10160 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10161 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10162 | #endif |
| 10163 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10164 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10165 | #endif |
| 10166 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10167 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10168 | #endif |
| 10169 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10170 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10171 | #endif |
| 10172 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10173 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10174 | #endif |
| 10175 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10176 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10177 | #endif |
| 10178 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10179 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10180 | #endif |
| 10181 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10182 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10183 | #endif |
| 10184 | }; |
| 10185 | |
| 10186 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10187 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10188 | { |
| 10189 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 10190 | sizeof(posix_constants_sysconf) |
| 10191 | / sizeof(struct constdef)); |
| 10192 | } |
| 10193 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10194 | |
| 10195 | /*[clinic input] |
| 10196 | os.sysconf -> long |
| 10197 | name: sysconf_confname |
| 10198 | / |
| 10199 | |
| 10200 | Return an integer-valued system configuration variable. |
| 10201 | [clinic start generated code]*/ |
| 10202 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10203 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10204 | os_sysconf_impl(PyObject *module, int name) |
| 10205 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10206 | { |
| 10207 | long value; |
| 10208 | |
| 10209 | errno = 0; |
| 10210 | value = sysconf(name); |
| 10211 | if (value == -1 && errno != 0) |
| 10212 | posix_error(); |
| 10213 | return value; |
| 10214 | } |
| 10215 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10216 | |
| 10217 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10218 | /* 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] | 10219 | * 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] | 10220 | * the exported dictionaries that are used to publish information about the |
| 10221 | * names available on the host platform. |
| 10222 | * |
| 10223 | * Sorting the table at runtime ensures that the table is properly ordered |
| 10224 | * when used, even for platforms we're not able to test on. It also makes |
| 10225 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10226 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10227 | |
| 10228 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10229 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10230 | { |
| 10231 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10232 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10233 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10234 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10235 | |
| 10236 | return strcmp(c1->name, c2->name); |
| 10237 | } |
| 10238 | |
| 10239 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10240 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 10241 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10242 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10243 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10244 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10245 | |
| 10246 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10247 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10248 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10249 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10250 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10251 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10252 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10253 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10254 | Py_XDECREF(o); |
| 10255 | Py_DECREF(d); |
| 10256 | return -1; |
| 10257 | } |
| 10258 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10259 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10260 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10261 | } |
| 10262 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10263 | /* Return -1 on failure, 0 on success. */ |
| 10264 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10265 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10266 | { |
| 10267 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10268 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10269 | sizeof(posix_constants_pathconf) |
| 10270 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10271 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10272 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10273 | #endif |
| 10274 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10275 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10276 | sizeof(posix_constants_confstr) |
| 10277 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10278 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10279 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10280 | #endif |
| 10281 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10282 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10283 | sizeof(posix_constants_sysconf) |
| 10284 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10285 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10286 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10287 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10288 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10289 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10290 | |
| 10291 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10292 | /*[clinic input] |
| 10293 | os.abort |
| 10294 | |
| 10295 | Abort the interpreter immediately. |
| 10296 | |
| 10297 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 10298 | on the hosting operating system. This function never returns. |
| 10299 | [clinic start generated code]*/ |
| 10300 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10301 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10302 | os_abort_impl(PyObject *module) |
| 10303 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10304 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10305 | abort(); |
| 10306 | /*NOTREACHED*/ |
| 10307 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10308 | return NULL; |
| 10309 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10310 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10311 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10312 | /* Grab ShellExecute dynamically from shell32 */ |
| 10313 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10314 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 10315 | LPCWSTR, INT); |
| 10316 | static int |
| 10317 | check_ShellExecute() |
| 10318 | { |
| 10319 | HINSTANCE hShell32; |
| 10320 | |
| 10321 | /* only recheck */ |
| 10322 | if (-1 == has_ShellExecute) { |
| 10323 | Py_BEGIN_ALLOW_THREADS |
| 10324 | hShell32 = LoadLibraryW(L"SHELL32"); |
| 10325 | Py_END_ALLOW_THREADS |
| 10326 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10327 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 10328 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10329 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10330 | } else { |
| 10331 | has_ShellExecute = 0; |
| 10332 | } |
| 10333 | } |
| 10334 | return has_ShellExecute; |
| 10335 | } |
| 10336 | |
| 10337 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10338 | /*[clinic input] |
| 10339 | os.startfile |
| 10340 | filepath: path_t |
| 10341 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10342 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10343 | startfile(filepath [, operation]) |
| 10344 | |
| 10345 | Start a file with its associated application. |
| 10346 | |
| 10347 | When "operation" is not specified or "open", this acts like |
| 10348 | double-clicking the file in Explorer, or giving the file name as an |
| 10349 | argument to the DOS "start" command: the file is opened with whatever |
| 10350 | application (if any) its extension is associated. |
| 10351 | When another "operation" is given, it specifies what should be done with |
| 10352 | the file. A typical operation is "print". |
| 10353 | |
| 10354 | startfile returns as soon as the associated application is launched. |
| 10355 | There is no option to wait for the application to close, and no way |
| 10356 | to retrieve the application's exit status. |
| 10357 | |
| 10358 | The filepath is relative to the current directory. If you want to use |
| 10359 | an absolute path, make sure the first character is not a slash ("/"); |
| 10360 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 10361 | [clinic start generated code]*/ |
| 10362 | |
| 10363 | static PyObject * |
| 10364 | os_startfile_impl(PyObject *module, path_t *filepath, Py_UNICODE *operation) |
| 10365 | /*[clinic end generated code: output=912ceba79acfa1c9 input=63950bf2986380d0]*/ |
| 10366 | { |
| 10367 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10368 | |
| 10369 | if(!check_ShellExecute()) { |
| 10370 | /* If the OS doesn't have ShellExecute, return a |
| 10371 | NotImplementedError. */ |
| 10372 | return PyErr_Format(PyExc_NotImplementedError, |
| 10373 | "startfile not available on this platform"); |
| 10374 | } |
| 10375 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10376 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10377 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10378 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10379 | Py_END_ALLOW_THREADS |
| 10380 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10381 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10382 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10383 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10384 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10385 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10386 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10387 | #endif /* MS_WINDOWS */ |
| 10388 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10389 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10390 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10391 | /*[clinic input] |
| 10392 | os.getloadavg |
| 10393 | |
| 10394 | Return average recent system load information. |
| 10395 | |
| 10396 | Return the number of processes in the system run queue averaged over |
| 10397 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 10398 | Raises OSError if the load average was unobtainable. |
| 10399 | [clinic start generated code]*/ |
| 10400 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10401 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10402 | os_getloadavg_impl(PyObject *module) |
| 10403 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10404 | { |
| 10405 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10406 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10407 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10408 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10409 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10410 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10411 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10412 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10413 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10414 | |
| 10415 | /*[clinic input] |
| 10416 | os.device_encoding |
| 10417 | fd: int |
| 10418 | |
| 10419 | Return a string describing the encoding of a terminal's file descriptor. |
| 10420 | |
| 10421 | The file descriptor must be attached to a terminal. |
| 10422 | If the device is not a terminal, return None. |
| 10423 | [clinic start generated code]*/ |
| 10424 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10425 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10426 | os_device_encoding_impl(PyObject *module, int fd) |
| 10427 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10428 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10429 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10430 | } |
| 10431 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10432 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10433 | #ifdef HAVE_SETRESUID |
| 10434 | /*[clinic input] |
| 10435 | os.setresuid |
| 10436 | |
| 10437 | ruid: uid_t |
| 10438 | euid: uid_t |
| 10439 | suid: uid_t |
| 10440 | / |
| 10441 | |
| 10442 | Set the current process's real, effective, and saved user ids. |
| 10443 | [clinic start generated code]*/ |
| 10444 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10445 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10446 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 10447 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10448 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10449 | if (setresuid(ruid, euid, suid) < 0) |
| 10450 | return posix_error(); |
| 10451 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10452 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10453 | #endif /* HAVE_SETRESUID */ |
| 10454 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10455 | |
| 10456 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10457 | /*[clinic input] |
| 10458 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10459 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10460 | rgid: gid_t |
| 10461 | egid: gid_t |
| 10462 | sgid: gid_t |
| 10463 | / |
| 10464 | |
| 10465 | Set the current process's real, effective, and saved group ids. |
| 10466 | [clinic start generated code]*/ |
| 10467 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10468 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10469 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 10470 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10471 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10472 | if (setresgid(rgid, egid, sgid) < 0) |
| 10473 | return posix_error(); |
| 10474 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10475 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10476 | #endif /* HAVE_SETRESGID */ |
| 10477 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10478 | |
| 10479 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10480 | /*[clinic input] |
| 10481 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10482 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10483 | Return a tuple of the current process's real, effective, and saved user ids. |
| 10484 | [clinic start generated code]*/ |
| 10485 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10486 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10487 | os_getresuid_impl(PyObject *module) |
| 10488 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10489 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10490 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10491 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10492 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10493 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 10494 | _PyLong_FromUid(euid), |
| 10495 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10496 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10497 | #endif /* HAVE_GETRESUID */ |
| 10498 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10499 | |
| 10500 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10501 | /*[clinic input] |
| 10502 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10503 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10504 | Return a tuple of the current process's real, effective, and saved group ids. |
| 10505 | [clinic start generated code]*/ |
| 10506 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10507 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10508 | os_getresgid_impl(PyObject *module) |
| 10509 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10510 | { |
| 10511 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10512 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10513 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10514 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 10515 | _PyLong_FromGid(egid), |
| 10516 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10517 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10518 | #endif /* HAVE_GETRESGID */ |
| 10519 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10520 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10521 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10522 | /*[clinic input] |
| 10523 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10524 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10525 | path: path_t(allow_fd=True) |
| 10526 | attribute: path_t |
| 10527 | * |
| 10528 | follow_symlinks: bool = True |
| 10529 | |
| 10530 | Return the value of extended attribute attribute on path. |
| 10531 | |
| 10532 | path may be either a string or an open file descriptor. |
| 10533 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10534 | link, getxattr will examine the symbolic link itself instead of the file |
| 10535 | the link points to. |
| 10536 | |
| 10537 | [clinic start generated code]*/ |
| 10538 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10539 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10540 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10541 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10542 | /*[clinic end generated code: output=5f2f44200a43cff2 input=8c8ea3bab78d89c2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10543 | { |
| 10544 | Py_ssize_t i; |
| 10545 | PyObject *buffer = NULL; |
| 10546 | |
| 10547 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 10548 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10549 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10550 | for (i = 0; ; i++) { |
| 10551 | void *ptr; |
| 10552 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10553 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10554 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10555 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10556 | path_error(path); |
| 10557 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10558 | } |
| 10559 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10560 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10561 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10562 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10563 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10564 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10565 | if (path->fd >= 0) |
| 10566 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10567 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10568 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10569 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10570 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10571 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10572 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10573 | if (result < 0) { |
| 10574 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10575 | if (errno == ERANGE) |
| 10576 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10577 | path_error(path); |
| 10578 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10579 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10580 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10581 | if (result != buffer_size) { |
| 10582 | /* Can only shrink. */ |
| 10583 | _PyBytes_Resize(&buffer, result); |
| 10584 | } |
| 10585 | break; |
| 10586 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10587 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10588 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10589 | } |
| 10590 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10591 | |
| 10592 | /*[clinic input] |
| 10593 | os.setxattr |
| 10594 | |
| 10595 | path: path_t(allow_fd=True) |
| 10596 | attribute: path_t |
| 10597 | value: Py_buffer |
| 10598 | flags: int = 0 |
| 10599 | * |
| 10600 | follow_symlinks: bool = True |
| 10601 | |
| 10602 | Set extended attribute attribute on path to value. |
| 10603 | |
| 10604 | path may be either a string or an open file descriptor. |
| 10605 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10606 | link, setxattr will modify the symbolic link itself instead of the file |
| 10607 | the link points to. |
| 10608 | |
| 10609 | [clinic start generated code]*/ |
| 10610 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10611 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10612 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10613 | Py_buffer *value, int flags, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10614 | /*[clinic end generated code: output=98b83f63fdde26bb input=f0d26833992015c2]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10615 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10616 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10617 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10618 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10619 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10620 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10621 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10622 | if (path->fd > -1) |
| 10623 | result = fsetxattr(path->fd, attribute->narrow, |
| 10624 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10625 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10626 | result = setxattr(path->narrow, attribute->narrow, |
| 10627 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10628 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10629 | result = lsetxattr(path->narrow, attribute->narrow, |
| 10630 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10631 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10632 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10633 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10634 | path_error(path); |
| 10635 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10636 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10637 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10638 | Py_RETURN_NONE; |
| 10639 | } |
| 10640 | |
| 10641 | |
| 10642 | /*[clinic input] |
| 10643 | os.removexattr |
| 10644 | |
| 10645 | path: path_t(allow_fd=True) |
| 10646 | attribute: path_t |
| 10647 | * |
| 10648 | follow_symlinks: bool = True |
| 10649 | |
| 10650 | Remove extended attribute attribute on path. |
| 10651 | |
| 10652 | path may be either a string or an open file descriptor. |
| 10653 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10654 | link, removexattr will modify the symbolic link itself instead of the file |
| 10655 | the link points to. |
| 10656 | |
| 10657 | [clinic start generated code]*/ |
| 10658 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10659 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10660 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10661 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10662 | /*[clinic end generated code: output=521a51817980cda6 input=cdb54834161e3329]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10663 | { |
| 10664 | ssize_t result; |
| 10665 | |
| 10666 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 10667 | return NULL; |
| 10668 | |
| 10669 | Py_BEGIN_ALLOW_THREADS; |
| 10670 | if (path->fd > -1) |
| 10671 | result = fremovexattr(path->fd, attribute->narrow); |
| 10672 | else if (follow_symlinks) |
| 10673 | result = removexattr(path->narrow, attribute->narrow); |
| 10674 | else |
| 10675 | result = lremovexattr(path->narrow, attribute->narrow); |
| 10676 | Py_END_ALLOW_THREADS; |
| 10677 | |
| 10678 | if (result) { |
| 10679 | return path_error(path); |
| 10680 | } |
| 10681 | |
| 10682 | Py_RETURN_NONE; |
| 10683 | } |
| 10684 | |
| 10685 | |
| 10686 | /*[clinic input] |
| 10687 | os.listxattr |
| 10688 | |
| 10689 | path: path_t(allow_fd=True, nullable=True) = None |
| 10690 | * |
| 10691 | follow_symlinks: bool = True |
| 10692 | |
| 10693 | Return a list of extended attributes on path. |
| 10694 | |
| 10695 | path may be either None, a string, or an open file descriptor. |
| 10696 | if path is None, listxattr will examine the current directory. |
| 10697 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10698 | link, listxattr will examine the symbolic link itself instead of the file |
| 10699 | the link points to. |
| 10700 | [clinic start generated code]*/ |
| 10701 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10702 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10703 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
| 10704 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=08cca53ac0b07c13]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10705 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10706 | Py_ssize_t i; |
| 10707 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10708 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10709 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10710 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10711 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10712 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10713 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10714 | name = path->narrow ? path->narrow : "."; |
| 10715 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10716 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 10717 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10718 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10719 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10720 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10721 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10722 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10723 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10724 | break; |
| 10725 | } |
| 10726 | buffer = PyMem_MALLOC(buffer_size); |
| 10727 | if (!buffer) { |
| 10728 | PyErr_NoMemory(); |
| 10729 | break; |
| 10730 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10731 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10732 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10733 | if (path->fd > -1) |
| 10734 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10735 | else if (follow_symlinks) |
| 10736 | length = listxattr(name, buffer, buffer_size); |
| 10737 | else |
| 10738 | length = llistxattr(name, buffer, buffer_size); |
| 10739 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10740 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10741 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10742 | if (errno == ERANGE) { |
| 10743 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 10744 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10745 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10746 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10747 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10748 | break; |
| 10749 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10750 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10751 | result = PyList_New(0); |
| 10752 | if (!result) { |
| 10753 | goto exit; |
| 10754 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10755 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10756 | end = buffer + length; |
| 10757 | for (trace = start = buffer; trace != end; trace++) { |
| 10758 | if (!*trace) { |
| 10759 | int error; |
| 10760 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10761 | trace - start); |
| 10762 | if (!attribute) { |
| 10763 | Py_DECREF(result); |
| 10764 | result = NULL; |
| 10765 | goto exit; |
| 10766 | } |
| 10767 | error = PyList_Append(result, attribute); |
| 10768 | Py_DECREF(attribute); |
| 10769 | if (error) { |
| 10770 | Py_DECREF(result); |
| 10771 | result = NULL; |
| 10772 | goto exit; |
| 10773 | } |
| 10774 | start = trace + 1; |
| 10775 | } |
| 10776 | } |
| 10777 | break; |
| 10778 | } |
| 10779 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10780 | if (buffer) |
| 10781 | PyMem_FREE(buffer); |
| 10782 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10783 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10784 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10785 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10786 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10787 | /*[clinic input] |
| 10788 | os.urandom |
| 10789 | |
| 10790 | size: Py_ssize_t |
| 10791 | / |
| 10792 | |
| 10793 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 10794 | [clinic start generated code]*/ |
| 10795 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10796 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10797 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 10798 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10799 | { |
| 10800 | PyObject *bytes; |
| 10801 | int result; |
| 10802 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10803 | if (size < 0) |
| 10804 | return PyErr_Format(PyExc_ValueError, |
| 10805 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10806 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 10807 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10808 | return NULL; |
| 10809 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 10810 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10811 | if (result == -1) { |
| 10812 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10813 | return NULL; |
| 10814 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10815 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10816 | } |
| 10817 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10818 | /* Terminal size querying */ |
| 10819 | |
| 10820 | static PyTypeObject TerminalSizeType; |
| 10821 | |
| 10822 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10823 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10824 | |
| 10825 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10826 | {"columns", "width of the terminal window in characters"}, |
| 10827 | {"lines", "height of the terminal window in characters"}, |
| 10828 | {NULL, NULL} |
| 10829 | }; |
| 10830 | |
| 10831 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10832 | "os.terminal_size", |
| 10833 | TerminalSize_docstring, |
| 10834 | TerminalSize_fields, |
| 10835 | 2, |
| 10836 | }; |
| 10837 | |
| 10838 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10839 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10840 | PyDoc_STRVAR(termsize__doc__, |
| 10841 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10842 | "\n" \ |
| 10843 | "The optional argument fd (default standard output) specifies\n" \ |
| 10844 | "which file descriptor should be queried.\n" \ |
| 10845 | "\n" \ |
| 10846 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10847 | "is thrown.\n" \ |
| 10848 | "\n" \ |
| 10849 | "This function will only be defined if an implementation is\n" \ |
| 10850 | "available for this system.\n" \ |
| 10851 | "\n" \ |
| 10852 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10853 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10854 | |
| 10855 | static PyObject* |
| 10856 | get_terminal_size(PyObject *self, PyObject *args) |
| 10857 | { |
| 10858 | int columns, lines; |
| 10859 | PyObject *termsize; |
| 10860 | |
| 10861 | int fd = fileno(stdout); |
| 10862 | /* Under some conditions stdout may not be connected and |
| 10863 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10864 | * GUI apps don't have valid standard streams by default. |
| 10865 | * |
| 10866 | * If this happens, and the optional fd argument is not present, |
| 10867 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10868 | */ |
| 10869 | |
| 10870 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10871 | return NULL; |
| 10872 | |
| 10873 | #ifdef TERMSIZE_USE_IOCTL |
| 10874 | { |
| 10875 | struct winsize w; |
| 10876 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10877 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10878 | columns = w.ws_col; |
| 10879 | lines = w.ws_row; |
| 10880 | } |
| 10881 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10882 | |
| 10883 | #ifdef TERMSIZE_USE_CONIO |
| 10884 | { |
| 10885 | DWORD nhandle; |
| 10886 | HANDLE handle; |
| 10887 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10888 | switch (fd) { |
| 10889 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10890 | break; |
| 10891 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10892 | break; |
| 10893 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10894 | break; |
| 10895 | default: |
| 10896 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10897 | } |
| 10898 | handle = GetStdHandle(nhandle); |
| 10899 | if (handle == NULL) |
| 10900 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10901 | if (handle == INVALID_HANDLE_VALUE) |
| 10902 | return PyErr_SetFromWindowsErr(0); |
| 10903 | |
| 10904 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10905 | return PyErr_SetFromWindowsErr(0); |
| 10906 | |
| 10907 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10908 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10909 | } |
| 10910 | #endif /* TERMSIZE_USE_CONIO */ |
| 10911 | |
| 10912 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10913 | if (termsize == NULL) |
| 10914 | return NULL; |
| 10915 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10916 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10917 | if (PyErr_Occurred()) { |
| 10918 | Py_DECREF(termsize); |
| 10919 | return NULL; |
| 10920 | } |
| 10921 | return termsize; |
| 10922 | } |
| 10923 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10924 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10925 | |
| 10926 | /*[clinic input] |
| 10927 | os.cpu_count |
| 10928 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 10929 | Return the number of CPUs in the system; return None if indeterminable. |
| 10930 | |
| 10931 | This number is not equivalent to the number of CPUs the current process can |
| 10932 | use. The number of usable CPUs can be obtained with |
| 10933 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10934 | [clinic start generated code]*/ |
| 10935 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10936 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10937 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 10938 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10939 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 10940 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10941 | #ifdef MS_WINDOWS |
| 10942 | SYSTEM_INFO sysinfo; |
| 10943 | GetSystemInfo(&sysinfo); |
| 10944 | ncpu = sysinfo.dwNumberOfProcessors; |
| 10945 | #elif defined(__hpux) |
| 10946 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 10947 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 10948 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10949 | #elif defined(__DragonFly__) || \ |
| 10950 | defined(__OpenBSD__) || \ |
| 10951 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 10952 | defined(__NetBSD__) || \ |
| 10953 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 10954 | int mib[2]; |
| 10955 | size_t len = sizeof(ncpu); |
| 10956 | mib[0] = CTL_HW; |
| 10957 | mib[1] = HW_NCPU; |
| 10958 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 10959 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10960 | #endif |
| 10961 | if (ncpu >= 1) |
| 10962 | return PyLong_FromLong(ncpu); |
| 10963 | else |
| 10964 | Py_RETURN_NONE; |
| 10965 | } |
| 10966 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10967 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10968 | /*[clinic input] |
| 10969 | os.get_inheritable -> bool |
| 10970 | |
| 10971 | fd: int |
| 10972 | / |
| 10973 | |
| 10974 | Get the close-on-exe flag of the specified file descriptor. |
| 10975 | [clinic start generated code]*/ |
| 10976 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10977 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10978 | os_get_inheritable_impl(PyObject *module, int fd) |
| 10979 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10980 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 10981 | int return_value; |
| 10982 | if (!_PyVerify_fd(fd)) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10983 | posix_error(); |
| 10984 | return -1; |
| 10985 | } |
| 10986 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 10987 | _Py_BEGIN_SUPPRESS_IPH |
| 10988 | return_value = _Py_get_inheritable(fd); |
| 10989 | _Py_END_SUPPRESS_IPH |
| 10990 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10991 | } |
| 10992 | |
| 10993 | |
| 10994 | /*[clinic input] |
| 10995 | os.set_inheritable |
| 10996 | fd: int |
| 10997 | inheritable: int |
| 10998 | / |
| 10999 | |
| 11000 | Set the inheritable flag of the specified file descriptor. |
| 11001 | [clinic start generated code]*/ |
| 11002 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11003 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11004 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 11005 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11006 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11007 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11008 | if (!_PyVerify_fd(fd)) |
| 11009 | return posix_error(); |
| 11010 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11011 | _Py_BEGIN_SUPPRESS_IPH |
| 11012 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 11013 | _Py_END_SUPPRESS_IPH |
| 11014 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11015 | return NULL; |
| 11016 | Py_RETURN_NONE; |
| 11017 | } |
| 11018 | |
| 11019 | |
| 11020 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11021 | /*[clinic input] |
| 11022 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11023 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11024 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11025 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11026 | Get the close-on-exe flag of the specified file descriptor. |
| 11027 | [clinic start generated code]*/ |
| 11028 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11029 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11030 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 11031 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11032 | { |
| 11033 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11034 | |
| 11035 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 11036 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11037 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11038 | } |
| 11039 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11040 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11041 | } |
| 11042 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11043 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11044 | /*[clinic input] |
| 11045 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11046 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11047 | inheritable: bool |
| 11048 | / |
| 11049 | |
| 11050 | Set the inheritable flag of the specified handle. |
| 11051 | [clinic start generated code]*/ |
| 11052 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11053 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 11054 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11055 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 11056 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11057 | { |
| 11058 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11059 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 11060 | PyErr_SetFromWindowsErr(0); |
| 11061 | return NULL; |
| 11062 | } |
| 11063 | Py_RETURN_NONE; |
| 11064 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11065 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11066 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11067 | #ifndef MS_WINDOWS |
| 11068 | PyDoc_STRVAR(get_blocking__doc__, |
| 11069 | "get_blocking(fd) -> bool\n" \ |
| 11070 | "\n" \ |
| 11071 | "Get the blocking mode of the file descriptor:\n" \ |
| 11072 | "False if the O_NONBLOCK flag is set, True if the flag is cleared."); |
| 11073 | |
| 11074 | static PyObject* |
| 11075 | posix_get_blocking(PyObject *self, PyObject *args) |
| 11076 | { |
| 11077 | int fd; |
| 11078 | int blocking; |
| 11079 | |
| 11080 | if (!PyArg_ParseTuple(args, "i:get_blocking", &fd)) |
| 11081 | return NULL; |
| 11082 | |
| 11083 | if (!_PyVerify_fd(fd)) |
| 11084 | return posix_error(); |
| 11085 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11086 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11087 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11088 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11089 | if (blocking < 0) |
| 11090 | return NULL; |
| 11091 | return PyBool_FromLong(blocking); |
| 11092 | } |
| 11093 | |
| 11094 | PyDoc_STRVAR(set_blocking__doc__, |
| 11095 | "set_blocking(fd, blocking)\n" \ |
| 11096 | "\n" \ |
| 11097 | "Set the blocking mode of the specified file descriptor.\n" \ |
| 11098 | "Set the O_NONBLOCK flag if blocking is False,\n" \ |
| 11099 | "clear the O_NONBLOCK flag otherwise."); |
| 11100 | |
| 11101 | static PyObject* |
| 11102 | posix_set_blocking(PyObject *self, PyObject *args) |
| 11103 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11104 | int fd, blocking, result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11105 | |
| 11106 | if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking)) |
| 11107 | return NULL; |
| 11108 | |
| 11109 | if (!_PyVerify_fd(fd)) |
| 11110 | return posix_error(); |
| 11111 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11112 | _Py_BEGIN_SUPPRESS_IPH |
| 11113 | result = _Py_set_blocking(fd, blocking); |
| 11114 | _Py_END_SUPPRESS_IPH |
| 11115 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11116 | return NULL; |
| 11117 | Py_RETURN_NONE; |
| 11118 | } |
| 11119 | #endif /* !MS_WINDOWS */ |
| 11120 | |
| 11121 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11122 | PyDoc_STRVAR(posix_scandir__doc__, |
| 11123 | "scandir(path='.') -> iterator of DirEntry objects for given path"); |
| 11124 | |
| 11125 | static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL}; |
| 11126 | |
| 11127 | typedef struct { |
| 11128 | PyObject_HEAD |
| 11129 | PyObject *name; |
| 11130 | PyObject *path; |
| 11131 | PyObject *stat; |
| 11132 | PyObject *lstat; |
| 11133 | #ifdef MS_WINDOWS |
| 11134 | struct _Py_stat_struct win32_lstat; |
| 11135 | __int64 win32_file_index; |
| 11136 | int got_file_index; |
| 11137 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11138 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11139 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11140 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11141 | ino_t d_ino; |
| 11142 | #endif |
| 11143 | } DirEntry; |
| 11144 | |
| 11145 | static void |
| 11146 | DirEntry_dealloc(DirEntry *entry) |
| 11147 | { |
| 11148 | Py_XDECREF(entry->name); |
| 11149 | Py_XDECREF(entry->path); |
| 11150 | Py_XDECREF(entry->stat); |
| 11151 | Py_XDECREF(entry->lstat); |
| 11152 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 11153 | } |
| 11154 | |
| 11155 | /* Forward reference */ |
| 11156 | static int |
| 11157 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 11158 | |
| 11159 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11160 | static int |
| 11161 | DirEntry_is_symlink(DirEntry *self) |
| 11162 | { |
| 11163 | #ifdef MS_WINDOWS |
| 11164 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11165 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 11166 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11167 | if (self->d_type != DT_UNKNOWN) |
| 11168 | return self->d_type == DT_LNK; |
| 11169 | else |
| 11170 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11171 | #else |
| 11172 | /* POSIX without d_type */ |
| 11173 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11174 | #endif |
| 11175 | } |
| 11176 | |
| 11177 | static PyObject * |
| 11178 | DirEntry_py_is_symlink(DirEntry *self) |
| 11179 | { |
| 11180 | int result; |
| 11181 | |
| 11182 | result = DirEntry_is_symlink(self); |
| 11183 | if (result == -1) |
| 11184 | return NULL; |
| 11185 | return PyBool_FromLong(result); |
| 11186 | } |
| 11187 | |
| 11188 | static PyObject * |
| 11189 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 11190 | { |
| 11191 | int result; |
| 11192 | struct _Py_stat_struct st; |
| 11193 | |
| 11194 | #ifdef MS_WINDOWS |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11195 | const wchar_t *path; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11196 | |
| 11197 | path = PyUnicode_AsUnicode(self->path); |
| 11198 | if (!path) |
| 11199 | return NULL; |
| 11200 | |
| 11201 | if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11202 | result = win32_stat(path, &st); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11203 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11204 | result = win32_lstat(path, &st); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11205 | |
| 11206 | if (result != 0) { |
| 11207 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11208 | 0, self->path); |
| 11209 | } |
| 11210 | #else /* POSIX */ |
| 11211 | PyObject *bytes; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11212 | const char *path; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11213 | |
| 11214 | if (!PyUnicode_FSConverter(self->path, &bytes)) |
| 11215 | return NULL; |
| 11216 | path = PyBytes_AS_STRING(bytes); |
| 11217 | |
| 11218 | if (follow_symlinks) |
| 11219 | result = STAT(path, &st); |
| 11220 | else |
| 11221 | result = LSTAT(path, &st); |
| 11222 | Py_DECREF(bytes); |
| 11223 | |
| 11224 | if (result != 0) |
| 11225 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path); |
| 11226 | #endif |
| 11227 | |
| 11228 | return _pystat_fromstructstat(&st); |
| 11229 | } |
| 11230 | |
| 11231 | static PyObject * |
| 11232 | DirEntry_get_lstat(DirEntry *self) |
| 11233 | { |
| 11234 | if (!self->lstat) { |
| 11235 | #ifdef MS_WINDOWS |
| 11236 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 11237 | #else /* POSIX */ |
| 11238 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 11239 | #endif |
| 11240 | } |
| 11241 | Py_XINCREF(self->lstat); |
| 11242 | return self->lstat; |
| 11243 | } |
| 11244 | |
| 11245 | static PyObject * |
| 11246 | DirEntry_get_stat(DirEntry *self, int follow_symlinks) |
| 11247 | { |
| 11248 | if (!follow_symlinks) |
| 11249 | return DirEntry_get_lstat(self); |
| 11250 | |
| 11251 | if (!self->stat) { |
| 11252 | int result = DirEntry_is_symlink(self); |
| 11253 | if (result == -1) |
| 11254 | return NULL; |
| 11255 | else if (result) |
| 11256 | self->stat = DirEntry_fetch_stat(self, 1); |
| 11257 | else |
| 11258 | self->stat = DirEntry_get_lstat(self); |
| 11259 | } |
| 11260 | |
| 11261 | Py_XINCREF(self->stat); |
| 11262 | return self->stat; |
| 11263 | } |
| 11264 | |
| 11265 | static PyObject * |
| 11266 | DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11267 | { |
| 11268 | int follow_symlinks = 1; |
| 11269 | |
| 11270 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.stat", |
| 11271 | follow_symlinks_keywords, &follow_symlinks)) |
| 11272 | return NULL; |
| 11273 | |
| 11274 | return DirEntry_get_stat(self, follow_symlinks); |
| 11275 | } |
| 11276 | |
| 11277 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11278 | static int |
| 11279 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11280 | { |
| 11281 | PyObject *stat = NULL; |
| 11282 | PyObject *st_mode = NULL; |
| 11283 | long mode; |
| 11284 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11285 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11286 | int is_symlink; |
| 11287 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11288 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11289 | #ifdef MS_WINDOWS |
| 11290 | unsigned long dir_bits; |
| 11291 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11292 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11293 | |
| 11294 | #ifdef MS_WINDOWS |
| 11295 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 11296 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11297 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11298 | is_symlink = self->d_type == DT_LNK; |
| 11299 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 11300 | #endif |
| 11301 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11302 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11303 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11304 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11305 | stat = DirEntry_get_stat(self, follow_symlinks); |
| 11306 | if (!stat) { |
| 11307 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 11308 | /* If file doesn't exist (anymore), then return False |
| 11309 | (i.e., say it's not a file/directory) */ |
| 11310 | PyErr_Clear(); |
| 11311 | return 0; |
| 11312 | } |
| 11313 | goto error; |
| 11314 | } |
| 11315 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 11316 | if (!st_mode) |
| 11317 | goto error; |
| 11318 | |
| 11319 | mode = PyLong_AsLong(st_mode); |
| 11320 | if (mode == -1 && PyErr_Occurred()) |
| 11321 | goto error; |
| 11322 | Py_CLEAR(st_mode); |
| 11323 | Py_CLEAR(stat); |
| 11324 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11325 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11326 | } |
| 11327 | else if (is_symlink) { |
| 11328 | assert(mode_bits != S_IFLNK); |
| 11329 | result = 0; |
| 11330 | } |
| 11331 | else { |
| 11332 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 11333 | #ifdef MS_WINDOWS |
| 11334 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 11335 | if (mode_bits == S_IFDIR) |
| 11336 | result = dir_bits != 0; |
| 11337 | else |
| 11338 | result = dir_bits == 0; |
| 11339 | #else /* POSIX */ |
| 11340 | if (mode_bits == S_IFDIR) |
| 11341 | result = self->d_type == DT_DIR; |
| 11342 | else |
| 11343 | result = self->d_type == DT_REG; |
| 11344 | #endif |
| 11345 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11346 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11347 | |
| 11348 | return result; |
| 11349 | |
| 11350 | error: |
| 11351 | Py_XDECREF(st_mode); |
| 11352 | Py_XDECREF(stat); |
| 11353 | return -1; |
| 11354 | } |
| 11355 | |
| 11356 | static PyObject * |
| 11357 | DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11358 | { |
| 11359 | int result; |
| 11360 | |
| 11361 | result = DirEntry_test_mode(self, follow_symlinks, mode_bits); |
| 11362 | if (result == -1) |
| 11363 | return NULL; |
| 11364 | return PyBool_FromLong(result); |
| 11365 | } |
| 11366 | |
| 11367 | static PyObject * |
| 11368 | DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11369 | { |
| 11370 | int follow_symlinks = 1; |
| 11371 | |
| 11372 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_dir", |
| 11373 | follow_symlinks_keywords, &follow_symlinks)) |
| 11374 | return NULL; |
| 11375 | |
| 11376 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR); |
| 11377 | } |
| 11378 | |
| 11379 | static PyObject * |
| 11380 | DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11381 | { |
| 11382 | int follow_symlinks = 1; |
| 11383 | |
| 11384 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_file", |
| 11385 | follow_symlinks_keywords, &follow_symlinks)) |
| 11386 | return NULL; |
| 11387 | |
| 11388 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG); |
| 11389 | } |
| 11390 | |
| 11391 | static PyObject * |
| 11392 | DirEntry_inode(DirEntry *self) |
| 11393 | { |
| 11394 | #ifdef MS_WINDOWS |
| 11395 | if (!self->got_file_index) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11396 | const wchar_t *path; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11397 | struct _Py_stat_struct stat; |
| 11398 | |
| 11399 | path = PyUnicode_AsUnicode(self->path); |
| 11400 | if (!path) |
| 11401 | return NULL; |
| 11402 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11403 | if (win32_lstat(path, &stat) != 0) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11404 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11405 | 0, self->path); |
| 11406 | } |
| 11407 | |
| 11408 | self->win32_file_index = stat.st_ino; |
| 11409 | self->got_file_index = 1; |
| 11410 | } |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 11411 | return PyLong_FromLongLong((long long)self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11412 | #else /* POSIX */ |
| 11413 | #ifdef HAVE_LARGEFILE_SUPPORT |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 11414 | return PyLong_FromLongLong((long long)self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11415 | #else |
| 11416 | return PyLong_FromLong((long)self->d_ino); |
| 11417 | #endif |
| 11418 | #endif |
| 11419 | } |
| 11420 | |
| 11421 | static PyObject * |
| 11422 | DirEntry_repr(DirEntry *self) |
| 11423 | { |
| 11424 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 11425 | } |
| 11426 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 11427 | static PyObject * |
| 11428 | DirEntry_fspath(DirEntry *self) |
| 11429 | { |
| 11430 | Py_INCREF(self->path); |
| 11431 | return self->path; |
| 11432 | } |
| 11433 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11434 | static PyMemberDef DirEntry_members[] = { |
| 11435 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 11436 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 11437 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 11438 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 11439 | {NULL} |
| 11440 | }; |
| 11441 | |
| 11442 | static PyMethodDef DirEntry_methods[] = { |
| 11443 | {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS, |
| 11444 | "return True if the entry is a directory; cached per entry" |
| 11445 | }, |
| 11446 | {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS, |
| 11447 | "return True if the entry is a file; cached per entry" |
| 11448 | }, |
| 11449 | {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS, |
| 11450 | "return True if the entry is a symbolic link; cached per entry" |
| 11451 | }, |
| 11452 | {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS, |
| 11453 | "return stat_result object for the entry; cached per entry" |
| 11454 | }, |
| 11455 | {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS, |
| 11456 | "return inode of the entry; cached per entry", |
| 11457 | }, |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 11458 | {"__fspath__", (PyCFunction)DirEntry_fspath, METH_NOARGS, |
| 11459 | "returns the path for the entry", |
| 11460 | }, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11461 | {NULL} |
| 11462 | }; |
| 11463 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 11464 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11465 | PyVarObject_HEAD_INIT(NULL, 0) |
| 11466 | MODNAME ".DirEntry", /* tp_name */ |
| 11467 | sizeof(DirEntry), /* tp_basicsize */ |
| 11468 | 0, /* tp_itemsize */ |
| 11469 | /* methods */ |
| 11470 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
| 11471 | 0, /* tp_print */ |
| 11472 | 0, /* tp_getattr */ |
| 11473 | 0, /* tp_setattr */ |
| 11474 | 0, /* tp_compare */ |
| 11475 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 11476 | 0, /* tp_as_number */ |
| 11477 | 0, /* tp_as_sequence */ |
| 11478 | 0, /* tp_as_mapping */ |
| 11479 | 0, /* tp_hash */ |
| 11480 | 0, /* tp_call */ |
| 11481 | 0, /* tp_str */ |
| 11482 | 0, /* tp_getattro */ |
| 11483 | 0, /* tp_setattro */ |
| 11484 | 0, /* tp_as_buffer */ |
| 11485 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 11486 | 0, /* tp_doc */ |
| 11487 | 0, /* tp_traverse */ |
| 11488 | 0, /* tp_clear */ |
| 11489 | 0, /* tp_richcompare */ |
| 11490 | 0, /* tp_weaklistoffset */ |
| 11491 | 0, /* tp_iter */ |
| 11492 | 0, /* tp_iternext */ |
| 11493 | DirEntry_methods, /* tp_methods */ |
| 11494 | DirEntry_members, /* tp_members */ |
| 11495 | }; |
| 11496 | |
| 11497 | #ifdef MS_WINDOWS |
| 11498 | |
| 11499 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11500 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11501 | { |
| 11502 | Py_ssize_t path_len; |
| 11503 | Py_ssize_t size; |
| 11504 | wchar_t *result; |
| 11505 | wchar_t ch; |
| 11506 | |
| 11507 | if (!path_wide) { /* Default arg: "." */ |
| 11508 | path_wide = L"."; |
| 11509 | path_len = 1; |
| 11510 | } |
| 11511 | else { |
| 11512 | path_len = wcslen(path_wide); |
| 11513 | } |
| 11514 | |
| 11515 | /* The +1's are for the path separator and the NUL */ |
| 11516 | size = path_len + 1 + wcslen(filename) + 1; |
| 11517 | result = PyMem_New(wchar_t, size); |
| 11518 | if (!result) { |
| 11519 | PyErr_NoMemory(); |
| 11520 | return NULL; |
| 11521 | } |
| 11522 | wcscpy(result, path_wide); |
| 11523 | if (path_len > 0) { |
| 11524 | ch = result[path_len - 1]; |
| 11525 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 11526 | result[path_len++] = SEP; |
| 11527 | wcscpy(result + path_len, filename); |
| 11528 | } |
| 11529 | return result; |
| 11530 | } |
| 11531 | |
| 11532 | static PyObject * |
| 11533 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 11534 | { |
| 11535 | DirEntry *entry; |
| 11536 | BY_HANDLE_FILE_INFORMATION file_info; |
| 11537 | ULONG reparse_tag; |
| 11538 | wchar_t *joined_path; |
| 11539 | |
| 11540 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11541 | if (!entry) |
| 11542 | return NULL; |
| 11543 | entry->name = NULL; |
| 11544 | entry->path = NULL; |
| 11545 | entry->stat = NULL; |
| 11546 | entry->lstat = NULL; |
| 11547 | entry->got_file_index = 0; |
| 11548 | |
| 11549 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 11550 | if (!entry->name) |
| 11551 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11552 | if (path->narrow) { |
| 11553 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 11554 | if (!entry->name) |
| 11555 | goto error; |
| 11556 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11557 | |
| 11558 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 11559 | if (!joined_path) |
| 11560 | goto error; |
| 11561 | |
| 11562 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 11563 | PyMem_Free(joined_path); |
| 11564 | if (!entry->path) |
| 11565 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11566 | if (path->narrow) { |
| 11567 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 11568 | if (!entry->path) |
| 11569 | goto error; |
| 11570 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11571 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11572 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11573 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 11574 | |
| 11575 | return (PyObject *)entry; |
| 11576 | |
| 11577 | error: |
| 11578 | Py_DECREF(entry); |
| 11579 | return NULL; |
| 11580 | } |
| 11581 | |
| 11582 | #else /* POSIX */ |
| 11583 | |
| 11584 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 11585 | 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] | 11586 | { |
| 11587 | Py_ssize_t path_len; |
| 11588 | Py_ssize_t size; |
| 11589 | char *result; |
| 11590 | |
| 11591 | if (!path_narrow) { /* Default arg: "." */ |
| 11592 | path_narrow = "."; |
| 11593 | path_len = 1; |
| 11594 | } |
| 11595 | else { |
| 11596 | path_len = strlen(path_narrow); |
| 11597 | } |
| 11598 | |
| 11599 | if (filename_len == -1) |
| 11600 | filename_len = strlen(filename); |
| 11601 | |
| 11602 | /* The +1's are for the path separator and the NUL */ |
| 11603 | size = path_len + 1 + filename_len + 1; |
| 11604 | result = PyMem_New(char, size); |
| 11605 | if (!result) { |
| 11606 | PyErr_NoMemory(); |
| 11607 | return NULL; |
| 11608 | } |
| 11609 | strcpy(result, path_narrow); |
| 11610 | if (path_len > 0 && result[path_len - 1] != '/') |
| 11611 | result[path_len++] = '/'; |
| 11612 | strcpy(result + path_len, filename); |
| 11613 | return result; |
| 11614 | } |
| 11615 | |
| 11616 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 11617 | 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] | 11618 | ino_t d_ino |
| 11619 | #ifdef HAVE_DIRENT_D_TYPE |
| 11620 | , unsigned char d_type |
| 11621 | #endif |
| 11622 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11623 | { |
| 11624 | DirEntry *entry; |
| 11625 | char *joined_path; |
| 11626 | |
| 11627 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11628 | if (!entry) |
| 11629 | return NULL; |
| 11630 | entry->name = NULL; |
| 11631 | entry->path = NULL; |
| 11632 | entry->stat = NULL; |
| 11633 | entry->lstat = NULL; |
| 11634 | |
| 11635 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 11636 | if (!joined_path) |
| 11637 | goto error; |
| 11638 | |
| 11639 | if (!path->narrow || !PyBytes_Check(path->object)) { |
| 11640 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
| 11641 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
| 11642 | } |
| 11643 | else { |
| 11644 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
| 11645 | entry->path = PyBytes_FromString(joined_path); |
| 11646 | } |
| 11647 | PyMem_Free(joined_path); |
| 11648 | if (!entry->name || !entry->path) |
| 11649 | goto error; |
| 11650 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11651 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11652 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11653 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11654 | entry->d_ino = d_ino; |
| 11655 | |
| 11656 | return (PyObject *)entry; |
| 11657 | |
| 11658 | error: |
| 11659 | Py_XDECREF(entry); |
| 11660 | return NULL; |
| 11661 | } |
| 11662 | |
| 11663 | #endif |
| 11664 | |
| 11665 | |
| 11666 | typedef struct { |
| 11667 | PyObject_HEAD |
| 11668 | path_t path; |
| 11669 | #ifdef MS_WINDOWS |
| 11670 | HANDLE handle; |
| 11671 | WIN32_FIND_DATAW file_data; |
| 11672 | int first_time; |
| 11673 | #else /* POSIX */ |
| 11674 | DIR *dirp; |
| 11675 | #endif |
| 11676 | } ScandirIterator; |
| 11677 | |
| 11678 | #ifdef MS_WINDOWS |
| 11679 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11680 | static int |
| 11681 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 11682 | { |
| 11683 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 11684 | } |
| 11685 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11686 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11687 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11688 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 11689 | HANDLE handle = iterator->handle; |
| 11690 | |
| 11691 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11692 | return; |
| 11693 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11694 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 11695 | Py_BEGIN_ALLOW_THREADS |
| 11696 | FindClose(handle); |
| 11697 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11698 | } |
| 11699 | |
| 11700 | static PyObject * |
| 11701 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 11702 | { |
| 11703 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 11704 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11705 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11706 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11707 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11708 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11709 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11710 | |
| 11711 | while (1) { |
| 11712 | if (!iterator->first_time) { |
| 11713 | Py_BEGIN_ALLOW_THREADS |
| 11714 | success = FindNextFileW(iterator->handle, file_data); |
| 11715 | Py_END_ALLOW_THREADS |
| 11716 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11717 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11718 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11719 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11720 | break; |
| 11721 | } |
| 11722 | } |
| 11723 | iterator->first_time = 0; |
| 11724 | |
| 11725 | /* Skip over . and .. */ |
| 11726 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11727 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 11728 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 11729 | if (!entry) |
| 11730 | break; |
| 11731 | return entry; |
| 11732 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11733 | |
| 11734 | /* Loop till we get a non-dot directory or finish iterating */ |
| 11735 | } |
| 11736 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11737 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11738 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11739 | return NULL; |
| 11740 | } |
| 11741 | |
| 11742 | #else /* POSIX */ |
| 11743 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11744 | static int |
| 11745 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 11746 | { |
| 11747 | return !iterator->dirp; |
| 11748 | } |
| 11749 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11750 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11751 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11752 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 11753 | DIR *dirp = iterator->dirp; |
| 11754 | |
| 11755 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11756 | return; |
| 11757 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11758 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 11759 | Py_BEGIN_ALLOW_THREADS |
| 11760 | closedir(dirp); |
| 11761 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11762 | return; |
| 11763 | } |
| 11764 | |
| 11765 | static PyObject * |
| 11766 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 11767 | { |
| 11768 | struct dirent *direntp; |
| 11769 | Py_ssize_t name_len; |
| 11770 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11771 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11772 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11773 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11774 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11775 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11776 | |
| 11777 | while (1) { |
| 11778 | errno = 0; |
| 11779 | Py_BEGIN_ALLOW_THREADS |
| 11780 | direntp = readdir(iterator->dirp); |
| 11781 | Py_END_ALLOW_THREADS |
| 11782 | |
| 11783 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11784 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11785 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11786 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11787 | break; |
| 11788 | } |
| 11789 | |
| 11790 | /* Skip over . and .. */ |
| 11791 | name_len = NAMLEN(direntp); |
| 11792 | is_dot = direntp->d_name[0] == '.' && |
| 11793 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 11794 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11795 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11796 | name_len, direntp->d_ino |
| 11797 | #ifdef HAVE_DIRENT_D_TYPE |
| 11798 | , direntp->d_type |
| 11799 | #endif |
| 11800 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11801 | if (!entry) |
| 11802 | break; |
| 11803 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11804 | } |
| 11805 | |
| 11806 | /* Loop till we get a non-dot directory or finish iterating */ |
| 11807 | } |
| 11808 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11809 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11810 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11811 | return NULL; |
| 11812 | } |
| 11813 | |
| 11814 | #endif |
| 11815 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11816 | static PyObject * |
| 11817 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 11818 | { |
| 11819 | ScandirIterator_closedir(self); |
| 11820 | Py_RETURN_NONE; |
| 11821 | } |
| 11822 | |
| 11823 | static PyObject * |
| 11824 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 11825 | { |
| 11826 | Py_INCREF(self); |
| 11827 | return self; |
| 11828 | } |
| 11829 | |
| 11830 | static PyObject * |
| 11831 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 11832 | { |
| 11833 | ScandirIterator_closedir(self); |
| 11834 | Py_RETURN_NONE; |
| 11835 | } |
| 11836 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11837 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 11838 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 11839 | { |
| 11840 | PyObject *error_type, *error_value, *error_traceback; |
| 11841 | |
| 11842 | /* Save the current exception, if any. */ |
| 11843 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 11844 | |
| 11845 | if (!ScandirIterator_is_closed(iterator)) { |
| 11846 | ScandirIterator_closedir(iterator); |
| 11847 | |
| 11848 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 11849 | "unclosed scandir iterator %R", iterator)) { |
| 11850 | /* Spurious errors can appear at shutdown */ |
| 11851 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 11852 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 11853 | } |
| 11854 | } |
| 11855 | } |
| 11856 | |
| 11857 | Py_CLEAR(iterator->path.object); |
| 11858 | path_cleanup(&iterator->path); |
| 11859 | |
| 11860 | /* Restore the saved exception. */ |
| 11861 | PyErr_Restore(error_type, error_value, error_traceback); |
| 11862 | } |
| 11863 | |
| 11864 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11865 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 11866 | { |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 11867 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 11868 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11869 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11870 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 11871 | } |
| 11872 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11873 | static PyMethodDef ScandirIterator_methods[] = { |
| 11874 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 11875 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 11876 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 11877 | {NULL} |
| 11878 | }; |
| 11879 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 11880 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11881 | PyVarObject_HEAD_INIT(NULL, 0) |
| 11882 | MODNAME ".ScandirIterator", /* tp_name */ |
| 11883 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 11884 | 0, /* tp_itemsize */ |
| 11885 | /* methods */ |
| 11886 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
| 11887 | 0, /* tp_print */ |
| 11888 | 0, /* tp_getattr */ |
| 11889 | 0, /* tp_setattr */ |
| 11890 | 0, /* tp_compare */ |
| 11891 | 0, /* tp_repr */ |
| 11892 | 0, /* tp_as_number */ |
| 11893 | 0, /* tp_as_sequence */ |
| 11894 | 0, /* tp_as_mapping */ |
| 11895 | 0, /* tp_hash */ |
| 11896 | 0, /* tp_call */ |
| 11897 | 0, /* tp_str */ |
| 11898 | 0, /* tp_getattro */ |
| 11899 | 0, /* tp_setattro */ |
| 11900 | 0, /* tp_as_buffer */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 11901 | Py_TPFLAGS_DEFAULT |
| 11902 | | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11903 | 0, /* tp_doc */ |
| 11904 | 0, /* tp_traverse */ |
| 11905 | 0, /* tp_clear */ |
| 11906 | 0, /* tp_richcompare */ |
| 11907 | 0, /* tp_weaklistoffset */ |
| 11908 | PyObject_SelfIter, /* tp_iter */ |
| 11909 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11910 | ScandirIterator_methods, /* tp_methods */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 11911 | 0, /* tp_members */ |
| 11912 | 0, /* tp_getset */ |
| 11913 | 0, /* tp_base */ |
| 11914 | 0, /* tp_dict */ |
| 11915 | 0, /* tp_descr_get */ |
| 11916 | 0, /* tp_descr_set */ |
| 11917 | 0, /* tp_dictoffset */ |
| 11918 | 0, /* tp_init */ |
| 11919 | 0, /* tp_alloc */ |
| 11920 | 0, /* tp_new */ |
| 11921 | 0, /* tp_free */ |
| 11922 | 0, /* tp_is_gc */ |
| 11923 | 0, /* tp_bases */ |
| 11924 | 0, /* tp_mro */ |
| 11925 | 0, /* tp_cache */ |
| 11926 | 0, /* tp_subclasses */ |
| 11927 | 0, /* tp_weaklist */ |
| 11928 | 0, /* tp_del */ |
| 11929 | 0, /* tp_version_tag */ |
| 11930 | (destructor)ScandirIterator_finalize, /* tp_finalize */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11931 | }; |
| 11932 | |
| 11933 | static PyObject * |
| 11934 | posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 11935 | { |
| 11936 | ScandirIterator *iterator; |
| 11937 | static char *keywords[] = {"path", NULL}; |
| 11938 | #ifdef MS_WINDOWS |
| 11939 | wchar_t *path_strW; |
| 11940 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11941 | const char *path; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11942 | #endif |
| 11943 | |
| 11944 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 11945 | if (!iterator) |
| 11946 | return NULL; |
| 11947 | memset(&iterator->path, 0, sizeof(path_t)); |
| 11948 | iterator->path.function_name = "scandir"; |
| 11949 | iterator->path.nullable = 1; |
| 11950 | |
| 11951 | #ifdef MS_WINDOWS |
| 11952 | iterator->handle = INVALID_HANDLE_VALUE; |
| 11953 | #else |
| 11954 | iterator->dirp = NULL; |
| 11955 | #endif |
| 11956 | |
| 11957 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords, |
| 11958 | path_converter, &iterator->path)) |
| 11959 | goto error; |
| 11960 | |
| 11961 | /* path_converter doesn't keep path.object around, so do it |
| 11962 | manually for the lifetime of the iterator here (the refcount |
| 11963 | is decremented in ScandirIterator_dealloc) |
| 11964 | */ |
| 11965 | Py_XINCREF(iterator->path.object); |
| 11966 | |
| 11967 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11968 | iterator->first_time = 1; |
| 11969 | |
| 11970 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 11971 | if (!path_strW) |
| 11972 | goto error; |
| 11973 | |
| 11974 | Py_BEGIN_ALLOW_THREADS |
| 11975 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 11976 | Py_END_ALLOW_THREADS |
| 11977 | |
| 11978 | PyMem_Free(path_strW); |
| 11979 | |
| 11980 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 11981 | path_error(&iterator->path); |
| 11982 | goto error; |
| 11983 | } |
| 11984 | #else /* POSIX */ |
| 11985 | if (iterator->path.narrow) |
| 11986 | path = iterator->path.narrow; |
| 11987 | else |
| 11988 | path = "."; |
| 11989 | |
| 11990 | errno = 0; |
| 11991 | Py_BEGIN_ALLOW_THREADS |
| 11992 | iterator->dirp = opendir(path); |
| 11993 | Py_END_ALLOW_THREADS |
| 11994 | |
| 11995 | if (!iterator->dirp) { |
| 11996 | path_error(&iterator->path); |
| 11997 | goto error; |
| 11998 | } |
| 11999 | #endif |
| 12000 | |
| 12001 | return (PyObject *)iterator; |
| 12002 | |
| 12003 | error: |
| 12004 | Py_DECREF(iterator); |
| 12005 | return NULL; |
| 12006 | } |
| 12007 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12008 | /* |
| 12009 | Return the file system path representation of the object. |
| 12010 | |
| 12011 | If the object is str or bytes, then allow it to pass through with |
| 12012 | an incremented refcount. If the object defines __fspath__(), then |
| 12013 | return the result of that method. All other types raise a TypeError. |
| 12014 | */ |
| 12015 | PyObject * |
| 12016 | PyOS_FSPath(PyObject *path) |
| 12017 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 12018 | /* For error message reasons, this function is manually inlined in |
| 12019 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12020 | _Py_IDENTIFIER(__fspath__); |
| 12021 | PyObject *func = NULL; |
| 12022 | PyObject *path_repr = NULL; |
| 12023 | |
| 12024 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 12025 | Py_INCREF(path); |
| 12026 | return path; |
| 12027 | } |
| 12028 | |
| 12029 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 12030 | if (NULL == func) { |
| 12031 | return PyErr_Format(PyExc_TypeError, |
| 12032 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 12033 | "not %.200s", |
| 12034 | Py_TYPE(path)->tp_name); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12035 | } |
| 12036 | |
| 12037 | path_repr = PyObject_CallFunctionObjArgs(func, NULL); |
| 12038 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 12039 | if (NULL == path_repr) { |
| 12040 | return NULL; |
| 12041 | } |
| 12042 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 12043 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 12044 | PyErr_Format(PyExc_TypeError, |
| 12045 | "expected %.200s.__fspath__() to return str or bytes, " |
| 12046 | "not %.200s", Py_TYPE(path)->tp_name, |
| 12047 | Py_TYPE(path_repr)->tp_name); |
| 12048 | Py_DECREF(path_repr); |
| 12049 | return NULL; |
| 12050 | } |
| 12051 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12052 | return path_repr; |
| 12053 | } |
| 12054 | |
| 12055 | /*[clinic input] |
| 12056 | os.fspath |
| 12057 | |
| 12058 | path: object |
| 12059 | |
| 12060 | Return the file system path representation of the object. |
| 12061 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 12062 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 12063 | object defines __fspath__(), then return the result of that method. All other |
| 12064 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12065 | [clinic start generated code]*/ |
| 12066 | |
| 12067 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12068 | os_fspath_impl(PyObject *module, PyObject *path) |
| 12069 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12070 | { |
| 12071 | return PyOS_FSPath(path); |
| 12072 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12073 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12074 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 12075 | /*[clinic input] |
| 12076 | os.getrandom |
| 12077 | |
| 12078 | size: Py_ssize_t |
| 12079 | flags: int=0 |
| 12080 | |
| 12081 | Obtain a series of random bytes. |
| 12082 | [clinic start generated code]*/ |
| 12083 | |
| 12084 | static PyObject * |
| 12085 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 12086 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 12087 | { |
| 12088 | char *buffer; |
| 12089 | Py_ssize_t n; |
| 12090 | PyObject *bytes; |
| 12091 | |
| 12092 | if (size < 0) { |
| 12093 | errno = EINVAL; |
| 12094 | return posix_error(); |
| 12095 | } |
| 12096 | |
| 12097 | buffer = PyMem_Malloc(size); |
| 12098 | if (buffer == NULL) { |
| 12099 | PyErr_NoMemory(); |
| 12100 | return NULL; |
| 12101 | } |
| 12102 | |
| 12103 | while (1) { |
| 12104 | n = syscall(SYS_getrandom, buffer, size, flags); |
| 12105 | if (n < 0 && errno == EINTR) { |
| 12106 | if (PyErr_CheckSignals() < 0) { |
| 12107 | return NULL; |
| 12108 | } |
| 12109 | continue; |
| 12110 | } |
| 12111 | break; |
| 12112 | } |
| 12113 | |
| 12114 | if (n < 0) { |
| 12115 | PyMem_Free(buffer); |
| 12116 | PyErr_SetFromErrno(PyExc_OSError); |
| 12117 | return NULL; |
| 12118 | } |
| 12119 | |
| 12120 | bytes = PyBytes_FromStringAndSize(buffer, n); |
| 12121 | PyMem_Free(buffer); |
| 12122 | |
| 12123 | return bytes; |
| 12124 | } |
| 12125 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 12126 | |
Serhiy Storchaka | a4c6bad | 2015-04-04 23:35:52 +0300 | [diff] [blame] | 12127 | #include "clinic/posixmodule.c.h" |
| 12128 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12129 | /*[clinic input] |
| 12130 | dump buffer |
| 12131 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 12132 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12133 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12134 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12135 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12136 | |
| 12137 | OS_STAT_METHODDEF |
| 12138 | OS_ACCESS_METHODDEF |
| 12139 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12140 | OS_CHDIR_METHODDEF |
| 12141 | OS_CHFLAGS_METHODDEF |
| 12142 | OS_CHMOD_METHODDEF |
| 12143 | OS_FCHMOD_METHODDEF |
| 12144 | OS_LCHMOD_METHODDEF |
| 12145 | OS_CHOWN_METHODDEF |
| 12146 | OS_FCHOWN_METHODDEF |
| 12147 | OS_LCHOWN_METHODDEF |
| 12148 | OS_LCHFLAGS_METHODDEF |
| 12149 | OS_CHROOT_METHODDEF |
| 12150 | OS_CTERMID_METHODDEF |
| 12151 | OS_GETCWD_METHODDEF |
| 12152 | OS_GETCWDB_METHODDEF |
| 12153 | OS_LINK_METHODDEF |
| 12154 | OS_LISTDIR_METHODDEF |
| 12155 | OS_LSTAT_METHODDEF |
| 12156 | OS_MKDIR_METHODDEF |
| 12157 | OS_NICE_METHODDEF |
| 12158 | OS_GETPRIORITY_METHODDEF |
| 12159 | OS_SETPRIORITY_METHODDEF |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12160 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12161 | {"readlink", (PyCFunction)posix_readlink, |
| 12162 | METH_VARARGS | METH_KEYWORDS, |
| 12163 | readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 12164 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12165 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12166 | {"readlink", (PyCFunction)win_readlink, |
| 12167 | METH_VARARGS | METH_KEYWORDS, |
| 12168 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12169 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12170 | OS_RENAME_METHODDEF |
| 12171 | OS_REPLACE_METHODDEF |
| 12172 | OS_RMDIR_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12173 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12174 | OS_SYMLINK_METHODDEF |
| 12175 | OS_SYSTEM_METHODDEF |
| 12176 | OS_UMASK_METHODDEF |
| 12177 | OS_UNAME_METHODDEF |
| 12178 | OS_UNLINK_METHODDEF |
| 12179 | OS_REMOVE_METHODDEF |
| 12180 | OS_UTIME_METHODDEF |
| 12181 | OS_TIMES_METHODDEF |
| 12182 | OS__EXIT_METHODDEF |
| 12183 | OS_EXECV_METHODDEF |
| 12184 | OS_EXECVE_METHODDEF |
| 12185 | OS_SPAWNV_METHODDEF |
| 12186 | OS_SPAWNVE_METHODDEF |
| 12187 | OS_FORK1_METHODDEF |
| 12188 | OS_FORK_METHODDEF |
| 12189 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 12190 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 12191 | OS_SCHED_GETPARAM_METHODDEF |
| 12192 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 12193 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 12194 | OS_SCHED_SETPARAM_METHODDEF |
| 12195 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 12196 | OS_SCHED_YIELD_METHODDEF |
| 12197 | OS_SCHED_SETAFFINITY_METHODDEF |
| 12198 | OS_SCHED_GETAFFINITY_METHODDEF |
| 12199 | OS_OPENPTY_METHODDEF |
| 12200 | OS_FORKPTY_METHODDEF |
| 12201 | OS_GETEGID_METHODDEF |
| 12202 | OS_GETEUID_METHODDEF |
| 12203 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 12204 | #ifdef HAVE_GETGROUPLIST |
| 12205 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 12206 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12207 | OS_GETGROUPS_METHODDEF |
| 12208 | OS_GETPID_METHODDEF |
| 12209 | OS_GETPGRP_METHODDEF |
| 12210 | OS_GETPPID_METHODDEF |
| 12211 | OS_GETUID_METHODDEF |
| 12212 | OS_GETLOGIN_METHODDEF |
| 12213 | OS_KILL_METHODDEF |
| 12214 | OS_KILLPG_METHODDEF |
| 12215 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12216 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12217 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12218 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12219 | OS_SETUID_METHODDEF |
| 12220 | OS_SETEUID_METHODDEF |
| 12221 | OS_SETREUID_METHODDEF |
| 12222 | OS_SETGID_METHODDEF |
| 12223 | OS_SETEGID_METHODDEF |
| 12224 | OS_SETREGID_METHODDEF |
| 12225 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12226 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12227 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12228 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12229 | OS_GETPGID_METHODDEF |
| 12230 | OS_SETPGRP_METHODDEF |
| 12231 | OS_WAIT_METHODDEF |
| 12232 | OS_WAIT3_METHODDEF |
| 12233 | OS_WAIT4_METHODDEF |
| 12234 | OS_WAITID_METHODDEF |
| 12235 | OS_WAITPID_METHODDEF |
| 12236 | OS_GETSID_METHODDEF |
| 12237 | OS_SETSID_METHODDEF |
| 12238 | OS_SETPGID_METHODDEF |
| 12239 | OS_TCGETPGRP_METHODDEF |
| 12240 | OS_TCSETPGRP_METHODDEF |
| 12241 | OS_OPEN_METHODDEF |
| 12242 | OS_CLOSE_METHODDEF |
| 12243 | OS_CLOSERANGE_METHODDEF |
| 12244 | OS_DEVICE_ENCODING_METHODDEF |
| 12245 | OS_DUP_METHODDEF |
| 12246 | OS_DUP2_METHODDEF |
| 12247 | OS_LOCKF_METHODDEF |
| 12248 | OS_LSEEK_METHODDEF |
| 12249 | OS_READ_METHODDEF |
| 12250 | OS_READV_METHODDEF |
| 12251 | OS_PREAD_METHODDEF |
| 12252 | OS_WRITE_METHODDEF |
| 12253 | OS_WRITEV_METHODDEF |
| 12254 | OS_PWRITE_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12255 | #ifdef HAVE_SENDFILE |
| 12256 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 12257 | posix_sendfile__doc__}, |
| 12258 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12259 | OS_FSTAT_METHODDEF |
| 12260 | OS_ISATTY_METHODDEF |
| 12261 | OS_PIPE_METHODDEF |
| 12262 | OS_PIPE2_METHODDEF |
| 12263 | OS_MKFIFO_METHODDEF |
| 12264 | OS_MKNOD_METHODDEF |
| 12265 | OS_MAJOR_METHODDEF |
| 12266 | OS_MINOR_METHODDEF |
| 12267 | OS_MAKEDEV_METHODDEF |
| 12268 | OS_FTRUNCATE_METHODDEF |
| 12269 | OS_TRUNCATE_METHODDEF |
| 12270 | OS_POSIX_FALLOCATE_METHODDEF |
| 12271 | OS_POSIX_FADVISE_METHODDEF |
| 12272 | OS_PUTENV_METHODDEF |
| 12273 | OS_UNSETENV_METHODDEF |
| 12274 | OS_STRERROR_METHODDEF |
| 12275 | OS_FCHDIR_METHODDEF |
| 12276 | OS_FSYNC_METHODDEF |
| 12277 | OS_SYNC_METHODDEF |
| 12278 | OS_FDATASYNC_METHODDEF |
| 12279 | OS_WCOREDUMP_METHODDEF |
| 12280 | OS_WIFCONTINUED_METHODDEF |
| 12281 | OS_WIFSTOPPED_METHODDEF |
| 12282 | OS_WIFSIGNALED_METHODDEF |
| 12283 | OS_WIFEXITED_METHODDEF |
| 12284 | OS_WEXITSTATUS_METHODDEF |
| 12285 | OS_WTERMSIG_METHODDEF |
| 12286 | OS_WSTOPSIG_METHODDEF |
| 12287 | OS_FSTATVFS_METHODDEF |
| 12288 | OS_STATVFS_METHODDEF |
| 12289 | OS_CONFSTR_METHODDEF |
| 12290 | OS_SYSCONF_METHODDEF |
| 12291 | OS_FPATHCONF_METHODDEF |
| 12292 | OS_PATHCONF_METHODDEF |
| 12293 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 12294 | OS__GETFULLPATHNAME_METHODDEF |
| 12295 | OS__ISDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12296 | OS__GETDISKUSAGE_METHODDEF |
| 12297 | OS__GETFINALPATHNAME_METHODDEF |
| 12298 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 12299 | OS_GETLOADAVG_METHODDEF |
| 12300 | OS_URANDOM_METHODDEF |
| 12301 | OS_SETRESUID_METHODDEF |
| 12302 | OS_SETRESGID_METHODDEF |
| 12303 | OS_GETRESUID_METHODDEF |
| 12304 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12305 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12306 | OS_GETXATTR_METHODDEF |
| 12307 | OS_SETXATTR_METHODDEF |
| 12308 | OS_REMOVEXATTR_METHODDEF |
| 12309 | OS_LISTXATTR_METHODDEF |
| 12310 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12311 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 12312 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 12313 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12314 | OS_CPU_COUNT_METHODDEF |
| 12315 | OS_GET_INHERITABLE_METHODDEF |
| 12316 | OS_SET_INHERITABLE_METHODDEF |
| 12317 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 12318 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12319 | #ifndef MS_WINDOWS |
| 12320 | {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__}, |
| 12321 | {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__}, |
| 12322 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12323 | {"scandir", (PyCFunction)posix_scandir, |
| 12324 | METH_VARARGS | METH_KEYWORDS, |
| 12325 | posix_scandir__doc__}, |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12326 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12327 | OS_GETRANDOM_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12328 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12329 | }; |
| 12330 | |
| 12331 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12332 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12333 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12334 | enable_symlink() |
| 12335 | { |
| 12336 | HANDLE tok; |
| 12337 | TOKEN_PRIVILEGES tok_priv; |
| 12338 | LUID luid; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12339 | |
| 12340 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12341 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12342 | |
| 12343 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12344 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12345 | |
| 12346 | tok_priv.PrivilegeCount = 1; |
| 12347 | tok_priv.Privileges[0].Luid = luid; |
| 12348 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 12349 | |
| 12350 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 12351 | sizeof(TOKEN_PRIVILEGES), |
| 12352 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12353 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12354 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12355 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 12356 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12357 | } |
| 12358 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 12359 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12360 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12361 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12362 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12363 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12364 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12365 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12366 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12367 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12368 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12369 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12370 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12371 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12372 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12373 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12374 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12375 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12376 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12377 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12378 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12379 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12380 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12381 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12382 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12383 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12384 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12385 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12386 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12387 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12388 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12389 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12390 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12391 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12392 | #endif |
| 12393 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12394 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12395 | #endif |
| 12396 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12397 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12398 | #endif |
| 12399 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12400 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12401 | #endif |
| 12402 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12403 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12404 | #endif |
| 12405 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12406 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12407 | #endif |
| 12408 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12409 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12410 | #endif |
| 12411 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12412 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12413 | #endif |
| 12414 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12415 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12416 | #endif |
| 12417 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12418 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12419 | #endif |
| 12420 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12421 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12422 | #endif |
| 12423 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12424 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12425 | #endif |
| 12426 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12427 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12428 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12429 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12430 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12431 | #endif |
| 12432 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12433 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12434 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12435 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12436 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12437 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12438 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12439 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12440 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 12441 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12442 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12443 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12444 | #endif |
| 12445 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12446 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12447 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 12448 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12449 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12450 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12451 | #endif |
| 12452 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12453 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12454 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12455 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12456 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12457 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12458 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12459 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12460 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 12461 | #ifdef O_TMPFILE |
| 12462 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 12463 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12464 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12465 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12466 | #endif |
| 12467 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12468 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12469 | #endif |
| 12470 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12471 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12472 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12473 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12474 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12475 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12476 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12477 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12478 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12479 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12480 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12481 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12482 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12483 | #endif |
| 12484 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12485 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12486 | #endif |
| 12487 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12488 | /* MS Windows */ |
| 12489 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12490 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12491 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12492 | #endif |
| 12493 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12494 | /* Optimize for short life (keep in memory). */ |
| 12495 | /* 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] | 12496 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12497 | #endif |
| 12498 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12499 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12500 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12501 | #endif |
| 12502 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12503 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12504 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12505 | #endif |
| 12506 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12507 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12508 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12509 | #endif |
| 12510 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12511 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12512 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12513 | /* Send a SIGIO signal whenever input or output |
| 12514 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12515 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12516 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12517 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12518 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12519 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12520 | #endif |
| 12521 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12522 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12523 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12524 | #endif |
| 12525 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12526 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12527 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12528 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12529 | #ifdef O_NOLINKS |
| 12530 | /* 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] | 12531 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12532 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12533 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12534 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12535 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12536 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 12537 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12538 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12539 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12540 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12541 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12542 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12543 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12544 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12545 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12546 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12547 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12548 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12549 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12550 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12551 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12552 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12553 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12554 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12555 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12556 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12557 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12558 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12559 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12560 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12561 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12562 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12563 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12564 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12565 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12566 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12567 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12568 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12569 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12570 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12571 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12572 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12573 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12574 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12575 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12576 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12577 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12578 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12579 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12580 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12581 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12582 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12583 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12584 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12585 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12586 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12587 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12588 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12589 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12590 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 12591 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12592 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12593 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12594 | #endif /* ST_RDONLY */ |
| 12595 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12596 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12597 | #endif /* ST_NOSUID */ |
| 12598 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 12599 | /* GNU extensions */ |
| 12600 | #ifdef ST_NODEV |
| 12601 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 12602 | #endif /* ST_NODEV */ |
| 12603 | #ifdef ST_NOEXEC |
| 12604 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 12605 | #endif /* ST_NOEXEC */ |
| 12606 | #ifdef ST_SYNCHRONOUS |
| 12607 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 12608 | #endif /* ST_SYNCHRONOUS */ |
| 12609 | #ifdef ST_MANDLOCK |
| 12610 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 12611 | #endif /* ST_MANDLOCK */ |
| 12612 | #ifdef ST_WRITE |
| 12613 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 12614 | #endif /* ST_WRITE */ |
| 12615 | #ifdef ST_APPEND |
| 12616 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 12617 | #endif /* ST_APPEND */ |
| 12618 | #ifdef ST_NOATIME |
| 12619 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 12620 | #endif /* ST_NOATIME */ |
| 12621 | #ifdef ST_NODIRATIME |
| 12622 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 12623 | #endif /* ST_NODIRATIME */ |
| 12624 | #ifdef ST_RELATIME |
| 12625 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 12626 | #endif /* ST_RELATIME */ |
| 12627 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12628 | /* FreeBSD sendfile() constants */ |
| 12629 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12630 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12631 | #endif |
| 12632 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12633 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12634 | #endif |
| 12635 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12636 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12637 | #endif |
| 12638 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12639 | /* constants for posix_fadvise */ |
| 12640 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12641 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12642 | #endif |
| 12643 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12644 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12645 | #endif |
| 12646 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12647 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12648 | #endif |
| 12649 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12650 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12651 | #endif |
| 12652 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12653 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12654 | #endif |
| 12655 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12656 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12657 | #endif |
| 12658 | |
| 12659 | /* constants for waitid */ |
| 12660 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12661 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 12662 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 12663 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12664 | #endif |
| 12665 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12666 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12667 | #endif |
| 12668 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12669 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12670 | #endif |
| 12671 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12672 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12673 | #endif |
| 12674 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12675 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12676 | #endif |
| 12677 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12678 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12679 | #endif |
| 12680 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12681 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12682 | #endif |
| 12683 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12684 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12685 | #endif |
| 12686 | |
| 12687 | /* constants for lockf */ |
| 12688 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12689 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12690 | #endif |
| 12691 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12692 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12693 | #endif |
| 12694 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12695 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12696 | #endif |
| 12697 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12698 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12699 | #endif |
| 12700 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12701 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12702 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 12703 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 12704 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 12705 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 12706 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12707 | #endif |
| 12708 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12709 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 12710 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12711 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 12712 | #endif |
| 12713 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12714 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 12715 | #endif |
| 12716 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12717 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 12718 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12719 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12720 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12721 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12722 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12723 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12724 | #endif |
| 12725 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12726 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12727 | #endif |
| 12728 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12729 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12730 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12731 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12732 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12733 | #endif |
| 12734 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12735 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12736 | #endif |
| 12737 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12738 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12739 | #endif |
| 12740 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12741 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12742 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12743 | #endif |
| 12744 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12745 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12746 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 12747 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 12748 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12749 | #endif |
| 12750 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12751 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12752 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12753 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12754 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12755 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12756 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12757 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12758 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12759 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12760 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12761 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12762 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12763 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12764 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12765 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12766 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12767 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12768 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12769 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12770 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12771 | #endif |
| 12772 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12773 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 12774 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 12775 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 12776 | #endif |
| 12777 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12778 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12779 | } |
| 12780 | |
| 12781 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12782 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12783 | PyModuleDef_HEAD_INIT, |
| 12784 | MODNAME, |
| 12785 | posix__doc__, |
| 12786 | -1, |
| 12787 | posix_methods, |
| 12788 | NULL, |
| 12789 | NULL, |
| 12790 | NULL, |
| 12791 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12792 | }; |
| 12793 | |
| 12794 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12795 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12796 | |
| 12797 | #ifdef HAVE_FACCESSAT |
| 12798 | "HAVE_FACCESSAT", |
| 12799 | #endif |
| 12800 | |
| 12801 | #ifdef HAVE_FCHDIR |
| 12802 | "HAVE_FCHDIR", |
| 12803 | #endif |
| 12804 | |
| 12805 | #ifdef HAVE_FCHMOD |
| 12806 | "HAVE_FCHMOD", |
| 12807 | #endif |
| 12808 | |
| 12809 | #ifdef HAVE_FCHMODAT |
| 12810 | "HAVE_FCHMODAT", |
| 12811 | #endif |
| 12812 | |
| 12813 | #ifdef HAVE_FCHOWN |
| 12814 | "HAVE_FCHOWN", |
| 12815 | #endif |
| 12816 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 12817 | #ifdef HAVE_FCHOWNAT |
| 12818 | "HAVE_FCHOWNAT", |
| 12819 | #endif |
| 12820 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12821 | #ifdef HAVE_FEXECVE |
| 12822 | "HAVE_FEXECVE", |
| 12823 | #endif |
| 12824 | |
| 12825 | #ifdef HAVE_FDOPENDIR |
| 12826 | "HAVE_FDOPENDIR", |
| 12827 | #endif |
| 12828 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12829 | #ifdef HAVE_FPATHCONF |
| 12830 | "HAVE_FPATHCONF", |
| 12831 | #endif |
| 12832 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12833 | #ifdef HAVE_FSTATAT |
| 12834 | "HAVE_FSTATAT", |
| 12835 | #endif |
| 12836 | |
| 12837 | #ifdef HAVE_FSTATVFS |
| 12838 | "HAVE_FSTATVFS", |
| 12839 | #endif |
| 12840 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 12841 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12842 | "HAVE_FTRUNCATE", |
| 12843 | #endif |
| 12844 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12845 | #ifdef HAVE_FUTIMENS |
| 12846 | "HAVE_FUTIMENS", |
| 12847 | #endif |
| 12848 | |
| 12849 | #ifdef HAVE_FUTIMES |
| 12850 | "HAVE_FUTIMES", |
| 12851 | #endif |
| 12852 | |
| 12853 | #ifdef HAVE_FUTIMESAT |
| 12854 | "HAVE_FUTIMESAT", |
| 12855 | #endif |
| 12856 | |
| 12857 | #ifdef HAVE_LINKAT |
| 12858 | "HAVE_LINKAT", |
| 12859 | #endif |
| 12860 | |
| 12861 | #ifdef HAVE_LCHFLAGS |
| 12862 | "HAVE_LCHFLAGS", |
| 12863 | #endif |
| 12864 | |
| 12865 | #ifdef HAVE_LCHMOD |
| 12866 | "HAVE_LCHMOD", |
| 12867 | #endif |
| 12868 | |
| 12869 | #ifdef HAVE_LCHOWN |
| 12870 | "HAVE_LCHOWN", |
| 12871 | #endif |
| 12872 | |
| 12873 | #ifdef HAVE_LSTAT |
| 12874 | "HAVE_LSTAT", |
| 12875 | #endif |
| 12876 | |
| 12877 | #ifdef HAVE_LUTIMES |
| 12878 | "HAVE_LUTIMES", |
| 12879 | #endif |
| 12880 | |
| 12881 | #ifdef HAVE_MKDIRAT |
| 12882 | "HAVE_MKDIRAT", |
| 12883 | #endif |
| 12884 | |
| 12885 | #ifdef HAVE_MKFIFOAT |
| 12886 | "HAVE_MKFIFOAT", |
| 12887 | #endif |
| 12888 | |
| 12889 | #ifdef HAVE_MKNODAT |
| 12890 | "HAVE_MKNODAT", |
| 12891 | #endif |
| 12892 | |
| 12893 | #ifdef HAVE_OPENAT |
| 12894 | "HAVE_OPENAT", |
| 12895 | #endif |
| 12896 | |
| 12897 | #ifdef HAVE_READLINKAT |
| 12898 | "HAVE_READLINKAT", |
| 12899 | #endif |
| 12900 | |
| 12901 | #ifdef HAVE_RENAMEAT |
| 12902 | "HAVE_RENAMEAT", |
| 12903 | #endif |
| 12904 | |
| 12905 | #ifdef HAVE_SYMLINKAT |
| 12906 | "HAVE_SYMLINKAT", |
| 12907 | #endif |
| 12908 | |
| 12909 | #ifdef HAVE_UNLINKAT |
| 12910 | "HAVE_UNLINKAT", |
| 12911 | #endif |
| 12912 | |
| 12913 | #ifdef HAVE_UTIMENSAT |
| 12914 | "HAVE_UTIMENSAT", |
| 12915 | #endif |
| 12916 | |
| 12917 | #ifdef MS_WINDOWS |
| 12918 | "MS_WINDOWS", |
| 12919 | #endif |
| 12920 | |
| 12921 | NULL |
| 12922 | }; |
| 12923 | |
| 12924 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 12925 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 12926 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12927 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12928 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12929 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12930 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12931 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12932 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12933 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12934 | #endif |
| 12935 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12936 | m = PyModule_Create(&posixmodule); |
| 12937 | if (m == NULL) |
| 12938 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12939 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12940 | /* Initialize environ dictionary */ |
| 12941 | v = convertenviron(); |
| 12942 | Py_XINCREF(v); |
| 12943 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 12944 | return NULL; |
| 12945 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12946 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12947 | if (all_ins(m)) |
| 12948 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12949 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12950 | if (setup_confname_tables(m)) |
| 12951 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12952 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12953 | Py_INCREF(PyExc_OSError); |
| 12954 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 12955 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12956 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12957 | if (posix_putenv_garbage == NULL) |
| 12958 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12959 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 12960 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12961 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12962 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12963 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12964 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 12965 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12966 | #endif |
| 12967 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12968 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12969 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 12970 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 12971 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12972 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 12973 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12974 | structseq_new = StatResultType.tp_new; |
| 12975 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12976 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12977 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12978 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 12979 | &statvfs_result_desc) < 0) |
| 12980 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12981 | #ifdef NEED_TICKS_PER_SECOND |
| 12982 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12983 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12984 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12985 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12986 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12987 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12988 | # endif |
| 12989 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12990 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 12991 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12992 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12993 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 12994 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12995 | SchedParamType.tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12996 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12997 | |
| 12998 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12999 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 13000 | &TerminalSize_desc) < 0) |
| 13001 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13002 | |
| 13003 | /* initialize scandir types */ |
| 13004 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 13005 | return NULL; |
| 13006 | if (PyType_Ready(&DirEntryType) < 0) |
| 13007 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13008 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13009 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 13010 | Py_INCREF((PyObject*) &WaitidResultType); |
| 13011 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 13012 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13013 | Py_INCREF((PyObject*) &StatResultType); |
| 13014 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 13015 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 13016 | PyModule_AddObject(m, "statvfs_result", |
| 13017 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 13018 | |
| 13019 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13020 | Py_INCREF(&SchedParamType); |
| 13021 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 13022 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13023 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13024 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13025 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 13026 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13027 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 13028 | |
| 13029 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13030 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 13031 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13032 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 13033 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13034 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13035 | /* |
| 13036 | * Step 2 of weak-linking support on Mac OS X. |
| 13037 | * |
| 13038 | * The code below removes functions that are not available on the |
| 13039 | * currently active platform. |
| 13040 | * |
| 13041 | * 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] | 13042 | * 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] | 13043 | * OSX 10.4. |
| 13044 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13045 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13046 | if (fstatvfs == NULL) { |
| 13047 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 13048 | return NULL; |
| 13049 | } |
| 13050 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13051 | #endif /* HAVE_FSTATVFS */ |
| 13052 | |
| 13053 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13054 | if (statvfs == NULL) { |
| 13055 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 13056 | return NULL; |
| 13057 | } |
| 13058 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13059 | #endif /* HAVE_STATVFS */ |
| 13060 | |
| 13061 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13062 | if (lchown == NULL) { |
| 13063 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 13064 | return NULL; |
| 13065 | } |
| 13066 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13067 | #endif /* HAVE_LCHOWN */ |
| 13068 | |
| 13069 | |
| 13070 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13071 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 13072 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13073 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 13074 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 13075 | billion = PyLong_FromLong(1000000000); |
| 13076 | if (!billion) |
| 13077 | return NULL; |
| 13078 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13079 | /* suppress "function not used" warnings */ |
| 13080 | { |
| 13081 | int ignored; |
| 13082 | fd_specified("", -1); |
| 13083 | follow_symlinks_specified("", 1); |
| 13084 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 13085 | dir_fd_converter(Py_None, &ignored); |
| 13086 | dir_fd_unavailable(Py_None, &ignored); |
| 13087 | } |
| 13088 | |
| 13089 | /* |
| 13090 | * provide list of locally available functions |
| 13091 | * so os.py can populate support_* lists |
| 13092 | */ |
| 13093 | list = PyList_New(0); |
| 13094 | if (!list) |
| 13095 | return NULL; |
| 13096 | for (trace = have_functions; *trace; trace++) { |
| 13097 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 13098 | if (!unicode) |
| 13099 | return NULL; |
| 13100 | if (PyList_Append(list, unicode)) |
| 13101 | return NULL; |
| 13102 | Py_DECREF(unicode); |
| 13103 | } |
| 13104 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 13105 | |
| 13106 | Py_INCREF((PyObject *) &DirEntryType); |
Brett Cannon | a32c4d0 | 2016-06-24 14:14:44 -0700 | [diff] [blame] | 13107 | PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13108 | |
| 13109 | initialized = 1; |
| 13110 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13111 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13112 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13113 | |
| 13114 | #ifdef __cplusplus |
| 13115 | } |
| 13116 | #endif |